blob: 97c48b5b05789a2d70a86c42ee48243ab7ec766f [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 *
15 * Goal-directed block allocation by Stephen Tweedie
16 * (sct@redhat.com), 1993, 1998
17 * Big-endian to little-endian byte-swapping/bitmaps by
18 * David S. Miller (davem@caip.rutgers.edu), 1995
19 * 64-bit file support on 64-bit platforms by Jakub Jelinek
20 * (jj@sunsite.ms.mff.cuni.cz)
21 *
Mingming Cao617ba132006-10-11 01:20:53 -070022 * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
Dave Kleikampac27a0e2006-10-11 01:20:50 -070023 */
24
25#include <linux/module.h>
26#include <linux/fs.h>
27#include <linux/time.h>
Mingming Caodab291a2006-10-11 01:21:01 -070028#include <linux/jbd2.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070029#include <linux/highuid.h>
30#include <linux/pagemap.h>
31#include <linux/quotaops.h>
32#include <linux/string.h>
33#include <linux/buffer_head.h>
34#include <linux/writeback.h>
Alex Tomas64769242008-07-11 19:27:31 -040035#include <linux/pagevec.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070036#include <linux/mpage.h>
Duane Griffine83c1392008-12-19 20:47:15 +000037#include <linux/namei.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070038#include <linux/uio.h>
39#include <linux/bio.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040040
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040041#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070042#include "xattr.h"
43#include "acl.h"
Mingming Caod2a17632008-07-14 17:52:37 -040044#include "ext4_extents.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070045
Theodore Ts'o9bffad12009-06-17 11:48:11 -040046#include <trace/events/ext4.h>
47
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -040048#define MPAGE_DA_EXTENT_TAIL 0x01
49
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -040050static int __ext4_journalled_writepage(struct page *page,
51 struct writeback_control *wbc,
52 unsigned int len);
53
Jan Kara678aaf42008-07-11 19:27:31 -040054static inline int ext4_begin_ordered_truncate(struct inode *inode,
55 loff_t new_size)
56{
Jan Kara7f5aa212009-02-10 11:15:34 -050057 return jbd2_journal_begin_ordered_truncate(
58 EXT4_SB(inode->i_sb)->s_journal,
59 &EXT4_I(inode)->jinode,
60 new_size);
Jan Kara678aaf42008-07-11 19:27:31 -040061}
62
Alex Tomas64769242008-07-11 19:27:31 -040063static void ext4_invalidatepage(struct page *page, unsigned long offset);
64
Dave Kleikampac27a0e2006-10-11 01:20:50 -070065/*
66 * Test whether an inode is a fast symlink.
67 */
Mingming Cao617ba132006-10-11 01:20:53 -070068static int ext4_inode_is_fast_symlink(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070069{
Mingming Cao617ba132006-10-11 01:20:53 -070070 int ea_blocks = EXT4_I(inode)->i_file_acl ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -070071 (inode->i_sb->s_blocksize >> 9) : 0;
72
73 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
74}
75
76/*
Mingming Cao617ba132006-10-11 01:20:53 -070077 * The ext4 forget function must perform a revoke if we are freeing data
Dave Kleikampac27a0e2006-10-11 01:20:50 -070078 * which has been journaled. Metadata (eg. indirect blocks) must be
79 * revoked in all cases.
80 *
81 * "bh" may be NULL: a metadata block may have been freed from memory
82 * but there may still be a record of it in the journal, and that record
83 * still needs to be revoked.
Frank Mayhar03901312009-01-07 00:06:22 -050084 *
85 * If the handle isn't valid we're not journaling so there's nothing to do.
Dave Kleikampac27a0e2006-10-11 01:20:50 -070086 */
Mingming Cao617ba132006-10-11 01:20:53 -070087int ext4_forget(handle_t *handle, int is_metadata, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -040088 struct buffer_head *bh, ext4_fsblk_t blocknr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070089{
90 int err;
91
Frank Mayhar03901312009-01-07 00:06:22 -050092 if (!ext4_handle_valid(handle))
93 return 0;
94
Dave Kleikampac27a0e2006-10-11 01:20:50 -070095 might_sleep();
96
97 BUFFER_TRACE(bh, "enter");
98
99 jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
Theodore Ts'o7f4520c2009-06-13 10:09:41 -0400100 "data mode %x\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700101 bh, is_metadata, inode->i_mode,
102 test_opt(inode->i_sb, DATA_FLAGS));
103
104 /* Never use the revoke function if we are doing full data
105 * journaling: there is no need to, and a V1 superblock won't
106 * support it. Otherwise, only skip the revoke on un-journaled
107 * data blocks. */
108
Mingming Cao617ba132006-10-11 01:20:53 -0700109 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
110 (!is_metadata && !ext4_should_journal_data(inode))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700111 if (bh) {
Mingming Caodab291a2006-10-11 01:21:01 -0700112 BUFFER_TRACE(bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700113 return ext4_journal_forget(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700114 }
115 return 0;
116 }
117
118 /*
119 * data!=journal && (is_metadata || should_journal_data(inode))
120 */
Mingming Cao617ba132006-10-11 01:20:53 -0700121 BUFFER_TRACE(bh, "call ext4_journal_revoke");
122 err = ext4_journal_revoke(handle, blocknr, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700123 if (err)
Harvey Harrison46e665e2008-04-17 10:38:59 -0400124 ext4_abort(inode->i_sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700125 "error %d when attempting revoke", err);
126 BUFFER_TRACE(bh, "exit");
127 return err;
128}
129
130/*
131 * Work out how many blocks we need to proceed with the next chunk of a
132 * truncate transaction.
133 */
134static unsigned long blocks_for_truncate(struct inode *inode)
135{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500136 ext4_lblk_t needed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700137
138 needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
139
140 /* Give ourselves just enough room to cope with inodes in which
141 * i_blocks is corrupt: we've seen disk corruptions in the past
142 * which resulted in random data in an inode which looked enough
Mingming Cao617ba132006-10-11 01:20:53 -0700143 * like a regular file for ext4 to try to delete it. Things
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700144 * will go a bit crazy if that happens, but at least we should
145 * try not to panic the whole kernel. */
146 if (needed < 2)
147 needed = 2;
148
149 /* But we need to bound the transaction so we don't overflow the
150 * journal. */
Mingming Cao617ba132006-10-11 01:20:53 -0700151 if (needed > EXT4_MAX_TRANS_DATA)
152 needed = EXT4_MAX_TRANS_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700153
Mingming Cao617ba132006-10-11 01:20:53 -0700154 return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700155}
156
157/*
158 * Truncate transactions can be complex and absolutely huge. So we need to
159 * be able to restart the transaction at a conventient checkpoint to make
160 * sure we don't overflow the journal.
161 *
162 * start_transaction gets us a new handle for a truncate transaction,
163 * and extend_transaction tries to extend the existing one a bit. If
164 * extend fails, we need to propagate the failure up and restart the
165 * transaction in the top-level truncate loop. --sct
166 */
167static handle_t *start_transaction(struct inode *inode)
168{
169 handle_t *result;
170
Mingming Cao617ba132006-10-11 01:20:53 -0700171 result = ext4_journal_start(inode, blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700172 if (!IS_ERR(result))
173 return result;
174
Mingming Cao617ba132006-10-11 01:20:53 -0700175 ext4_std_error(inode->i_sb, PTR_ERR(result));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700176 return result;
177}
178
179/*
180 * Try to extend this transaction for the purposes of truncation.
181 *
182 * Returns 0 if we managed to create more room. If we can't create more
183 * room, and the transaction must be restarted we return 1.
184 */
185static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
186{
Frank Mayhar03901312009-01-07 00:06:22 -0500187 if (!ext4_handle_valid(handle))
188 return 0;
189 if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700190 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700191 if (!ext4_journal_extend(handle, blocks_for_truncate(inode)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700192 return 0;
193 return 1;
194}
195
196/*
197 * Restart the transaction associated with *handle. This does a commit,
198 * so before we call here everything must be consistently dirtied against
199 * this transaction.
200 */
Mingming Cao617ba132006-10-11 01:20:53 -0700201static int ext4_journal_test_restart(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700202{
Frank Mayhar03901312009-01-07 00:06:22 -0500203 BUG_ON(EXT4_JOURNAL(inode) == NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700204 jbd_debug(2, "restarting handle %p\n", handle);
Mingming Cao617ba132006-10-11 01:20:53 -0700205 return ext4_journal_restart(handle, blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700206}
207
208/*
209 * Called at the last iput() if i_nlink is zero.
210 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400211void ext4_delete_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700212{
213 handle_t *handle;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400214 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700215
Jan Kara678aaf42008-07-11 19:27:31 -0400216 if (ext4_should_order_data(inode))
217 ext4_begin_ordered_truncate(inode, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700218 truncate_inode_pages(&inode->i_data, 0);
219
220 if (is_bad_inode(inode))
221 goto no_delete;
222
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400223 handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700224 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400225 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700226 /*
227 * If we're going to skip the normal cleanup, we still need to
228 * make sure that the in-core orphan linked list is properly
229 * cleaned up.
230 */
Mingming Cao617ba132006-10-11 01:20:53 -0700231 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700232 goto no_delete;
233 }
234
235 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500236 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700237 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400238 err = ext4_mark_inode_dirty(handle, inode);
239 if (err) {
240 ext4_warning(inode->i_sb, __func__,
241 "couldn't mark inode dirty (err %d)", err);
242 goto stop_handle;
243 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700244 if (inode->i_blocks)
Mingming Cao617ba132006-10-11 01:20:53 -0700245 ext4_truncate(inode);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400246
247 /*
248 * ext4_ext_truncate() doesn't reserve any slop when it
249 * restarts journal transactions; therefore there may not be
250 * enough credits left in the handle to remove the inode from
251 * the orphan list and set the dtime field.
252 */
Frank Mayhar03901312009-01-07 00:06:22 -0500253 if (!ext4_handle_has_enough_credits(handle, 3)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400254 err = ext4_journal_extend(handle, 3);
255 if (err > 0)
256 err = ext4_journal_restart(handle, 3);
257 if (err != 0) {
258 ext4_warning(inode->i_sb, __func__,
259 "couldn't extend journal (err %d)", err);
260 stop_handle:
261 ext4_journal_stop(handle);
262 goto no_delete;
263 }
264 }
265
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700266 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700267 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700268 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700269 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700270 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700271 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700272 * (Well, we could do this if we need to, but heck - it works)
273 */
Mingming Cao617ba132006-10-11 01:20:53 -0700274 ext4_orphan_del(handle, inode);
275 EXT4_I(inode)->i_dtime = get_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700276
277 /*
278 * One subtle ordering requirement: if anything has gone wrong
279 * (transaction abort, IO errors, whatever), then we can still
280 * do these next steps (the fs will already have been marked as
281 * having errors), but we can't free the inode if the mark_dirty
282 * fails.
283 */
Mingming Cao617ba132006-10-11 01:20:53 -0700284 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700285 /* If that failed, just do the required in-core inode clear. */
286 clear_inode(inode);
287 else
Mingming Cao617ba132006-10-11 01:20:53 -0700288 ext4_free_inode(handle, inode);
289 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700290 return;
291no_delete:
292 clear_inode(inode); /* We must guarantee clearing of inode... */
293}
294
295typedef struct {
296 __le32 *p;
297 __le32 key;
298 struct buffer_head *bh;
299} Indirect;
300
301static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
302{
303 p->key = *(p->p = v);
304 p->bh = bh;
305}
306
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700307/**
Mingming Cao617ba132006-10-11 01:20:53 -0700308 * ext4_block_to_path - parse the block number into array of offsets
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700309 * @inode: inode in question (we are only interested in its superblock)
310 * @i_block: block number to be parsed
311 * @offsets: array to store the offsets in
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400312 * @boundary: set this non-zero if the referred-to block is likely to be
313 * followed (on disk) by an indirect block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700314 *
Mingming Cao617ba132006-10-11 01:20:53 -0700315 * To store the locations of file's data ext4 uses a data structure common
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700316 * for UNIX filesystems - tree of pointers anchored in the inode, with
317 * data blocks at leaves and indirect blocks in intermediate nodes.
318 * This function translates the block number into path in that tree -
319 * return value is the path length and @offsets[n] is the offset of
320 * pointer to (n+1)th node in the nth one. If @block is out of range
321 * (negative or too large) warning is printed and zero returned.
322 *
323 * Note: function doesn't find node addresses, so no IO is needed. All
324 * we need to know is the capacity of indirect blocks (taken from the
325 * inode->i_sb).
326 */
327
328/*
329 * Portability note: the last comparison (check that we fit into triple
330 * indirect block) is spelled differently, because otherwise on an
331 * architecture with 32-bit longs and 8Kb pages we might get into trouble
332 * if our filesystem had 8Kb blocks. We might use long long, but that would
333 * kill us on x86. Oh, well, at least the sign propagation does not matter -
334 * i_block would have to be negative in the very beginning, so we would not
335 * get there at all.
336 */
337
Mingming Cao617ba132006-10-11 01:20:53 -0700338static int ext4_block_to_path(struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400339 ext4_lblk_t i_block,
340 ext4_lblk_t offsets[4], int *boundary)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700341{
Mingming Cao617ba132006-10-11 01:20:53 -0700342 int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb);
343 int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb);
344 const long direct_blocks = EXT4_NDIR_BLOCKS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700345 indirect_blocks = ptrs,
346 double_blocks = (1 << (ptrs_bits * 2));
347 int n = 0;
348 int final = 0;
349
350 if (i_block < 0) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400351 ext4_warning(inode->i_sb, "ext4_block_to_path", "block < 0");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700352 } else if (i_block < direct_blocks) {
353 offsets[n++] = i_block;
354 final = direct_blocks;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400355 } else if ((i_block -= direct_blocks) < indirect_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700356 offsets[n++] = EXT4_IND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700357 offsets[n++] = i_block;
358 final = ptrs;
359 } else if ((i_block -= indirect_blocks) < double_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700360 offsets[n++] = EXT4_DIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700361 offsets[n++] = i_block >> ptrs_bits;
362 offsets[n++] = i_block & (ptrs - 1);
363 final = ptrs;
364 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
Mingming Cao617ba132006-10-11 01:20:53 -0700365 offsets[n++] = EXT4_TIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700366 offsets[n++] = i_block >> (ptrs_bits * 2);
367 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
368 offsets[n++] = i_block & (ptrs - 1);
369 final = ptrs;
370 } else {
Eric Sandeene2b46572008-01-28 23:58:27 -0500371 ext4_warning(inode->i_sb, "ext4_block_to_path",
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400372 "block %lu > max in inode %lu",
373 i_block + direct_blocks +
374 indirect_blocks + double_blocks, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700375 }
376 if (boundary)
377 *boundary = final - 1 - (i_block & (ptrs - 1));
378 return n;
379}
380
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400381static int __ext4_check_blockref(const char *function, struct inode *inode,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400382 __le32 *p, unsigned int max)
383{
Thiemo Nagelf73953c2009-04-07 18:46:47 -0400384 __le32 *bref = p;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400385 unsigned int blk;
386
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400387 while (bref < p+max) {
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400388 blk = le32_to_cpu(*bref++);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400389 if (blk &&
390 unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb),
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400391 blk, 1))) {
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400392 ext4_error(inode->i_sb, function,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400393 "invalid block reference %u "
394 "in inode #%lu", blk, inode->i_ino);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400395 return -EIO;
396 }
397 }
398 return 0;
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400399}
400
401
402#define ext4_check_indirect_blockref(inode, bh) \
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400403 __ext4_check_blockref(__func__, inode, (__le32 *)(bh)->b_data, \
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400404 EXT4_ADDR_PER_BLOCK((inode)->i_sb))
405
406#define ext4_check_inode_blockref(inode) \
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400407 __ext4_check_blockref(__func__, inode, EXT4_I(inode)->i_data, \
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400408 EXT4_NDIR_BLOCKS)
409
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700410/**
Mingming Cao617ba132006-10-11 01:20:53 -0700411 * ext4_get_branch - read the chain of indirect blocks leading to data
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700412 * @inode: inode in question
413 * @depth: depth of the chain (1 - direct pointer, etc.)
414 * @offsets: offsets of pointers in inode/indirect blocks
415 * @chain: place to store the result
416 * @err: here we store the error value
417 *
418 * Function fills the array of triples <key, p, bh> and returns %NULL
419 * if everything went OK or the pointer to the last filled triple
420 * (incomplete one) otherwise. Upon the return chain[i].key contains
421 * the number of (i+1)-th block in the chain (as it is stored in memory,
422 * i.e. little-endian 32-bit), chain[i].p contains the address of that
423 * number (it points into struct inode for i==0 and into the bh->b_data
424 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
425 * block for i>0 and NULL for i==0. In other words, it holds the block
426 * numbers of the chain, addresses they were taken from (and where we can
427 * verify that chain did not change) and buffer_heads hosting these
428 * numbers.
429 *
430 * Function stops when it stumbles upon zero pointer (absent block)
431 * (pointer to last triple returned, *@err == 0)
432 * or when it gets an IO error reading an indirect block
433 * (ditto, *@err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700434 * or when it reads all @depth-1 indirect blocks successfully and finds
435 * the whole chain, all way to the data (returns %NULL, *err == 0).
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500436 *
437 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500438 * down_read(&EXT4_I(inode)->i_data_sem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700439 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500440static Indirect *ext4_get_branch(struct inode *inode, int depth,
441 ext4_lblk_t *offsets,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700442 Indirect chain[4], int *err)
443{
444 struct super_block *sb = inode->i_sb;
445 Indirect *p = chain;
446 struct buffer_head *bh;
447
448 *err = 0;
449 /* i_data is not going away, no lock needed */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400450 add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700451 if (!p->key)
452 goto no_block;
453 while (--depth) {
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400454 bh = sb_getblk(sb, le32_to_cpu(p->key));
455 if (unlikely(!bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700456 goto failure;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400457
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400458 if (!bh_uptodate_or_lock(bh)) {
459 if (bh_submit_read(bh) < 0) {
460 put_bh(bh);
461 goto failure;
462 }
463 /* validate block references */
464 if (ext4_check_indirect_blockref(inode, bh)) {
465 put_bh(bh);
466 goto failure;
467 }
468 }
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400469
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400470 add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700471 /* Reader: end */
472 if (!p->key)
473 goto no_block;
474 }
475 return NULL;
476
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700477failure:
478 *err = -EIO;
479no_block:
480 return p;
481}
482
483/**
Mingming Cao617ba132006-10-11 01:20:53 -0700484 * ext4_find_near - find a place for allocation with sufficient locality
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700485 * @inode: owner
486 * @ind: descriptor of indirect block.
487 *
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000488 * This function returns the preferred place for block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700489 * It is used when heuristic for sequential allocation fails.
490 * Rules are:
491 * + if there is a block to the left of our position - allocate near it.
492 * + if pointer will live in indirect block - allocate near that block.
493 * + if pointer will live in inode - allocate in the same
494 * cylinder group.
495 *
496 * In the latter case we colour the starting block by the callers PID to
497 * prevent it from clashing with concurrent allocations for a different inode
498 * in the same block group. The PID is used here so that functionally related
499 * files will be close-by on-disk.
500 *
501 * Caller must make sure that @ind is valid and will stay that way.
502 */
Mingming Cao617ba132006-10-11 01:20:53 -0700503static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700504{
Mingming Cao617ba132006-10-11 01:20:53 -0700505 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400506 __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700507 __le32 *p;
Mingming Cao617ba132006-10-11 01:20:53 -0700508 ext4_fsblk_t bg_start;
Valerie Clement74d34872008-02-15 13:43:07 -0500509 ext4_fsblk_t last_block;
Mingming Cao617ba132006-10-11 01:20:53 -0700510 ext4_grpblk_t colour;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400511 ext4_group_t block_group;
512 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700513
514 /* Try to find previous block */
515 for (p = ind->p - 1; p >= start; p--) {
516 if (*p)
517 return le32_to_cpu(*p);
518 }
519
520 /* No such thing, so let's try location of indirect block */
521 if (ind->bh)
522 return ind->bh->b_blocknr;
523
524 /*
525 * It is going to be referred to from the inode itself? OK, just put it
526 * into the same cylinder group then.
527 */
Theodore Ts'oa4912122009-03-12 12:18:34 -0400528 block_group = ei->i_block_group;
529 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
530 block_group &= ~(flex_size-1);
531 if (S_ISREG(inode->i_mode))
532 block_group++;
533 }
534 bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
Valerie Clement74d34872008-02-15 13:43:07 -0500535 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
536
Theodore Ts'oa4912122009-03-12 12:18:34 -0400537 /*
538 * If we are doing delayed allocation, we don't need take
539 * colour into account.
540 */
541 if (test_opt(inode->i_sb, DELALLOC))
542 return bg_start;
543
Valerie Clement74d34872008-02-15 13:43:07 -0500544 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
545 colour = (current->pid % 16) *
Mingming Cao617ba132006-10-11 01:20:53 -0700546 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
Valerie Clement74d34872008-02-15 13:43:07 -0500547 else
548 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700549 return bg_start + colour;
550}
551
552/**
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000553 * ext4_find_goal - find a preferred place for allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700554 * @inode: owner
555 * @block: block we want
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700556 * @partial: pointer to the last triple within a chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700557 *
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000558 * Normally this function find the preferred place for block allocation,
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800559 * returns it.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700560 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500561static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400562 Indirect *partial)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700564 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400565 * XXX need to get goal block from mballoc's data structures
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700566 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700567
Mingming Cao617ba132006-10-11 01:20:53 -0700568 return ext4_find_near(inode, partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700569}
570
571/**
Mingming Cao617ba132006-10-11 01:20:53 -0700572 * ext4_blks_to_allocate: Look up the block map and count the number
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700573 * of direct blocks need to be allocated for the given branch.
574 *
575 * @branch: chain of indirect blocks
576 * @k: number of blocks need for indirect blocks
577 * @blks: number of data blocks to be mapped.
578 * @blocks_to_boundary: the offset in the indirect block
579 *
580 * return the total number of blocks to be allocate, including the
581 * direct and indirect blocks.
582 */
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500583static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400584 int blocks_to_boundary)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700585{
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500586 unsigned int count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700587
588 /*
589 * Simple case, [t,d]Indirect block(s) has not allocated yet
590 * then it's clear blocks on that path have not allocated
591 */
592 if (k > 0) {
593 /* right now we don't handle cross boundary allocation */
594 if (blks < blocks_to_boundary + 1)
595 count += blks;
596 else
597 count += blocks_to_boundary + 1;
598 return count;
599 }
600
601 count++;
602 while (count < blks && count <= blocks_to_boundary &&
603 le32_to_cpu(*(branch[0].p + count)) == 0) {
604 count++;
605 }
606 return count;
607}
608
609/**
Mingming Cao617ba132006-10-11 01:20:53 -0700610 * ext4_alloc_blocks: multiple allocate blocks needed for a branch
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700611 * @indirect_blks: the number of blocks need to allocate for indirect
612 * blocks
613 *
614 * @new_blocks: on return it will store the new block numbers for
615 * the indirect blocks(if needed) and the first direct block,
616 * @blks: on return it will store the total number of allocated
617 * direct blocks
618 */
Mingming Cao617ba132006-10-11 01:20:53 -0700619static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400620 ext4_lblk_t iblock, ext4_fsblk_t goal,
621 int indirect_blks, int blks,
622 ext4_fsblk_t new_blocks[4], int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700623{
Theodore Ts'o815a1132009-01-01 23:59:43 -0500624 struct ext4_allocation_request ar;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700625 int target, i;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400626 unsigned long count = 0, blk_allocated = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700627 int index = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700628 ext4_fsblk_t current_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700629 int ret = 0;
630
631 /*
632 * Here we try to allocate the requested multiple blocks at once,
633 * on a best-effort basis.
634 * To build a branch, we should allocate blocks for
635 * the indirect blocks(if not allocated yet), and at least
636 * the first direct block of this branch. That's the
637 * minimum number of blocks need to allocate(required)
638 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400639 /* first we try to allocate the indirect blocks */
640 target = indirect_blks;
641 while (target > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700642 count = target;
643 /* allocating blocks for indirect blocks and direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400644 current_block = ext4_new_meta_blocks(handle, inode,
645 goal, &count, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700646 if (*err)
647 goto failed_out;
648
649 target -= count;
650 /* allocate blocks for indirect blocks */
651 while (index < indirect_blks && count) {
652 new_blocks[index++] = current_block++;
653 count--;
654 }
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400655 if (count > 0) {
656 /*
657 * save the new block number
658 * for the first direct block
659 */
660 new_blocks[index] = current_block;
661 printk(KERN_INFO "%s returned more blocks than "
662 "requested\n", __func__);
663 WARN_ON(1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700664 break;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400665 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700666 }
667
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400668 target = blks - count ;
669 blk_allocated = count;
670 if (!target)
671 goto allocated;
672 /* Now allocate data blocks */
Theodore Ts'o815a1132009-01-01 23:59:43 -0500673 memset(&ar, 0, sizeof(ar));
674 ar.inode = inode;
675 ar.goal = goal;
676 ar.len = target;
677 ar.logical = iblock;
678 if (S_ISREG(inode->i_mode))
679 /* enable in-core preallocation only for regular files */
680 ar.flags = EXT4_MB_HINT_DATA;
681
682 current_block = ext4_mb_new_blocks(handle, &ar, err);
683
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400684 if (*err && (target == blks)) {
685 /*
686 * if the allocation failed and we didn't allocate
687 * any blocks before
688 */
689 goto failed_out;
690 }
691 if (!*err) {
692 if (target == blks) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400693 /*
694 * save the new block number
695 * for the first direct block
696 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400697 new_blocks[index] = current_block;
698 }
Theodore Ts'o815a1132009-01-01 23:59:43 -0500699 blk_allocated += ar.len;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400700 }
701allocated:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700702 /* total number of blocks allocated for direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400703 ret = blk_allocated;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700704 *err = 0;
705 return ret;
706failed_out:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400707 for (i = 0; i < index; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500708 ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700709 return ret;
710}
711
712/**
Mingming Cao617ba132006-10-11 01:20:53 -0700713 * ext4_alloc_branch - allocate and set up a chain of blocks.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700714 * @inode: owner
715 * @indirect_blks: number of allocated indirect blocks
716 * @blks: number of allocated direct blocks
717 * @offsets: offsets (in the blocks) to store the pointers to next.
718 * @branch: place to store the chain in.
719 *
720 * This function allocates blocks, zeroes out all but the last one,
721 * links them into chain and (if we are synchronous) writes them to disk.
722 * In other words, it prepares a branch that can be spliced onto the
723 * inode. It stores the information about that chain in the branch[], in
Mingming Cao617ba132006-10-11 01:20:53 -0700724 * the same format as ext4_get_branch() would do. We are calling it after
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700725 * we had read the existing part of chain and partial points to the last
726 * triple of that (one with zero ->key). Upon the exit we have the same
Mingming Cao617ba132006-10-11 01:20:53 -0700727 * picture as after the successful ext4_get_block(), except that in one
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700728 * place chain is disconnected - *branch->p is still zero (we did not
729 * set the last link), but branch->key contains the number that should
730 * be placed into *branch->p to fill that gap.
731 *
732 * If allocation fails we free all blocks we've allocated (and forget
733 * their buffer_heads) and return the error value the from failed
Mingming Cao617ba132006-10-11 01:20:53 -0700734 * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700735 * as described above and return 0.
736 */
Mingming Cao617ba132006-10-11 01:20:53 -0700737static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400738 ext4_lblk_t iblock, int indirect_blks,
739 int *blks, ext4_fsblk_t goal,
740 ext4_lblk_t *offsets, Indirect *branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700741{
742 int blocksize = inode->i_sb->s_blocksize;
743 int i, n = 0;
744 int err = 0;
745 struct buffer_head *bh;
746 int num;
Mingming Cao617ba132006-10-11 01:20:53 -0700747 ext4_fsblk_t new_blocks[4];
748 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700749
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400750 num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700751 *blks, new_blocks, &err);
752 if (err)
753 return err;
754
755 branch[0].key = cpu_to_le32(new_blocks[0]);
756 /*
757 * metadata blocks and data blocks are allocated.
758 */
759 for (n = 1; n <= indirect_blks; n++) {
760 /*
761 * Get buffer_head for parent block, zero it out
762 * and set the pointer to new one, then send
763 * parent to disk.
764 */
765 bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
766 branch[n].bh = bh;
767 lock_buffer(bh);
768 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700769 err = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700770 if (err) {
771 unlock_buffer(bh);
772 brelse(bh);
773 goto failed;
774 }
775
776 memset(bh->b_data, 0, blocksize);
777 branch[n].p = (__le32 *) bh->b_data + offsets[n];
778 branch[n].key = cpu_to_le32(new_blocks[n]);
779 *branch[n].p = branch[n].key;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400780 if (n == indirect_blks) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700781 current_block = new_blocks[n];
782 /*
783 * End of chain, update the last new metablock of
784 * the chain to point to the new allocated
785 * data blocks numbers
786 */
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400787 for (i = 1; i < num; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700788 *(branch[n].p + i) = cpu_to_le32(++current_block);
789 }
790 BUFFER_TRACE(bh, "marking uptodate");
791 set_buffer_uptodate(bh);
792 unlock_buffer(bh);
793
Frank Mayhar03901312009-01-07 00:06:22 -0500794 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
795 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700796 if (err)
797 goto failed;
798 }
799 *blks = num;
800 return err;
801failed:
802 /* Allocation failed, free what we already allocated */
803 for (i = 1; i <= n ; i++) {
Mingming Caodab291a2006-10-11 01:21:01 -0700804 BUFFER_TRACE(branch[i].bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700805 ext4_journal_forget(handle, branch[i].bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700806 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400807 for (i = 0; i < indirect_blks; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500808 ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700809
Alex Tomasc9de5602008-01-29 00:19:52 -0500810 ext4_free_blocks(handle, inode, new_blocks[i], num, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700811
812 return err;
813}
814
815/**
Mingming Cao617ba132006-10-11 01:20:53 -0700816 * ext4_splice_branch - splice the allocated branch onto inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700817 * @inode: owner
818 * @block: (logical) number of block we are adding
819 * @chain: chain of indirect blocks (with a missing link - see
Mingming Cao617ba132006-10-11 01:20:53 -0700820 * ext4_alloc_branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700821 * @where: location of missing link
822 * @num: number of indirect blocks we are adding
823 * @blks: number of direct blocks we are adding
824 *
825 * This function fills the missing link and does all housekeeping needed in
826 * inode (->i_blocks, etc.). In case of success we end up with the full
827 * chain to new block and return 0.
828 */
Mingming Cao617ba132006-10-11 01:20:53 -0700829static int ext4_splice_branch(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400830 ext4_lblk_t block, Indirect *where, int num,
831 int blks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700832{
833 int i;
834 int err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700835 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700836
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700837 /*
838 * If we're splicing into a [td]indirect block (as opposed to the
839 * inode) then we need to get write access to the [td]indirect block
840 * before the splice.
841 */
842 if (where->bh) {
843 BUFFER_TRACE(where->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700844 err = ext4_journal_get_write_access(handle, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700845 if (err)
846 goto err_out;
847 }
848 /* That's it */
849
850 *where->p = where->key;
851
852 /*
853 * Update the host buffer_head or inode to point to more just allocated
854 * direct blocks blocks
855 */
856 if (num == 0 && blks > 1) {
857 current_block = le32_to_cpu(where->key) + 1;
858 for (i = 1; i < blks; i++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400859 *(where->p + i) = cpu_to_le32(current_block++);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700860 }
861
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700862 /* We are done with atomic stuff, now do the rest of housekeeping */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700863 /* had we spliced it onto indirect block? */
864 if (where->bh) {
865 /*
866 * If we spliced it onto an indirect block, we haven't
867 * altered the inode. Note however that if it is being spliced
868 * onto an indirect block at the very end of the file (the
869 * file is growing) then we *will* alter the inode to reflect
870 * the new i_size. But that is not done here - it is done in
Mingming Cao617ba132006-10-11 01:20:53 -0700871 * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700872 */
873 jbd_debug(5, "splicing indirect only\n");
Frank Mayhar03901312009-01-07 00:06:22 -0500874 BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata");
875 err = ext4_handle_dirty_metadata(handle, inode, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700876 if (err)
877 goto err_out;
878 } else {
879 /*
880 * OK, we spliced it into the inode itself on a direct block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700881 */
Theodore Ts'o41591752009-06-15 03:41:23 -0400882 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700883 jbd_debug(5, "splicing direct\n");
884 }
885 return err;
886
887err_out:
888 for (i = 1; i <= num; i++) {
Mingming Caodab291a2006-10-11 01:21:01 -0700889 BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700890 ext4_journal_forget(handle, where[i].bh);
Alex Tomasc9de5602008-01-29 00:19:52 -0500891 ext4_free_blocks(handle, inode,
892 le32_to_cpu(where[i-1].key), 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700893 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500894 ext4_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700895
896 return err;
897}
898
899/*
Theodore Ts'ob920c752009-05-14 00:54:29 -0400900 * The ext4_ind_get_blocks() function handles non-extents inodes
901 * (i.e., using the traditional indirect/double-indirect i_blocks
902 * scheme) for ext4_get_blocks().
903 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700904 * Allocation strategy is simple: if we have to allocate something, we will
905 * have to go the whole way to leaf. So let's do it before attaching anything
906 * to tree, set linkage between the newborn blocks, write them if sync is
907 * required, recheck the path, free and repeat if check fails, otherwise
908 * set the last missing link (that will protect us from any truncate-generated
909 * removals - all blocks on the path are immune now) and possibly force the
910 * write on the parent block.
911 * That has a nice additional property: no special recovery from the failed
912 * allocations is needed - we simply release blocks and do not touch anything
913 * reachable from inode.
914 *
915 * `handle' can be NULL if create == 0.
916 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700917 * return > 0, # of blocks mapped or allocated.
918 * return = 0, if plain lookup failed.
919 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500920 *
Theodore Ts'ob920c752009-05-14 00:54:29 -0400921 * The ext4_ind_get_blocks() function should be called with
922 * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem
923 * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or
924 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system
925 * blocks.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700926 */
Theodore Ts'oe4d996c2009-05-12 00:25:28 -0400927static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400928 ext4_lblk_t iblock, unsigned int maxblocks,
929 struct buffer_head *bh_result,
930 int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700931{
932 int err = -EIO;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500933 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700934 Indirect chain[4];
935 Indirect *partial;
Mingming Cao617ba132006-10-11 01:20:53 -0700936 ext4_fsblk_t goal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700937 int indirect_blks;
938 int blocks_to_boundary = 0;
939 int depth;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700940 int count = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700941 ext4_fsblk_t first_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700942
Alex Tomasa86c6182006-10-11 01:21:03 -0700943 J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL));
Theodore Ts'oc2177052009-05-14 00:58:52 -0400944 J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500945 depth = ext4_block_to_path(inode, iblock, offsets,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400946 &blocks_to_boundary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700947
948 if (depth == 0)
949 goto out;
950
Mingming Cao617ba132006-10-11 01:20:53 -0700951 partial = ext4_get_branch(inode, depth, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700952
953 /* Simplest case - block found, no allocation needed */
954 if (!partial) {
955 first_block = le32_to_cpu(chain[depth - 1].key);
956 clear_buffer_new(bh_result);
957 count++;
958 /*map more blocks*/
959 while (count < maxblocks && count <= blocks_to_boundary) {
Mingming Cao617ba132006-10-11 01:20:53 -0700960 ext4_fsblk_t blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700961
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700962 blk = le32_to_cpu(*(chain[depth-1].p + count));
963
964 if (blk == first_block + count)
965 count++;
966 else
967 break;
968 }
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500969 goto got_it;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700970 }
971
972 /* Next simple case - plain lookup or failed read of indirect block */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400973 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700974 goto cleanup;
975
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700976 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400977 * Okay, we need to do block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700978 */
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800979 goal = ext4_find_goal(inode, iblock, partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700980
981 /* the number of blocks need to allocate for [d,t]indirect blocks */
982 indirect_blks = (chain + depth) - partial - 1;
983
984 /*
985 * Next look up the indirect map to count the totoal number of
986 * direct blocks to allocate for this branch.
987 */
Mingming Cao617ba132006-10-11 01:20:53 -0700988 count = ext4_blks_to_allocate(partial, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700989 maxblocks, blocks_to_boundary);
990 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700991 * Block out ext4_truncate while we alter the tree
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700992 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400993 err = ext4_alloc_branch(handle, inode, iblock, indirect_blks,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400994 &count, goal,
995 offsets + (partial - chain), partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700996
997 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700998 * The ext4_splice_branch call will free and forget any buffers
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700999 * on the new chain if there is a failure, but that risks using
1000 * up transaction credits, especially for bitmaps where the
1001 * credits cannot be returned. Can we handle this somehow? We
1002 * may need to return -EAGAIN upwards in the worst case. --sct
1003 */
1004 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07001005 err = ext4_splice_branch(handle, inode, iblock,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001006 partial, indirect_blks, count);
1007 else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001008 goto cleanup;
1009
1010 set_buffer_new(bh_result);
1011got_it:
1012 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
1013 if (count > blocks_to_boundary)
1014 set_buffer_boundary(bh_result);
1015 err = count;
1016 /* Clean up and exit */
1017 partial = chain + depth - 1; /* the whole chain */
1018cleanup:
1019 while (partial > chain) {
1020 BUFFER_TRACE(partial->bh, "call brelse");
1021 brelse(partial->bh);
1022 partial--;
1023 }
1024 BUFFER_TRACE(bh_result, "returned");
1025out:
1026 return err;
1027}
1028
Mingming Cao60e58e02009-01-22 18:13:05 +01001029qsize_t ext4_get_reserved_space(struct inode *inode)
1030{
1031 unsigned long long total;
1032
1033 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1034 total = EXT4_I(inode)->i_reserved_data_blocks +
1035 EXT4_I(inode)->i_reserved_meta_blocks;
1036 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1037
1038 return total;
1039}
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001040/*
1041 * Calculate the number of metadata blocks need to reserve
1042 * to allocate @blocks for non extent file based file
1043 */
1044static int ext4_indirect_calc_metadata_amount(struct inode *inode, int blocks)
1045{
1046 int icap = EXT4_ADDR_PER_BLOCK(inode->i_sb);
1047 int ind_blks, dind_blks, tind_blks;
1048
1049 /* number of new indirect blocks needed */
1050 ind_blks = (blocks + icap - 1) / icap;
1051
1052 dind_blks = (ind_blks + icap - 1) / icap;
1053
1054 tind_blks = 1;
1055
1056 return ind_blks + dind_blks + tind_blks;
1057}
1058
1059/*
1060 * Calculate the number of metadata blocks need to reserve
1061 * to allocate given number of blocks
1062 */
1063static int ext4_calc_metadata_amount(struct inode *inode, int blocks)
1064{
Mingming Caocd213222008-08-19 22:16:59 -04001065 if (!blocks)
1066 return 0;
1067
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001068 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)
1069 return ext4_ext_calc_metadata_amount(inode, blocks);
1070
1071 return ext4_indirect_calc_metadata_amount(inode, blocks);
1072}
1073
1074static void ext4_da_update_reserve_space(struct inode *inode, int used)
1075{
1076 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1077 int total, mdb, mdb_free;
1078
1079 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1080 /* recalculate the number of metablocks still need to be reserved */
1081 total = EXT4_I(inode)->i_reserved_data_blocks - used;
1082 mdb = ext4_calc_metadata_amount(inode, total);
1083
1084 /* figure out how many metablocks to release */
1085 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1086 mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
1087
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001088 if (mdb_free) {
1089 /* Account for allocated meta_blocks */
1090 mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001091
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001092 /* update fs dirty blocks counter */
1093 percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free);
1094 EXT4_I(inode)->i_allocated_meta_blocks = 0;
1095 EXT4_I(inode)->i_reserved_meta_blocks = mdb;
1096 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001097
1098 /* update per-inode reservations */
1099 BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks);
1100 EXT4_I(inode)->i_reserved_data_blocks -= used;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001101 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001102
1103 /*
1104 * free those over-booking quota for metadata blocks
1105 */
Mingming Cao60e58e02009-01-22 18:13:05 +01001106 if (mdb_free)
1107 vfs_dq_release_reservation_block(inode, mdb_free);
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -04001108
1109 /*
1110 * If we have done all the pending block allocations and if
1111 * there aren't any writers on the inode, we can discard the
1112 * inode's preallocations.
1113 */
1114 if (!total && (atomic_read(&inode->i_writecount) == 0))
1115 ext4_discard_preallocations(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001116}
1117
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001118static int check_block_validity(struct inode *inode, sector_t logical,
1119 sector_t phys, int len)
1120{
1121 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), phys, len)) {
1122 ext4_error(inode->i_sb, "check_block_validity",
1123 "inode #%lu logical block %llu mapped to %llu "
1124 "(size %d)", inode->i_ino,
1125 (unsigned long long) logical,
1126 (unsigned long long) phys, len);
1127 WARN_ON(1);
1128 return -EIO;
1129 }
1130 return 0;
1131}
1132
Mingming Caof5ab0d12008-02-25 15:29:55 -05001133/*
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001134 * The ext4_get_blocks() function tries to look up the requested blocks,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001135 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -05001136 *
Mingming Caof5ab0d12008-02-25 15:29:55 -05001137 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
1138 * and store the allocated blocks in the result buffer head and mark it
1139 * mapped.
1140 *
1141 * If file type is extents based, it will call ext4_ext_get_blocks(),
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001142 * Otherwise, call with ext4_ind_get_blocks() to handle indirect mapping
Mingming Caof5ab0d12008-02-25 15:29:55 -05001143 * based files
1144 *
1145 * On success, it returns the number of blocks being mapped or allocate.
1146 * if create==0 and the blocks are pre-allocated and uninitialized block,
1147 * the result buffer head is unmapped. If the create ==1, it will make sure
1148 * the buffer head is mapped.
1149 *
1150 * It returns 0 if plain look up failed (blocks have not been allocated), in
1151 * that casem, buffer head is unmapped
1152 *
1153 * It returns the error in case of allocation failure.
1154 */
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001155int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block,
1156 unsigned int max_blocks, struct buffer_head *bh,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001157 int flags)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001158{
1159 int retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -05001160
1161 clear_buffer_mapped(bh);
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -04001162 clear_buffer_unwritten(bh);
Mingming Caof5ab0d12008-02-25 15:29:55 -05001163
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001164 /*
Theodore Ts'ob920c752009-05-14 00:54:29 -04001165 * Try to see if we can get the block without requesting a new
1166 * file system block.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001167 */
1168 down_read((&EXT4_I(inode)->i_data_sem));
1169 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1170 retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001171 bh, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001172 } else {
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001173 retval = ext4_ind_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001174 bh, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001175 }
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001176 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -05001177
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001178 if (retval > 0 && buffer_mapped(bh)) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001179 int ret = check_block_validity(inode, block,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001180 bh->b_blocknr, retval);
1181 if (ret != 0)
1182 return ret;
1183 }
1184
Mingming Caof5ab0d12008-02-25 15:29:55 -05001185 /* If it is only a block(s) look up */
Theodore Ts'oc2177052009-05-14 00:58:52 -04001186 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001187 return retval;
1188
1189 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001190 * Returns if the blocks have already allocated
1191 *
1192 * Note that if blocks have been preallocated
1193 * ext4_ext_get_block() returns th create = 0
1194 * with buffer head unmapped.
1195 */
1196 if (retval > 0 && buffer_mapped(bh))
1197 return retval;
1198
1199 /*
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -04001200 * When we call get_blocks without the create flag, the
1201 * BH_Unwritten flag could have gotten set if the blocks
1202 * requested were part of a uninitialized extent. We need to
1203 * clear this flag now that we are committed to convert all or
1204 * part of the uninitialized extent to be an initialized
1205 * extent. This is because we need to avoid the combination
1206 * of BH_Unwritten and BH_Mapped flags being simultaneously
1207 * set on the buffer_head.
1208 */
1209 clear_buffer_unwritten(bh);
1210
1211 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001212 * New blocks allocate and/or writing to uninitialized extent
1213 * will possibly result in updating i_data, so we take
1214 * the write lock of i_data_sem, and call get_blocks()
1215 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001216 */
1217 down_write((&EXT4_I(inode)->i_data_sem));
Mingming Caod2a17632008-07-14 17:52:37 -04001218
1219 /*
1220 * if the caller is from delayed allocation writeout path
1221 * we have already reserved fs blocks for allocation
1222 * let the underlying get_block() function know to
1223 * avoid double accounting
1224 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04001225 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Mingming Caod2a17632008-07-14 17:52:37 -04001226 EXT4_I(inode)->i_delalloc_reserved_flag = 1;
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001227 /*
1228 * We need to check for EXT4 here because migrate
1229 * could have changed the inode type in between
1230 */
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001231 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1232 retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001233 bh, flags);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001234 } else {
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001235 retval = ext4_ind_get_blocks(handle, inode, block,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001236 max_blocks, bh, flags);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04001237
1238 if (retval > 0 && buffer_new(bh)) {
1239 /*
1240 * We allocated new blocks which will result in
1241 * i_data's format changing. Force the migrate
1242 * to fail by clearing migrate flags
1243 */
1244 EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags &
1245 ~EXT4_EXT_MIGRATE;
1246 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001247 }
Mingming Caod2a17632008-07-14 17:52:37 -04001248
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001249 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Mingming Caod2a17632008-07-14 17:52:37 -04001250 EXT4_I(inode)->i_delalloc_reserved_flag = 0;
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001251
1252 /*
1253 * Update reserved blocks/metadata blocks after successful
1254 * block allocation which had been deferred till now.
1255 */
1256 if ((retval > 0) && (flags & EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE))
1257 ext4_da_update_reserve_space(inode, retval);
Mingming Caod2a17632008-07-14 17:52:37 -04001258
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001259 up_write((&EXT4_I(inode)->i_data_sem));
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001260 if (retval > 0 && buffer_mapped(bh)) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001261 int ret = check_block_validity(inode, block,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001262 bh->b_blocknr, retval);
1263 if (ret != 0)
1264 return ret;
1265 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001266 return retval;
1267}
1268
Mingming Caof3bd1f32008-08-19 22:16:03 -04001269/* Maximum number of blocks we map for direct IO at once. */
1270#define DIO_MAX_BLOCKS 4096
1271
Eric Sandeen6873fa02008-10-07 00:46:36 -04001272int ext4_get_block(struct inode *inode, sector_t iblock,
1273 struct buffer_head *bh_result, int create)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001274{
Dmitriy Monakhov3e4fdaf2007-02-10 01:46:35 -08001275 handle_t *handle = ext4_journal_current_handle();
Jan Kara7fb54092008-02-10 01:08:38 -05001276 int ret = 0, started = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001277 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001278 int dio_credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001279
Jan Kara7fb54092008-02-10 01:08:38 -05001280 if (create && !handle) {
1281 /* Direct IO write... */
1282 if (max_blocks > DIO_MAX_BLOCKS)
1283 max_blocks = DIO_MAX_BLOCKS;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001284 dio_credits = ext4_chunk_trans_blocks(inode, max_blocks);
1285 handle = ext4_journal_start(inode, dio_credits);
Jan Kara7fb54092008-02-10 01:08:38 -05001286 if (IS_ERR(handle)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001287 ret = PTR_ERR(handle);
Jan Kara7fb54092008-02-10 01:08:38 -05001288 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001289 }
Jan Kara7fb54092008-02-10 01:08:38 -05001290 started = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001291 }
1292
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001293 ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001294 create ? EXT4_GET_BLOCKS_CREATE : 0);
Jan Kara7fb54092008-02-10 01:08:38 -05001295 if (ret > 0) {
1296 bh_result->b_size = (ret << inode->i_blkbits);
1297 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001298 }
Jan Kara7fb54092008-02-10 01:08:38 -05001299 if (started)
1300 ext4_journal_stop(handle);
1301out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001302 return ret;
1303}
1304
1305/*
1306 * `handle' can be NULL if create is zero
1307 */
Mingming Cao617ba132006-10-11 01:20:53 -07001308struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001309 ext4_lblk_t block, int create, int *errp)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001310{
1311 struct buffer_head dummy;
1312 int fatal = 0, err;
Jan Kara03f5d8b2009-06-09 00:17:05 -04001313 int flags = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001314
1315 J_ASSERT(handle != NULL || create == 0);
1316
1317 dummy.b_state = 0;
1318 dummy.b_blocknr = -1000;
1319 buffer_trace_init(&dummy.b_history);
Theodore Ts'oc2177052009-05-14 00:58:52 -04001320 if (create)
1321 flags |= EXT4_GET_BLOCKS_CREATE;
1322 err = ext4_get_blocks(handle, inode, block, 1, &dummy, flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001323 /*
Theodore Ts'oc2177052009-05-14 00:58:52 -04001324 * ext4_get_blocks() returns number of blocks mapped. 0 in
1325 * case of a HOLE.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001326 */
1327 if (err > 0) {
1328 if (err > 1)
1329 WARN_ON(1);
1330 err = 0;
1331 }
1332 *errp = err;
1333 if (!err && buffer_mapped(&dummy)) {
1334 struct buffer_head *bh;
1335 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
1336 if (!bh) {
1337 *errp = -EIO;
1338 goto err;
1339 }
1340 if (buffer_new(&dummy)) {
1341 J_ASSERT(create != 0);
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001342 J_ASSERT(handle != NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001343
1344 /*
1345 * Now that we do not always journal data, we should
1346 * keep in mind whether this should always journal the
1347 * new buffer as metadata. For now, regular file
Mingming Cao617ba132006-10-11 01:20:53 -07001348 * writes use ext4_get_block instead, so it's not a
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001349 * problem.
1350 */
1351 lock_buffer(bh);
1352 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001353 fatal = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001354 if (!fatal && !buffer_uptodate(bh)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001355 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001356 set_buffer_uptodate(bh);
1357 }
1358 unlock_buffer(bh);
Frank Mayhar03901312009-01-07 00:06:22 -05001359 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
1360 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001361 if (!fatal)
1362 fatal = err;
1363 } else {
1364 BUFFER_TRACE(bh, "not a new buffer");
1365 }
1366 if (fatal) {
1367 *errp = fatal;
1368 brelse(bh);
1369 bh = NULL;
1370 }
1371 return bh;
1372 }
1373err:
1374 return NULL;
1375}
1376
Mingming Cao617ba132006-10-11 01:20:53 -07001377struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001378 ext4_lblk_t block, int create, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001379{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001380 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001381
Mingming Cao617ba132006-10-11 01:20:53 -07001382 bh = ext4_getblk(handle, inode, block, create, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001383 if (!bh)
1384 return bh;
1385 if (buffer_uptodate(bh))
1386 return bh;
1387 ll_rw_block(READ_META, 1, &bh);
1388 wait_on_buffer(bh);
1389 if (buffer_uptodate(bh))
1390 return bh;
1391 put_bh(bh);
1392 *err = -EIO;
1393 return NULL;
1394}
1395
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001396static int walk_page_buffers(handle_t *handle,
1397 struct buffer_head *head,
1398 unsigned from,
1399 unsigned to,
1400 int *partial,
1401 int (*fn)(handle_t *handle,
1402 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001403{
1404 struct buffer_head *bh;
1405 unsigned block_start, block_end;
1406 unsigned blocksize = head->b_size;
1407 int err, ret = 0;
1408 struct buffer_head *next;
1409
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001410 for (bh = head, block_start = 0;
1411 ret == 0 && (bh != head || !block_start);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001412 block_start = block_end, bh = next) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001413 next = bh->b_this_page;
1414 block_end = block_start + blocksize;
1415 if (block_end <= from || block_start >= to) {
1416 if (partial && !buffer_uptodate(bh))
1417 *partial = 1;
1418 continue;
1419 }
1420 err = (*fn)(handle, bh);
1421 if (!ret)
1422 ret = err;
1423 }
1424 return ret;
1425}
1426
1427/*
1428 * To preserve ordering, it is essential that the hole instantiation and
1429 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -07001430 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -07001431 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001432 * prepare_write() is the right place.
1433 *
Mingming Cao617ba132006-10-11 01:20:53 -07001434 * Also, this function can nest inside ext4_writepage() ->
1435 * block_write_full_page(). In that case, we *know* that ext4_writepage()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001436 * has generated enough buffer credits to do the whole page. So we won't
1437 * block on the journal in that case, which is good, because the caller may
1438 * be PF_MEMALLOC.
1439 *
Mingming Cao617ba132006-10-11 01:20:53 -07001440 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001441 * quota file writes. If we were to commit the transaction while thus
1442 * reentered, there can be a deadlock - we would be holding a quota
1443 * lock, and the commit would never complete if another thread had a
1444 * transaction open and was blocking on the quota lock - a ranking
1445 * violation.
1446 *
Mingming Caodab291a2006-10-11 01:21:01 -07001447 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001448 * will _not_ run commit under these circumstances because handle->h_ref
1449 * is elevated. We'll still have enough credits for the tiny quotafile
1450 * write.
1451 */
1452static int do_journal_get_write_access(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001453 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001454{
1455 if (!buffer_mapped(bh) || buffer_freed(bh))
1456 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001457 return ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001458}
1459
Nick Pigginbfc1af62007-10-16 01:25:05 -07001460static int ext4_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001461 loff_t pos, unsigned len, unsigned flags,
1462 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001463{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001464 struct inode *inode = mapping->host;
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001465 int ret, needed_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001466 handle_t *handle;
1467 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001468 struct page *page;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001469 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001470 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001471
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001472 trace_ext4_write_begin(inode, pos, len, flags);
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001473 /*
1474 * Reserve one block more for addition to orphan list in case
1475 * we allocate blocks but write fails for some reason
1476 */
1477 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001478 index = pos >> PAGE_CACHE_SHIFT;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001479 from = pos & (PAGE_CACHE_SIZE - 1);
1480 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001481
1482retry:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001483 handle = ext4_journal_start(inode, needed_blocks);
1484 if (IS_ERR(handle)) {
1485 ret = PTR_ERR(handle);
1486 goto out;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001487 }
1488
Jan Karaebd36102009-02-22 21:09:59 -05001489 /* We cannot recurse into the filesystem as the transaction is already
1490 * started */
1491 flags |= AOP_FLAG_NOFS;
1492
Nick Piggin54566b22009-01-04 12:00:53 -08001493 page = grab_cache_page_write_begin(mapping, index, flags);
Jan Karacf108bc2008-07-11 19:27:31 -04001494 if (!page) {
1495 ext4_journal_stop(handle);
1496 ret = -ENOMEM;
1497 goto out;
1498 }
1499 *pagep = page;
1500
Nick Pigginbfc1af62007-10-16 01:25:05 -07001501 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
Jan Karaebd36102009-02-22 21:09:59 -05001502 ext4_get_block);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001503
1504 if (!ret && ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001505 ret = walk_page_buffers(handle, page_buffers(page),
1506 from, to, NULL, do_journal_get_write_access);
1507 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001508
1509 if (ret) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001510 unlock_page(page);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001511 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001512 /*
1513 * block_write_begin may have instantiated a few blocks
1514 * outside i_size. Trim these off again. Don't need
1515 * i_size_read because we hold i_mutex.
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001516 *
1517 * Add inode to orphan list in case we crash before
1518 * truncate finishes
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001519 */
Jan Karaffacfa72009-07-13 16:22:22 -04001520 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001521 ext4_orphan_add(handle, inode);
1522
1523 ext4_journal_stop(handle);
1524 if (pos + len > inode->i_size) {
Jan Karaffacfa72009-07-13 16:22:22 -04001525 ext4_truncate(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001526 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001527 * If truncate failed early the inode might
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001528 * still be on the orphan list; we need to
1529 * make sure the inode is removed from the
1530 * orphan list in that case.
1531 */
1532 if (inode->i_nlink)
1533 ext4_orphan_del(NULL, inode);
1534 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001535 }
1536
Mingming Cao617ba132006-10-11 01:20:53 -07001537 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001538 goto retry;
Andrew Morton7479d2b2007-04-01 23:49:44 -07001539out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001540 return ret;
1541}
1542
Nick Pigginbfc1af62007-10-16 01:25:05 -07001543/* For write_end() in data=journal mode */
1544static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001545{
1546 if (!buffer_mapped(bh) || buffer_freed(bh))
1547 return 0;
1548 set_buffer_uptodate(bh);
Frank Mayhar03901312009-01-07 00:06:22 -05001549 return ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001550}
1551
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001552static int ext4_generic_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001553 struct address_space *mapping,
1554 loff_t pos, unsigned len, unsigned copied,
1555 struct page *page, void *fsdata)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001556{
1557 int i_size_changed = 0;
1558 struct inode *inode = mapping->host;
1559 handle_t *handle = ext4_journal_current_handle();
1560
1561 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
1562
1563 /*
1564 * No need to use i_size_read() here, the i_size
1565 * cannot change under us because we hold i_mutex.
1566 *
1567 * But it's important to update i_size while still holding page lock:
1568 * page writeout could otherwise come in and zero beyond i_size.
1569 */
1570 if (pos + copied > inode->i_size) {
1571 i_size_write(inode, pos + copied);
1572 i_size_changed = 1;
1573 }
1574
1575 if (pos + copied > EXT4_I(inode)->i_disksize) {
1576 /* We need to mark inode dirty even if
1577 * new_i_size is less that inode->i_size
1578 * bu greater than i_disksize.(hint delalloc)
1579 */
1580 ext4_update_i_disksize(inode, (pos + copied));
1581 i_size_changed = 1;
1582 }
1583 unlock_page(page);
1584 page_cache_release(page);
1585
1586 /*
1587 * Don't mark the inode dirty under page lock. First, it unnecessarily
1588 * makes the holding time of page lock longer. Second, it forces lock
1589 * ordering of page lock and transaction start for journaling
1590 * filesystems.
1591 */
1592 if (i_size_changed)
1593 ext4_mark_inode_dirty(handle, inode);
1594
1595 return copied;
1596}
1597
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001598/*
1599 * We need to pick up the new inode size which generic_commit_write gave us
1600 * `file' can be NULL - eg, when called from page_symlink().
1601 *
Mingming Cao617ba132006-10-11 01:20:53 -07001602 * ext4 never places buffers on inode->i_mapping->private_list. metadata
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001603 * buffers are managed internally.
1604 */
Nick Pigginbfc1af62007-10-16 01:25:05 -07001605static int ext4_ordered_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001606 struct address_space *mapping,
1607 loff_t pos, unsigned len, unsigned copied,
1608 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001609{
Mingming Cao617ba132006-10-11 01:20:53 -07001610 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001611 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001612 int ret = 0, ret2;
1613
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001614 trace_ext4_ordered_write_end(inode, pos, len, copied);
Jan Kara678aaf42008-07-11 19:27:31 -04001615 ret = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001616
1617 if (ret == 0) {
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001618 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001619 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001620 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -04001621 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001622 /* if we have allocated more blocks and copied
1623 * less. We will have blocks allocated outside
1624 * inode->i_size. So truncate them
1625 */
1626 ext4_orphan_add(handle, inode);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001627 if (ret2 < 0)
1628 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001629 }
Mingming Cao617ba132006-10-11 01:20:53 -07001630 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001631 if (!ret)
1632 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001633
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001634 if (pos + len > inode->i_size) {
Jan Karaffacfa72009-07-13 16:22:22 -04001635 ext4_truncate(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001636 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001637 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001638 * on the orphan list; we need to make sure the inode
1639 * is removed from the orphan list in that case.
1640 */
1641 if (inode->i_nlink)
1642 ext4_orphan_del(NULL, inode);
1643 }
1644
1645
Nick Pigginbfc1af62007-10-16 01:25:05 -07001646 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001647}
1648
Nick Pigginbfc1af62007-10-16 01:25:05 -07001649static int ext4_writeback_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001650 struct address_space *mapping,
1651 loff_t pos, unsigned len, unsigned copied,
1652 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001653{
Mingming Cao617ba132006-10-11 01:20:53 -07001654 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001655 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001656 int ret = 0, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001657
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001658 trace_ext4_writeback_write_end(inode, pos, len, copied);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001659 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001660 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001661 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -04001662 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001663 /* if we have allocated more blocks and copied
1664 * less. We will have blocks allocated outside
1665 * inode->i_size. So truncate them
1666 */
1667 ext4_orphan_add(handle, inode);
1668
Roel Kluinf8a87d82008-04-29 22:01:18 -04001669 if (ret2 < 0)
1670 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001671
Mingming Cao617ba132006-10-11 01:20:53 -07001672 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001673 if (!ret)
1674 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001675
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001676 if (pos + len > inode->i_size) {
Jan Karaffacfa72009-07-13 16:22:22 -04001677 ext4_truncate(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001678 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001679 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001680 * on the orphan list; we need to make sure the inode
1681 * is removed from the orphan list in that case.
1682 */
1683 if (inode->i_nlink)
1684 ext4_orphan_del(NULL, inode);
1685 }
1686
Nick Pigginbfc1af62007-10-16 01:25:05 -07001687 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001688}
1689
Nick Pigginbfc1af62007-10-16 01:25:05 -07001690static int ext4_journalled_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001691 struct address_space *mapping,
1692 loff_t pos, unsigned len, unsigned copied,
1693 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001694{
Mingming Cao617ba132006-10-11 01:20:53 -07001695 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001696 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001697 int ret = 0, ret2;
1698 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001699 unsigned from, to;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001700 loff_t new_i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001701
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001702 trace_ext4_journalled_write_end(inode, pos, len, copied);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001703 from = pos & (PAGE_CACHE_SIZE - 1);
1704 to = from + len;
1705
1706 if (copied < len) {
1707 if (!PageUptodate(page))
1708 copied = 0;
1709 page_zero_new_buffers(page, from+copied, to);
1710 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001711
1712 ret = walk_page_buffers(handle, page_buffers(page), from,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001713 to, &partial, write_end_fn);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001714 if (!partial)
1715 SetPageUptodate(page);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001716 new_i_size = pos + copied;
1717 if (new_i_size > inode->i_size)
Nick Pigginbfc1af62007-10-16 01:25:05 -07001718 i_size_write(inode, pos+copied);
Mingming Cao617ba132006-10-11 01:20:53 -07001719 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001720 if (new_i_size > EXT4_I(inode)->i_disksize) {
1721 ext4_update_i_disksize(inode, new_i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001722 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001723 if (!ret)
1724 ret = ret2;
1725 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001726
Jan Karacf108bc2008-07-11 19:27:31 -04001727 unlock_page(page);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001728 page_cache_release(page);
Jan Karaffacfa72009-07-13 16:22:22 -04001729 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001730 /* if we have allocated more blocks and copied
1731 * less. We will have blocks allocated outside
1732 * inode->i_size. So truncate them
1733 */
1734 ext4_orphan_add(handle, inode);
1735
Mingming Cao617ba132006-10-11 01:20:53 -07001736 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001737 if (!ret)
1738 ret = ret2;
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001739 if (pos + len > inode->i_size) {
Jan Karaffacfa72009-07-13 16:22:22 -04001740 ext4_truncate(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001741 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001742 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001743 * on the orphan list; we need to make sure the inode
1744 * is removed from the orphan list in that case.
1745 */
1746 if (inode->i_nlink)
1747 ext4_orphan_del(NULL, inode);
1748 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001749
1750 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001751}
Mingming Caod2a17632008-07-14 17:52:37 -04001752
1753static int ext4_da_reserve_space(struct inode *inode, int nrblocks)
1754{
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001755 int retries = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01001756 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1757 unsigned long md_needed, mdblocks, total = 0;
Mingming Caod2a17632008-07-14 17:52:37 -04001758
1759 /*
1760 * recalculate the amount of metadata blocks to reserve
1761 * in order to allocate nrblocks
1762 * worse case is one extent per block
1763 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001764repeat:
Mingming Caod2a17632008-07-14 17:52:37 -04001765 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1766 total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks;
1767 mdblocks = ext4_calc_metadata_amount(inode, total);
1768 BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks);
1769
1770 md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks;
1771 total = md_needed + nrblocks;
1772
Mingming Cao60e58e02009-01-22 18:13:05 +01001773 /*
1774 * Make quota reservation here to prevent quota overflow
1775 * later. Real quota accounting is done at pages writeout
1776 * time.
1777 */
1778 if (vfs_dq_reserve_block(inode, total)) {
1779 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1780 return -EDQUOT;
1781 }
1782
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04001783 if (ext4_claim_free_blocks(sbi, total)) {
Mingming Caod2a17632008-07-14 17:52:37 -04001784 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001785 if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1786 yield();
1787 goto repeat;
1788 }
Mingming Cao60e58e02009-01-22 18:13:05 +01001789 vfs_dq_release_reservation_block(inode, total);
Mingming Caod2a17632008-07-14 17:52:37 -04001790 return -ENOSPC;
1791 }
Mingming Caod2a17632008-07-14 17:52:37 -04001792 EXT4_I(inode)->i_reserved_data_blocks += nrblocks;
1793 EXT4_I(inode)->i_reserved_meta_blocks = mdblocks;
1794
1795 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1796 return 0; /* success */
1797}
1798
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001799static void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001800{
1801 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1802 int total, mdb, mdb_free, release;
1803
Mingming Caocd213222008-08-19 22:16:59 -04001804 if (!to_free)
1805 return; /* Nothing to release, exit */
1806
Mingming Caod2a17632008-07-14 17:52:37 -04001807 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001808
1809 if (!EXT4_I(inode)->i_reserved_data_blocks) {
1810 /*
1811 * if there is no reserved blocks, but we try to free some
1812 * then the counter is messed up somewhere.
1813 * but since this function is called from invalidate
1814 * page, it's harmless to return without any action
1815 */
1816 printk(KERN_INFO "ext4 delalloc try to release %d reserved "
1817 "blocks for inode %lu, but there is no reserved "
1818 "data blocks\n", to_free, inode->i_ino);
1819 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1820 return;
1821 }
1822
Mingming Caod2a17632008-07-14 17:52:37 -04001823 /* recalculate the number of metablocks still need to be reserved */
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001824 total = EXT4_I(inode)->i_reserved_data_blocks - to_free;
Mingming Caod2a17632008-07-14 17:52:37 -04001825 mdb = ext4_calc_metadata_amount(inode, total);
1826
1827 /* figure out how many metablocks to release */
1828 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1829 mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
1830
Mingming Caod2a17632008-07-14 17:52:37 -04001831 release = to_free + mdb_free;
1832
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001833 /* update fs dirty blocks counter for truncate case */
1834 percpu_counter_sub(&sbi->s_dirtyblocks_counter, release);
Mingming Caod2a17632008-07-14 17:52:37 -04001835
1836 /* update per-inode reservations */
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001837 BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks);
1838 EXT4_I(inode)->i_reserved_data_blocks -= to_free;
Mingming Caod2a17632008-07-14 17:52:37 -04001839
1840 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1841 EXT4_I(inode)->i_reserved_meta_blocks = mdb;
Mingming Caod2a17632008-07-14 17:52:37 -04001842 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001843
1844 vfs_dq_release_reservation_block(inode, release);
Mingming Caod2a17632008-07-14 17:52:37 -04001845}
1846
1847static void ext4_da_page_release_reservation(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001848 unsigned long offset)
Mingming Caod2a17632008-07-14 17:52:37 -04001849{
1850 int to_release = 0;
1851 struct buffer_head *head, *bh;
1852 unsigned int curr_off = 0;
1853
1854 head = page_buffers(page);
1855 bh = head;
1856 do {
1857 unsigned int next_off = curr_off + bh->b_size;
1858
1859 if ((offset <= curr_off) && (buffer_delay(bh))) {
1860 to_release++;
1861 clear_buffer_delay(bh);
1862 }
1863 curr_off = next_off;
1864 } while ((bh = bh->b_this_page) != head);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001865 ext4_da_release_space(page->mapping->host, to_release);
Mingming Caod2a17632008-07-14 17:52:37 -04001866}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001867
1868/*
Alex Tomas64769242008-07-11 19:27:31 -04001869 * Delayed allocation stuff
1870 */
1871
1872struct mpage_da_data {
1873 struct inode *inode;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001874 sector_t b_blocknr; /* start block number of extent */
1875 size_t b_size; /* size of extent */
1876 unsigned long b_state; /* state of the extent */
Alex Tomas64769242008-07-11 19:27:31 -04001877 unsigned long first_page, next_page; /* extent of pages */
Alex Tomas64769242008-07-11 19:27:31 -04001878 struct writeback_control *wbc;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001879 int io_done;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001880 int pages_written;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001881 int retval;
Alex Tomas64769242008-07-11 19:27:31 -04001882};
1883
1884/*
1885 * mpage_da_submit_io - walks through extent of pages and try to write
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001886 * them with writepage() call back
Alex Tomas64769242008-07-11 19:27:31 -04001887 *
1888 * @mpd->inode: inode
1889 * @mpd->first_page: first page of the extent
1890 * @mpd->next_page: page after the last page of the extent
Alex Tomas64769242008-07-11 19:27:31 -04001891 *
1892 * By the time mpage_da_submit_io() is called we expect all blocks
1893 * to be allocated. this may be wrong if allocation failed.
1894 *
1895 * As pages are already locked by write_cache_pages(), we can't use it
1896 */
1897static int mpage_da_submit_io(struct mpage_da_data *mpd)
1898{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001899 long pages_skipped;
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001900 struct pagevec pvec;
1901 unsigned long index, end;
1902 int ret = 0, err, nr_pages, i;
1903 struct inode *inode = mpd->inode;
1904 struct address_space *mapping = inode->i_mapping;
Alex Tomas64769242008-07-11 19:27:31 -04001905
1906 BUG_ON(mpd->next_page <= mpd->first_page);
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001907 /*
1908 * We need to start from the first_page to the next_page - 1
1909 * to make sure we also write the mapped dirty buffer_heads.
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001910 * If we look at mpd->b_blocknr we would only be looking
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001911 * at the currently mapped buffer_heads.
1912 */
Alex Tomas64769242008-07-11 19:27:31 -04001913 index = mpd->first_page;
1914 end = mpd->next_page - 1;
1915
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001916 pagevec_init(&pvec, 0);
Alex Tomas64769242008-07-11 19:27:31 -04001917 while (index <= end) {
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001918 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
Alex Tomas64769242008-07-11 19:27:31 -04001919 if (nr_pages == 0)
1920 break;
1921 for (i = 0; i < nr_pages; i++) {
1922 struct page *page = pvec.pages[i];
1923
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001924 index = page->index;
1925 if (index > end)
1926 break;
1927 index++;
1928
1929 BUG_ON(!PageLocked(page));
1930 BUG_ON(PageWriteback(page));
1931
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001932 pages_skipped = mpd->wbc->pages_skipped;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001933 err = mapping->a_ops->writepage(page, mpd->wbc);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001934 if (!err && (pages_skipped == mpd->wbc->pages_skipped))
1935 /*
1936 * have successfully written the page
1937 * without skipping the same
1938 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001939 mpd->pages_written++;
Alex Tomas64769242008-07-11 19:27:31 -04001940 /*
1941 * In error case, we have to continue because
1942 * remaining pages are still locked
1943 * XXX: unlock and re-dirty them?
1944 */
1945 if (ret == 0)
1946 ret = err;
1947 }
1948 pagevec_release(&pvec);
1949 }
Alex Tomas64769242008-07-11 19:27:31 -04001950 return ret;
1951}
1952
1953/*
1954 * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers
1955 *
1956 * @mpd->inode - inode to walk through
1957 * @exbh->b_blocknr - first block on a disk
1958 * @exbh->b_size - amount of space in bytes
1959 * @logical - first logical block to start assignment with
1960 *
1961 * the function goes through all passed space and put actual disk
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001962 * block numbers into buffer heads, dropping BH_Delay and BH_Unwritten
Alex Tomas64769242008-07-11 19:27:31 -04001963 */
1964static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical,
1965 struct buffer_head *exbh)
1966{
1967 struct inode *inode = mpd->inode;
1968 struct address_space *mapping = inode->i_mapping;
1969 int blocks = exbh->b_size >> inode->i_blkbits;
1970 sector_t pblock = exbh->b_blocknr, cur_logical;
1971 struct buffer_head *head, *bh;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001972 pgoff_t index, end;
Alex Tomas64769242008-07-11 19:27:31 -04001973 struct pagevec pvec;
1974 int nr_pages, i;
1975
1976 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
1977 end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
1978 cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1979
1980 pagevec_init(&pvec, 0);
1981
1982 while (index <= end) {
1983 /* XXX: optimize tail */
1984 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1985 if (nr_pages == 0)
1986 break;
1987 for (i = 0; i < nr_pages; i++) {
1988 struct page *page = pvec.pages[i];
1989
1990 index = page->index;
1991 if (index > end)
1992 break;
1993 index++;
1994
1995 BUG_ON(!PageLocked(page));
1996 BUG_ON(PageWriteback(page));
1997 BUG_ON(!page_has_buffers(page));
1998
1999 bh = page_buffers(page);
2000 head = bh;
2001
2002 /* skip blocks out of the range */
2003 do {
2004 if (cur_logical >= logical)
2005 break;
2006 cur_logical++;
2007 } while ((bh = bh->b_this_page) != head);
2008
2009 do {
2010 if (cur_logical >= logical + blocks)
2011 break;
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002012
2013 if (buffer_delay(bh) ||
2014 buffer_unwritten(bh)) {
2015
2016 BUG_ON(bh->b_bdev != inode->i_sb->s_bdev);
2017
2018 if (buffer_delay(bh)) {
2019 clear_buffer_delay(bh);
2020 bh->b_blocknr = pblock;
2021 } else {
2022 /*
2023 * unwritten already should have
2024 * blocknr assigned. Verify that
2025 */
2026 clear_buffer_unwritten(bh);
2027 BUG_ON(bh->b_blocknr != pblock);
2028 }
2029
Mingming Cao61628a32008-07-11 19:27:31 -04002030 } else if (buffer_mapped(bh))
Alex Tomas64769242008-07-11 19:27:31 -04002031 BUG_ON(bh->b_blocknr != pblock);
Alex Tomas64769242008-07-11 19:27:31 -04002032
2033 cur_logical++;
2034 pblock++;
2035 } while ((bh = bh->b_this_page) != head);
2036 }
2037 pagevec_release(&pvec);
2038 }
2039}
2040
2041
2042/*
2043 * __unmap_underlying_blocks - just a helper function to unmap
2044 * set of blocks described by @bh
2045 */
2046static inline void __unmap_underlying_blocks(struct inode *inode,
2047 struct buffer_head *bh)
2048{
2049 struct block_device *bdev = inode->i_sb->s_bdev;
2050 int blocks, i;
2051
2052 blocks = bh->b_size >> inode->i_blkbits;
2053 for (i = 0; i < blocks; i++)
2054 unmap_underlying_metadata(bdev, bh->b_blocknr + i);
2055}
2056
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002057static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd,
2058 sector_t logical, long blk_cnt)
2059{
2060 int nr_pages, i;
2061 pgoff_t index, end;
2062 struct pagevec pvec;
2063 struct inode *inode = mpd->inode;
2064 struct address_space *mapping = inode->i_mapping;
2065
2066 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
2067 end = (logical + blk_cnt - 1) >>
2068 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2069 while (index <= end) {
2070 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
2071 if (nr_pages == 0)
2072 break;
2073 for (i = 0; i < nr_pages; i++) {
2074 struct page *page = pvec.pages[i];
2075 index = page->index;
2076 if (index > end)
2077 break;
2078 index++;
2079
2080 BUG_ON(!PageLocked(page));
2081 BUG_ON(PageWriteback(page));
2082 block_invalidatepage(page, 0);
2083 ClearPageUptodate(page);
2084 unlock_page(page);
2085 }
2086 }
2087 return;
2088}
2089
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002090static void ext4_print_free_blocks(struct inode *inode)
2091{
2092 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2093 printk(KERN_EMERG "Total free blocks count %lld\n",
2094 ext4_count_free_blocks(inode->i_sb));
2095 printk(KERN_EMERG "Free/Dirty block details\n");
2096 printk(KERN_EMERG "free_blocks=%lld\n",
Alexander Beregalov8f72fbd2008-10-29 17:13:08 -04002097 (long long)percpu_counter_sum(&sbi->s_freeblocks_counter));
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002098 printk(KERN_EMERG "dirty_blocks=%lld\n",
Alexander Beregalov8f72fbd2008-10-29 17:13:08 -04002099 (long long)percpu_counter_sum(&sbi->s_dirtyblocks_counter));
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002100 printk(KERN_EMERG "Block reservation details\n");
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002101 printk(KERN_EMERG "i_reserved_data_blocks=%u\n",
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002102 EXT4_I(inode)->i_reserved_data_blocks);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002103 printk(KERN_EMERG "i_reserved_meta_blocks=%u\n",
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002104 EXT4_I(inode)->i_reserved_meta_blocks);
2105 return;
2106}
2107
Theodore Ts'ob920c752009-05-14 00:54:29 -04002108/*
Alex Tomas64769242008-07-11 19:27:31 -04002109 * mpage_da_map_blocks - go through given space
2110 *
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002111 * @mpd - bh describing space
Alex Tomas64769242008-07-11 19:27:31 -04002112 *
2113 * The function skips space we know is already mapped to disk blocks.
2114 *
Alex Tomas64769242008-07-11 19:27:31 -04002115 */
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002116static int mpage_da_map_blocks(struct mpage_da_data *mpd)
Alex Tomas64769242008-07-11 19:27:31 -04002117{
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002118 int err, blks, get_blocks_flags;
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002119 struct buffer_head new;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002120 sector_t next = mpd->b_blocknr;
2121 unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
2122 loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
2123 handle_t *handle = NULL;
Alex Tomas64769242008-07-11 19:27:31 -04002124
2125 /*
2126 * We consider only non-mapped and non-allocated blocks
2127 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002128 if ((mpd->b_state & (1 << BH_Mapped)) &&
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002129 !(mpd->b_state & (1 << BH_Delay)) &&
2130 !(mpd->b_state & (1 << BH_Unwritten)))
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002131 return 0;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002132
2133 /*
2134 * If we didn't accumulate anything to write simply return
2135 */
2136 if (!mpd->b_size)
2137 return 0;
2138
2139 handle = ext4_journal_current_handle();
2140 BUG_ON(!handle);
2141
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04002142 /*
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002143 * Call ext4_get_blocks() to allocate any delayed allocation
2144 * blocks, or to convert an uninitialized extent to be
2145 * initialized (in the case where we have written into
2146 * one or more preallocated blocks).
2147 *
2148 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
2149 * indicate that we are on the delayed allocation path. This
2150 * affects functions in many different parts of the allocation
2151 * call path. This flag exists primarily because we don't
2152 * want to change *many* call functions, so ext4_get_blocks()
2153 * will set the magic i_delalloc_reserved_flag once the
2154 * inode's allocation semaphore is taken.
2155 *
2156 * If the blocks in questions were delalloc blocks, set
2157 * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
2158 * variables are updated after the blocks have been allocated.
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04002159 */
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002160 new.b_state = 0;
2161 get_blocks_flags = (EXT4_GET_BLOCKS_CREATE |
2162 EXT4_GET_BLOCKS_DELALLOC_RESERVE);
2163 if (mpd->b_state & (1 << BH_Delay))
2164 get_blocks_flags |= EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002165 blks = ext4_get_blocks(handle, mpd->inode, next, max_blocks,
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002166 &new, get_blocks_flags);
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002167 if (blks < 0) {
2168 err = blks;
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002169 /*
2170 * If get block returns with error we simply
2171 * return. Later writepage will redirty the page and
2172 * writepages will find the dirty page again
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002173 */
2174 if (err == -EAGAIN)
2175 return 0;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002176
2177 if (err == -ENOSPC &&
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002178 ext4_count_free_blocks(mpd->inode->i_sb)) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002179 mpd->retval = err;
2180 return 0;
2181 }
2182
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002183 /*
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002184 * get block failure will cause us to loop in
2185 * writepages, because a_ops->writepage won't be able
2186 * to make progress. The page will be redirtied by
2187 * writepage and writepages will again try to write
2188 * the same.
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002189 */
2190 printk(KERN_EMERG "%s block allocation failed for inode %lu "
2191 "at logical offset %llu with max blocks "
2192 "%zd with error %d\n",
2193 __func__, mpd->inode->i_ino,
2194 (unsigned long long)next,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002195 mpd->b_size >> mpd->inode->i_blkbits, err);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002196 printk(KERN_EMERG "This should not happen.!! "
2197 "Data will be lost\n");
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002198 if (err == -ENOSPC) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002199 ext4_print_free_blocks(mpd->inode);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002200 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002201 /* invalidate all the pages */
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002202 ext4_da_block_invalidatepages(mpd, next,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002203 mpd->b_size >> mpd->inode->i_blkbits);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002204 return err;
2205 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002206 BUG_ON(blks == 0);
2207
2208 new.b_size = (blks << mpd->inode->i_blkbits);
Alex Tomas64769242008-07-11 19:27:31 -04002209
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002210 if (buffer_new(&new))
2211 __unmap_underlying_blocks(mpd->inode, &new);
Alex Tomas64769242008-07-11 19:27:31 -04002212
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002213 /*
2214 * If blocks are delayed marked, we need to
2215 * put actual blocknr and drop delayed bit
2216 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002217 if ((mpd->b_state & (1 << BH_Delay)) ||
2218 (mpd->b_state & (1 << BH_Unwritten)))
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002219 mpage_put_bnr_to_bhs(mpd, next, &new);
2220
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002221 if (ext4_should_order_data(mpd->inode)) {
2222 err = ext4_jbd2_file_inode(handle, mpd->inode);
2223 if (err)
2224 return err;
2225 }
2226
2227 /*
Jan Kara03f5d8b2009-06-09 00:17:05 -04002228 * Update on-disk size along with block allocation.
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002229 */
2230 disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
2231 if (disksize > i_size_read(mpd->inode))
2232 disksize = i_size_read(mpd->inode);
2233 if (disksize > EXT4_I(mpd->inode)->i_disksize) {
2234 ext4_update_i_disksize(mpd->inode, disksize);
2235 return ext4_mark_inode_dirty(handle, mpd->inode);
2236 }
2237
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002238 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04002239}
2240
Aneesh Kumar K.Vbf068ee2008-08-19 22:16:43 -04002241#define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
2242 (1 << BH_Delay) | (1 << BH_Unwritten))
Alex Tomas64769242008-07-11 19:27:31 -04002243
2244/*
2245 * mpage_add_bh_to_extent - try to add one more block to extent of blocks
2246 *
2247 * @mpd->lbh - extent of blocks
2248 * @logical - logical number of the block in the file
2249 * @bh - bh of the block (used to access block's state)
2250 *
2251 * the function is used to collect contig. blocks in same state
2252 */
2253static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002254 sector_t logical, size_t b_size,
2255 unsigned long b_state)
Alex Tomas64769242008-07-11 19:27:31 -04002256{
Alex Tomas64769242008-07-11 19:27:31 -04002257 sector_t next;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002258 int nrblocks = mpd->b_size >> mpd->inode->i_blkbits;
Alex Tomas64769242008-07-11 19:27:31 -04002259
Mingming Cao525f4ed2008-08-19 22:15:58 -04002260 /* check if thereserved journal credits might overflow */
2261 if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) {
2262 if (nrblocks >= EXT4_MAX_TRANS_DATA) {
2263 /*
2264 * With non-extent format we are limited by the journal
2265 * credit available. Total credit needed to insert
2266 * nrblocks contiguous blocks is dependent on the
2267 * nrblocks. So limit nrblocks.
2268 */
2269 goto flush_it;
2270 } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
2271 EXT4_MAX_TRANS_DATA) {
2272 /*
2273 * Adding the new buffer_head would make it cross the
2274 * allowed limit for which we have journal credit
2275 * reserved. So limit the new bh->b_size
2276 */
2277 b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
2278 mpd->inode->i_blkbits;
2279 /* we will do mpage_da_submit_io in the next loop */
2280 }
2281 }
Alex Tomas64769242008-07-11 19:27:31 -04002282 /*
2283 * First block in the extent
2284 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002285 if (mpd->b_size == 0) {
2286 mpd->b_blocknr = logical;
2287 mpd->b_size = b_size;
2288 mpd->b_state = b_state & BH_FLAGS;
Alex Tomas64769242008-07-11 19:27:31 -04002289 return;
2290 }
2291
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002292 next = mpd->b_blocknr + nrblocks;
Alex Tomas64769242008-07-11 19:27:31 -04002293 /*
2294 * Can we merge the block to our big extent?
2295 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002296 if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
2297 mpd->b_size += b_size;
Alex Tomas64769242008-07-11 19:27:31 -04002298 return;
2299 }
2300
Mingming Cao525f4ed2008-08-19 22:15:58 -04002301flush_it:
Alex Tomas64769242008-07-11 19:27:31 -04002302 /*
2303 * We couldn't merge the block to our extent, so we
2304 * need to flush current extent and start new one
2305 */
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002306 if (mpage_da_map_blocks(mpd) == 0)
2307 mpage_da_submit_io(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002308 mpd->io_done = 1;
2309 return;
Alex Tomas64769242008-07-11 19:27:31 -04002310}
2311
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002312static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002313{
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002314 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002315}
2316
Alex Tomas64769242008-07-11 19:27:31 -04002317/*
2318 * __mpage_da_writepage - finds extent of pages and blocks
2319 *
2320 * @page: page to consider
2321 * @wbc: not used, we just follow rules
2322 * @data: context
2323 *
2324 * The function finds extents of pages and scan them for all blocks.
2325 */
2326static int __mpage_da_writepage(struct page *page,
2327 struct writeback_control *wbc, void *data)
2328{
2329 struct mpage_da_data *mpd = data;
2330 struct inode *inode = mpd->inode;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002331 struct buffer_head *bh, *head;
Alex Tomas64769242008-07-11 19:27:31 -04002332 sector_t logical;
2333
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002334 if (mpd->io_done) {
2335 /*
2336 * Rest of the page in the page_vec
2337 * redirty then and skip then. We will
2338 * try to to write them again after
2339 * starting a new transaction
2340 */
2341 redirty_page_for_writepage(wbc, page);
2342 unlock_page(page);
2343 return MPAGE_DA_EXTENT_TAIL;
2344 }
Alex Tomas64769242008-07-11 19:27:31 -04002345 /*
2346 * Can we merge this page to current extent?
2347 */
2348 if (mpd->next_page != page->index) {
2349 /*
2350 * Nope, we can't. So, we map non-allocated blocks
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002351 * and start IO on them using writepage()
Alex Tomas64769242008-07-11 19:27:31 -04002352 */
2353 if (mpd->next_page != mpd->first_page) {
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002354 if (mpage_da_map_blocks(mpd) == 0)
2355 mpage_da_submit_io(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002356 /*
2357 * skip rest of the page in the page_vec
2358 */
2359 mpd->io_done = 1;
2360 redirty_page_for_writepage(wbc, page);
2361 unlock_page(page);
2362 return MPAGE_DA_EXTENT_TAIL;
Alex Tomas64769242008-07-11 19:27:31 -04002363 }
2364
2365 /*
2366 * Start next extent of pages ...
2367 */
2368 mpd->first_page = page->index;
2369
2370 /*
2371 * ... and blocks
2372 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002373 mpd->b_size = 0;
2374 mpd->b_state = 0;
2375 mpd->b_blocknr = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002376 }
2377
2378 mpd->next_page = page->index + 1;
2379 logical = (sector_t) page->index <<
2380 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2381
2382 if (!page_has_buffers(page)) {
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002383 mpage_add_bh_to_extent(mpd, logical, PAGE_CACHE_SIZE,
2384 (1 << BH_Dirty) | (1 << BH_Uptodate));
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002385 if (mpd->io_done)
2386 return MPAGE_DA_EXTENT_TAIL;
Alex Tomas64769242008-07-11 19:27:31 -04002387 } else {
2388 /*
2389 * Page with regular buffer heads, just add all dirty ones
2390 */
2391 head = page_buffers(page);
2392 bh = head;
2393 do {
2394 BUG_ON(buffer_locked(bh));
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002395 /*
2396 * We need to try to allocate
2397 * unmapped blocks in the same page.
2398 * Otherwise we won't make progress
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002399 * with the page in ext4_writepage
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002400 */
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002401 if (ext4_bh_delay_or_unwritten(NULL, bh)) {
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002402 mpage_add_bh_to_extent(mpd, logical,
2403 bh->b_size,
2404 bh->b_state);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002405 if (mpd->io_done)
2406 return MPAGE_DA_EXTENT_TAIL;
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002407 } else if (buffer_dirty(bh) && (buffer_mapped(bh))) {
2408 /*
2409 * mapped dirty buffer. We need to update
2410 * the b_state because we look at
2411 * b_state in mpage_da_map_blocks. We don't
2412 * update b_size because if we find an
2413 * unmapped buffer_head later we need to
2414 * use the b_state flag of that buffer_head.
2415 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002416 if (mpd->b_size == 0)
2417 mpd->b_state = bh->b_state & BH_FLAGS;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002418 }
Alex Tomas64769242008-07-11 19:27:31 -04002419 logical++;
2420 } while ((bh = bh->b_this_page) != head);
2421 }
2422
2423 return 0;
2424}
2425
2426/*
Theodore Ts'ob920c752009-05-14 00:54:29 -04002427 * This is a special get_blocks_t callback which is used by
2428 * ext4_da_write_begin(). It will either return mapped block or
2429 * reserve space for a single block.
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002430 *
2431 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
2432 * We also have b_blocknr = -1 and b_bdev initialized properly
2433 *
2434 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
2435 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
2436 * initialized properly.
Alex Tomas64769242008-07-11 19:27:31 -04002437 */
2438static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
2439 struct buffer_head *bh_result, int create)
2440{
2441 int ret = 0;
Aneesh Kumar K.V33b98172009-05-12 14:40:37 -04002442 sector_t invalid_block = ~((sector_t) 0xffff);
2443
2444 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
2445 invalid_block = ~0;
Alex Tomas64769242008-07-11 19:27:31 -04002446
2447 BUG_ON(create == 0);
2448 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
2449
2450 /*
2451 * first, we need to know whether the block is allocated already
2452 * preallocated blocks are unmapped but should treated
2453 * the same as allocated blocks.
2454 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04002455 ret = ext4_get_blocks(NULL, inode, iblock, 1, bh_result, 0);
Mingming Caod2a17632008-07-14 17:52:37 -04002456 if ((ret == 0) && !buffer_delay(bh_result)) {
2457 /* the block isn't (pre)allocated yet, let's reserve space */
Alex Tomas64769242008-07-11 19:27:31 -04002458 /*
2459 * XXX: __block_prepare_write() unmaps passed block,
2460 * is it OK?
2461 */
Mingming Caod2a17632008-07-14 17:52:37 -04002462 ret = ext4_da_reserve_space(inode, 1);
2463 if (ret)
2464 /* not enough space to reserve */
2465 return ret;
2466
Aneesh Kumar K.V33b98172009-05-12 14:40:37 -04002467 map_bh(bh_result, inode->i_sb, invalid_block);
Alex Tomas64769242008-07-11 19:27:31 -04002468 set_buffer_new(bh_result);
2469 set_buffer_delay(bh_result);
2470 } else if (ret > 0) {
2471 bh_result->b_size = (ret << inode->i_blkbits);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002472 if (buffer_unwritten(bh_result)) {
2473 /* A delayed write to unwritten bh should
2474 * be marked new and mapped. Mapped ensures
2475 * that we don't do get_block multiple times
2476 * when we write to the same offset and new
2477 * ensures that we do proper zero out for
2478 * partial write.
2479 */
Aneesh Kumar K.V9c1ee182009-05-13 18:36:58 -04002480 set_buffer_new(bh_result);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002481 set_buffer_mapped(bh_result);
2482 }
Alex Tomas64769242008-07-11 19:27:31 -04002483 ret = 0;
2484 }
2485
2486 return ret;
2487}
Mingming Cao61628a32008-07-11 19:27:31 -04002488
Theodore Ts'ob920c752009-05-14 00:54:29 -04002489/*
2490 * This function is used as a standard get_block_t calback function
2491 * when there is no desire to allocate any blocks. It is used as a
2492 * callback function for block_prepare_write(), nobh_writepage(), and
2493 * block_write_full_page(). These functions should only try to map a
2494 * single block at a time.
2495 *
2496 * Since this function doesn't do block allocations even if the caller
2497 * requests it by passing in create=1, it is critically important that
2498 * any caller checks to make sure that any buffer heads are returned
2499 * by this function are either all already mapped or marked for
2500 * delayed allocation before calling nobh_writepage() or
2501 * block_write_full_page(). Otherwise, b_blocknr could be left
2502 * unitialized, and the page write functions will be taken by
2503 * surprise.
2504 */
2505static int noalloc_get_block_write(struct inode *inode, sector_t iblock,
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002506 struct buffer_head *bh_result, int create)
2507{
2508 int ret = 0;
2509 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
2510
Theodore Ts'oa2dc52b2009-05-12 13:51:29 -04002511 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
2512
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002513 /*
2514 * we don't want to do block allocation in writepage
2515 * so call get_block_wrap with create = 0
2516 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04002517 ret = ext4_get_blocks(NULL, inode, iblock, max_blocks, bh_result, 0);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002518 if (ret > 0) {
2519 bh_result->b_size = (ret << inode->i_blkbits);
2520 ret = 0;
2521 }
2522 return ret;
Mingming Cao61628a32008-07-11 19:27:31 -04002523}
2524
Mingming Cao61628a32008-07-11 19:27:31 -04002525/*
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002526 * Note that we don't need to start a transaction unless we're journaling data
2527 * because we should have holes filled from ext4_page_mkwrite(). We even don't
2528 * need to file the inode to the transaction's list in ordered mode because if
2529 * we are writing back data added by write(), the inode is already there and if
2530 * we are writing back data modified via mmap(), noone guarantees in which
2531 * transaction the data will hit the disk. In case we are journaling data, we
2532 * cannot start transaction directly because transaction start ranks above page
2533 * lock so we have to do some magic.
2534 *
Theodore Ts'ob920c752009-05-14 00:54:29 -04002535 * This function can get called via...
2536 * - ext4_da_writepages after taking page lock (have journal handle)
2537 * - journal_submit_inode_data_buffers (no journal handle)
2538 * - shrink_page_list via pdflush (no journal handle)
2539 * - grab_page_cache when doing write_begin (have journal handle)
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002540 *
2541 * We don't do any block allocation in this function. If we have page with
2542 * multiple blocks we need to write those buffer_heads that are mapped. This
2543 * is important for mmaped based write. So if we do with blocksize 1K
2544 * truncate(f, 1024);
2545 * a = mmap(f, 0, 4096);
2546 * a[0] = 'a';
2547 * truncate(f, 4096);
2548 * we have in the page first buffer_head mapped via page_mkwrite call back
2549 * but other bufer_heads would be unmapped but dirty(dirty done via the
2550 * do_wp_page). So writepage should write the first block. If we modify
2551 * the mmap area beyond 1024 we will again get a page_fault and the
2552 * page_mkwrite callback will do the block allocation and mark the
2553 * buffer_heads mapped.
2554 *
2555 * We redirty the page if we have any buffer_heads that is either delay or
2556 * unwritten in the page.
2557 *
2558 * We can get recursively called as show below.
2559 *
2560 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
2561 * ext4_writepage()
2562 *
2563 * But since we don't do any block allocation we should not deadlock.
2564 * Page also have the dirty flag cleared so we don't get recurive page_lock.
Mingming Cao61628a32008-07-11 19:27:31 -04002565 */
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002566static int ext4_writepage(struct page *page,
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002567 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002568{
Alex Tomas64769242008-07-11 19:27:31 -04002569 int ret = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002570 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002571 unsigned int len;
Mingming Cao61628a32008-07-11 19:27:31 -04002572 struct buffer_head *page_bufs;
2573 struct inode *inode = page->mapping->host;
Alex Tomas64769242008-07-11 19:27:31 -04002574
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002575 trace_ext4_writepage(inode, page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002576 size = i_size_read(inode);
2577 if (page->index == size >> PAGE_CACHE_SHIFT)
2578 len = size & ~PAGE_CACHE_MASK;
2579 else
2580 len = PAGE_CACHE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04002581
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002582 if (page_has_buffers(page)) {
Mingming Cao61628a32008-07-11 19:27:31 -04002583 page_bufs = page_buffers(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002584 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002585 ext4_bh_delay_or_unwritten)) {
Mingming Cao61628a32008-07-11 19:27:31 -04002586 /*
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002587 * We don't want to do block allocation
2588 * So redirty the page and return
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04002589 * We may reach here when we do a journal commit
2590 * via journal_submit_inode_data_buffers.
2591 * If we don't have mapping block we just ignore
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002592 * them. We can also reach here via shrink_page_list
2593 */
2594 redirty_page_for_writepage(wbc, page);
2595 unlock_page(page);
2596 return 0;
2597 }
2598 } else {
2599 /*
2600 * The test for page_has_buffers() is subtle:
2601 * We know the page is dirty but it lost buffers. That means
2602 * that at some moment in time after write_begin()/write_end()
2603 * has been called all buffers have been clean and thus they
2604 * must have been written at least once. So they are all
2605 * mapped and we can happily proceed with mapping them
2606 * and writing the page.
2607 *
2608 * Try to initialize the buffer_heads and check whether
2609 * all are mapped and non delay. We don't want to
2610 * do block allocation here.
2611 */
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04002612 ret = block_prepare_write(page, 0, len,
Theodore Ts'ob920c752009-05-14 00:54:29 -04002613 noalloc_get_block_write);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002614 if (!ret) {
2615 page_bufs = page_buffers(page);
2616 /* check whether all are mapped and non delay */
2617 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002618 ext4_bh_delay_or_unwritten)) {
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002619 redirty_page_for_writepage(wbc, page);
2620 unlock_page(page);
2621 return 0;
2622 }
2623 } else {
2624 /*
2625 * We can't do block allocation here
2626 * so just redity the page and unlock
2627 * and return
Mingming Cao61628a32008-07-11 19:27:31 -04002628 */
Mingming Cao61628a32008-07-11 19:27:31 -04002629 redirty_page_for_writepage(wbc, page);
2630 unlock_page(page);
2631 return 0;
2632 }
Aneesh Kumar K.Ved9b3e32008-11-07 09:06:45 -05002633 /* now mark the buffer_heads as dirty and uptodate */
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04002634 block_commit_write(page, 0, len);
Alex Tomas64769242008-07-11 19:27:31 -04002635 }
2636
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002637 if (PageChecked(page) && ext4_should_journal_data(inode)) {
2638 /*
2639 * It's mmapped pagecache. Add buffers and journal it. There
2640 * doesn't seem much point in redirtying the page here.
2641 */
2642 ClearPageChecked(page);
2643 return __ext4_journalled_writepage(page, wbc, len);
2644 }
2645
Alex Tomas64769242008-07-11 19:27:31 -04002646 if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode))
Theodore Ts'ob920c752009-05-14 00:54:29 -04002647 ret = nobh_writepage(page, noalloc_get_block_write, wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002648 else
Theodore Ts'ob920c752009-05-14 00:54:29 -04002649 ret = block_write_full_page(page, noalloc_get_block_write,
2650 wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002651
Alex Tomas64769242008-07-11 19:27:31 -04002652 return ret;
2653}
2654
Mingming Cao61628a32008-07-11 19:27:31 -04002655/*
Mingming Cao525f4ed2008-08-19 22:15:58 -04002656 * This is called via ext4_da_writepages() to
2657 * calulate the total number of credits to reserve to fit
2658 * a single extent allocation into a single transaction,
2659 * ext4_da_writpeages() will loop calling this before
2660 * the block allocation.
Mingming Cao61628a32008-07-11 19:27:31 -04002661 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002662
2663static int ext4_da_writepages_trans_blocks(struct inode *inode)
2664{
2665 int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
2666
2667 /*
2668 * With non-extent format the journal credit needed to
2669 * insert nrblocks contiguous block is dependent on
2670 * number of contiguous block. So we will limit
2671 * number of contiguous block to a sane value
2672 */
2673 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2674 (max_blocks > EXT4_MAX_TRANS_DATA))
2675 max_blocks = EXT4_MAX_TRANS_DATA;
2676
2677 return ext4_chunk_trans_blocks(inode, max_blocks);
2678}
Mingming Cao61628a32008-07-11 19:27:31 -04002679
Alex Tomas64769242008-07-11 19:27:31 -04002680static int ext4_da_writepages(struct address_space *mapping,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002681 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002682{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002683 pgoff_t index;
2684 int range_whole = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002685 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002686 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002687 struct inode *inode = mapping->host;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002688 int no_nrwrite_index_update;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002689 int pages_written = 0;
2690 long pages_skipped;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002691 int range_cyclic, cycled = 1, io_done = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002692 int needed_blocks, ret = 0, nr_to_writebump = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002693 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Mingming Cao61628a32008-07-11 19:27:31 -04002694
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002695 trace_ext4_da_writepages(inode, wbc);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002696
Mingming Cao61628a32008-07-11 19:27:31 -04002697 /*
2698 * No pages to write? This is mainly a kludge to avoid starting
2699 * a transaction for special inodes like journal inode on last iput()
2700 * because that could violate lock ordering on umount
2701 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002702 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Mingming Cao61628a32008-07-11 19:27:31 -04002703 return 0;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002704
2705 /*
2706 * If the filesystem has aborted, it is read-only, so return
2707 * right away instead of dumping stack traces later on that
2708 * will obscure the real source of the problem. We test
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002709 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002710 * the latter could be true if the filesystem is mounted
2711 * read-only, and in that case, ext4_da_writepages should
2712 * *never* be called, so if that ever happens, we would want
2713 * the stack trace.
2714 */
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002715 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002716 return -EROFS;
2717
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002718 /*
2719 * Make sure nr_to_write is >= sbi->s_mb_stream_request
2720 * This make sure small files blocks are allocated in
2721 * single attempt. This ensure that small files
2722 * get less fragmented.
2723 */
2724 if (wbc->nr_to_write < sbi->s_mb_stream_request) {
2725 nr_to_writebump = sbi->s_mb_stream_request - wbc->nr_to_write;
2726 wbc->nr_to_write = sbi->s_mb_stream_request;
2727 }
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002728 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2729 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002730
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002731 range_cyclic = wbc->range_cyclic;
2732 if (wbc->range_cyclic) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002733 index = mapping->writeback_index;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002734 if (index)
2735 cycled = 0;
2736 wbc->range_start = index << PAGE_CACHE_SHIFT;
2737 wbc->range_end = LLONG_MAX;
2738 wbc->range_cyclic = 0;
2739 } else
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002740 index = wbc->range_start >> PAGE_CACHE_SHIFT;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002741
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002742 mpd.wbc = wbc;
2743 mpd.inode = mapping->host;
2744
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002745 /*
2746 * we don't want write_cache_pages to update
2747 * nr_to_write and writeback_index
2748 */
2749 no_nrwrite_index_update = wbc->no_nrwrite_index_update;
2750 wbc->no_nrwrite_index_update = 1;
2751 pages_skipped = wbc->pages_skipped;
2752
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002753retry:
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002754 while (!ret && wbc->nr_to_write > 0) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002755
2756 /*
2757 * we insert one extent at a time. So we need
2758 * credit needed for single extent allocation.
2759 * journalled mode is currently not supported
2760 * by delalloc
2761 */
2762 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002763 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002764
Mingming Cao61628a32008-07-11 19:27:31 -04002765 /* start a new transaction*/
2766 handle = ext4_journal_start(inode, needed_blocks);
2767 if (IS_ERR(handle)) {
2768 ret = PTR_ERR(handle);
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002769 printk(KERN_CRIT "%s: jbd2_start: "
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002770 "%ld pages, ino %lu; err %d\n", __func__,
2771 wbc->nr_to_write, inode->i_ino, ret);
2772 dump_stack();
Mingming Cao61628a32008-07-11 19:27:31 -04002773 goto out_writepages;
2774 }
Theodore Ts'of63e6002009-02-23 16:42:39 -05002775
2776 /*
2777 * Now call __mpage_da_writepage to find the next
2778 * contiguous region of logical blocks that need
2779 * blocks to be allocated by ext4. We don't actually
2780 * submit the blocks for I/O here, even though
2781 * write_cache_pages thinks it will, and will set the
2782 * pages as clean for write before calling
2783 * __mpage_da_writepage().
2784 */
2785 mpd.b_size = 0;
2786 mpd.b_state = 0;
2787 mpd.b_blocknr = 0;
2788 mpd.first_page = 0;
2789 mpd.next_page = 0;
2790 mpd.io_done = 0;
2791 mpd.pages_written = 0;
2792 mpd.retval = 0;
2793 ret = write_cache_pages(mapping, wbc, __mpage_da_writepage,
2794 &mpd);
2795 /*
2796 * If we have a contigous extent of pages and we
2797 * haven't done the I/O yet, map the blocks and submit
2798 * them for I/O.
2799 */
2800 if (!mpd.io_done && mpd.next_page != mpd.first_page) {
2801 if (mpage_da_map_blocks(&mpd) == 0)
2802 mpage_da_submit_io(&mpd);
2803 mpd.io_done = 1;
2804 ret = MPAGE_DA_EXTENT_TAIL;
2805 }
2806 wbc->nr_to_write -= mpd.pages_written;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002807
Mingming Cao61628a32008-07-11 19:27:31 -04002808 ext4_journal_stop(handle);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002809
Eric Sandeen8f64b322009-02-26 00:57:35 -05002810 if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002811 /* commit the transaction which would
2812 * free blocks released in the transaction
2813 * and try again
2814 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002815 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002816 wbc->pages_skipped = pages_skipped;
2817 ret = 0;
2818 } else if (ret == MPAGE_DA_EXTENT_TAIL) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002819 /*
2820 * got one extent now try with
2821 * rest of the pages
2822 */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002823 pages_written += mpd.pages_written;
2824 wbc->pages_skipped = pages_skipped;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002825 ret = 0;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002826 io_done = 1;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002827 } else if (wbc->nr_to_write)
Mingming Cao61628a32008-07-11 19:27:31 -04002828 /*
2829 * There is no more writeout needed
2830 * or we requested for a noblocking writeout
2831 * and we found the device congested
2832 */
Mingming Cao61628a32008-07-11 19:27:31 -04002833 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002834 }
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002835 if (!io_done && !cycled) {
2836 cycled = 1;
2837 index = 0;
2838 wbc->range_start = index << PAGE_CACHE_SHIFT;
2839 wbc->range_end = mapping->writeback_index - 1;
2840 goto retry;
2841 }
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002842 if (pages_skipped != wbc->pages_skipped)
2843 printk(KERN_EMERG "This should not happen leaving %s "
2844 "with nr_to_write = %ld ret = %d\n",
2845 __func__, wbc->nr_to_write, ret);
Mingming Cao61628a32008-07-11 19:27:31 -04002846
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002847 /* Update index */
2848 index += pages_written;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002849 wbc->range_cyclic = range_cyclic;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002850 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2851 /*
2852 * set the writeback_index so that range_cyclic
2853 * mode will write it back later
2854 */
2855 mapping->writeback_index = index;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002856
Mingming Cao61628a32008-07-11 19:27:31 -04002857out_writepages:
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002858 if (!no_nrwrite_index_update)
2859 wbc->no_nrwrite_index_update = 0;
2860 wbc->nr_to_write -= nr_to_writebump;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002861 trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
Mingming Cao61628a32008-07-11 19:27:31 -04002862 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002863}
2864
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002865#define FALL_BACK_TO_NONDELALLOC 1
2866static int ext4_nonda_switch(struct super_block *sb)
2867{
2868 s64 free_blocks, dirty_blocks;
2869 struct ext4_sb_info *sbi = EXT4_SB(sb);
2870
2871 /*
2872 * switch to non delalloc mode if we are running low
2873 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08002874 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002875 * accumulated on each CPU without updating global counters
2876 * Delalloc need an accurate free block accounting. So switch
2877 * to non delalloc when we are near to error range.
2878 */
2879 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
2880 dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter);
2881 if (2 * free_blocks < 3 * dirty_blocks ||
2882 free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) {
2883 /*
2884 * free block count is less that 150% of dirty blocks
2885 * or free blocks is less that watermark
2886 */
2887 return 1;
2888 }
2889 return 0;
2890}
2891
Alex Tomas64769242008-07-11 19:27:31 -04002892static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002893 loff_t pos, unsigned len, unsigned flags,
2894 struct page **pagep, void **fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002895{
Mingming Caod2a17632008-07-14 17:52:37 -04002896 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002897 struct page *page;
2898 pgoff_t index;
2899 unsigned from, to;
2900 struct inode *inode = mapping->host;
2901 handle_t *handle;
2902
2903 index = pos >> PAGE_CACHE_SHIFT;
2904 from = pos & (PAGE_CACHE_SIZE - 1);
2905 to = from + len;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002906
2907 if (ext4_nonda_switch(inode->i_sb)) {
2908 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2909 return ext4_write_begin(file, mapping, pos,
2910 len, flags, pagep, fsdata);
2911 }
2912 *fsdata = (void *)0;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002913 trace_ext4_da_write_begin(inode, pos, len, flags);
Mingming Caod2a17632008-07-14 17:52:37 -04002914retry:
Alex Tomas64769242008-07-11 19:27:31 -04002915 /*
2916 * With delayed allocation, we don't log the i_disksize update
2917 * if there is delayed block allocation. But we still need
2918 * to journalling the i_disksize update if writes to the end
2919 * of file which has an already mapped buffer.
2920 */
2921 handle = ext4_journal_start(inode, 1);
2922 if (IS_ERR(handle)) {
2923 ret = PTR_ERR(handle);
2924 goto out;
2925 }
Jan Karaebd36102009-02-22 21:09:59 -05002926 /* We cannot recurse into the filesystem as the transaction is already
2927 * started */
2928 flags |= AOP_FLAG_NOFS;
Alex Tomas64769242008-07-11 19:27:31 -04002929
Nick Piggin54566b22009-01-04 12:00:53 -08002930 page = grab_cache_page_write_begin(mapping, index, flags);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002931 if (!page) {
2932 ext4_journal_stop(handle);
2933 ret = -ENOMEM;
2934 goto out;
2935 }
Alex Tomas64769242008-07-11 19:27:31 -04002936 *pagep = page;
2937
2938 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
Theodore Ts'ob920c752009-05-14 00:54:29 -04002939 ext4_da_get_block_prep);
Alex Tomas64769242008-07-11 19:27:31 -04002940 if (ret < 0) {
2941 unlock_page(page);
2942 ext4_journal_stop(handle);
2943 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04002944 /*
2945 * block_write_begin may have instantiated a few blocks
2946 * outside i_size. Trim these off again. Don't need
2947 * i_size_read because we hold i_mutex.
2948 */
2949 if (pos + len > inode->i_size)
Jan Karaffacfa72009-07-13 16:22:22 -04002950 ext4_truncate(inode);
Alex Tomas64769242008-07-11 19:27:31 -04002951 }
2952
Mingming Caod2a17632008-07-14 17:52:37 -04002953 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
2954 goto retry;
Alex Tomas64769242008-07-11 19:27:31 -04002955out:
2956 return ret;
2957}
2958
Mingming Cao632eaea2008-07-11 19:27:31 -04002959/*
2960 * Check if we should update i_disksize
2961 * when write to the end of file but not require block allocation
2962 */
2963static int ext4_da_should_update_i_disksize(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002964 unsigned long offset)
Mingming Cao632eaea2008-07-11 19:27:31 -04002965{
2966 struct buffer_head *bh;
2967 struct inode *inode = page->mapping->host;
2968 unsigned int idx;
2969 int i;
2970
2971 bh = page_buffers(page);
2972 idx = offset >> inode->i_blkbits;
2973
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002974 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04002975 bh = bh->b_this_page;
2976
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002977 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
Mingming Cao632eaea2008-07-11 19:27:31 -04002978 return 0;
2979 return 1;
2980}
2981
Alex Tomas64769242008-07-11 19:27:31 -04002982static int ext4_da_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002983 struct address_space *mapping,
2984 loff_t pos, unsigned len, unsigned copied,
2985 struct page *page, void *fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002986{
2987 struct inode *inode = mapping->host;
2988 int ret = 0, ret2;
2989 handle_t *handle = ext4_journal_current_handle();
2990 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04002991 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002992 int write_mode = (int)(unsigned long)fsdata;
2993
2994 if (write_mode == FALL_BACK_TO_NONDELALLOC) {
2995 if (ext4_should_order_data(inode)) {
2996 return ext4_ordered_write_end(file, mapping, pos,
2997 len, copied, page, fsdata);
2998 } else if (ext4_should_writeback_data(inode)) {
2999 return ext4_writeback_write_end(file, mapping, pos,
3000 len, copied, page, fsdata);
3001 } else {
3002 BUG();
3003 }
3004 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003005
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003006 trace_ext4_da_write_end(inode, pos, len, copied);
Mingming Cao632eaea2008-07-11 19:27:31 -04003007 start = pos & (PAGE_CACHE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003008 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04003009
3010 /*
3011 * generic_write_end() will run mark_inode_dirty() if i_size
3012 * changes. So let's piggyback the i_disksize mark_inode_dirty
3013 * into that.
3014 */
3015
3016 new_i_size = pos + copied;
Mingming Cao632eaea2008-07-11 19:27:31 -04003017 if (new_i_size > EXT4_I(inode)->i_disksize) {
3018 if (ext4_da_should_update_i_disksize(page, end)) {
3019 down_write(&EXT4_I(inode)->i_data_sem);
3020 if (new_i_size > EXT4_I(inode)->i_disksize) {
3021 /*
3022 * Updating i_disksize when extending file
3023 * without needing block allocation
3024 */
3025 if (ext4_should_order_data(inode))
3026 ret = ext4_jbd2_file_inode(handle,
3027 inode);
Alex Tomas64769242008-07-11 19:27:31 -04003028
Mingming Cao632eaea2008-07-11 19:27:31 -04003029 EXT4_I(inode)->i_disksize = new_i_size;
3030 }
3031 up_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003032 /* We need to mark inode dirty even if
3033 * new_i_size is less that inode->i_size
3034 * bu greater than i_disksize.(hint delalloc)
3035 */
3036 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04003037 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003038 }
Alex Tomas64769242008-07-11 19:27:31 -04003039 ret2 = generic_write_end(file, mapping, pos, len, copied,
3040 page, fsdata);
3041 copied = ret2;
3042 if (ret2 < 0)
3043 ret = ret2;
3044 ret2 = ext4_journal_stop(handle);
3045 if (!ret)
3046 ret = ret2;
3047
3048 return ret ? ret : copied;
3049}
3050
3051static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
3052{
Alex Tomas64769242008-07-11 19:27:31 -04003053 /*
3054 * Drop reserved blocks
3055 */
3056 BUG_ON(!PageLocked(page));
3057 if (!page_has_buffers(page))
3058 goto out;
3059
Mingming Caod2a17632008-07-14 17:52:37 -04003060 ext4_da_page_release_reservation(page, offset);
Alex Tomas64769242008-07-11 19:27:31 -04003061
3062out:
3063 ext4_invalidatepage(page, offset);
3064
3065 return;
3066}
3067
Theodore Ts'occd25062009-02-26 01:04:07 -05003068/*
3069 * Force all delayed allocation blocks to be allocated for a given inode.
3070 */
3071int ext4_alloc_da_blocks(struct inode *inode)
3072{
3073 if (!EXT4_I(inode)->i_reserved_data_blocks &&
3074 !EXT4_I(inode)->i_reserved_meta_blocks)
3075 return 0;
3076
3077 /*
3078 * We do something simple for now. The filemap_flush() will
3079 * also start triggering a write of the data blocks, which is
3080 * not strictly speaking necessary (and for users of
3081 * laptop_mode, not even desirable). However, to do otherwise
3082 * would require replicating code paths in:
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003083 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003084 * ext4_da_writepages() ->
3085 * write_cache_pages() ---> (via passed in callback function)
3086 * __mpage_da_writepage() -->
3087 * mpage_add_bh_to_extent()
3088 * mpage_da_map_blocks()
3089 *
3090 * The problem is that write_cache_pages(), located in
3091 * mm/page-writeback.c, marks pages clean in preparation for
3092 * doing I/O, which is not desirable if we're not planning on
3093 * doing I/O at all.
3094 *
3095 * We could call write_cache_pages(), and then redirty all of
3096 * the pages by calling redirty_page_for_writeback() but that
3097 * would be ugly in the extreme. So instead we would need to
3098 * replicate parts of the code in the above functions,
3099 * simplifying them becuase we wouldn't actually intend to
3100 * write out the pages, but rather only collect contiguous
3101 * logical block extents, call the multi-block allocator, and
3102 * then update the buffer heads with the block allocations.
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003103 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003104 * For now, though, we'll cheat by calling filemap_flush(),
3105 * which will map the blocks, and start the I/O, but not
3106 * actually wait for the I/O to complete.
3107 */
3108 return filemap_flush(inode->i_mapping);
3109}
Alex Tomas64769242008-07-11 19:27:31 -04003110
3111/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003112 * bmap() is special. It gets used by applications such as lilo and by
3113 * the swapper to find the on-disk block of a specific piece of data.
3114 *
3115 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07003116 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003117 * filesystem and enables swap, then they may get a nasty shock when the
3118 * data getting swapped to that swapfile suddenly gets overwritten by
3119 * the original zero's written out previously to the journal and
3120 * awaiting writeback in the kernel's buffer cache.
3121 *
3122 * So, if we see any bmap calls here on a modified, data-journaled file,
3123 * take extra steps to flush any blocks which might be in the cache.
3124 */
Mingming Cao617ba132006-10-11 01:20:53 -07003125static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003126{
3127 struct inode *inode = mapping->host;
3128 journal_t *journal;
3129 int err;
3130
Alex Tomas64769242008-07-11 19:27:31 -04003131 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
3132 test_opt(inode->i_sb, DELALLOC)) {
3133 /*
3134 * With delalloc we want to sync the file
3135 * so that we can make sure we allocate
3136 * blocks for file
3137 */
3138 filemap_write_and_wait(mapping);
3139 }
3140
Frank Mayhar03901312009-01-07 00:06:22 -05003141 if (EXT4_JOURNAL(inode) && EXT4_I(inode)->i_state & EXT4_STATE_JDATA) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003142 /*
3143 * This is a REALLY heavyweight approach, but the use of
3144 * bmap on dirty files is expected to be extremely rare:
3145 * only if we run lilo or swapon on a freshly made file
3146 * do we expect this to happen.
3147 *
3148 * (bmap requires CAP_SYS_RAWIO so this does not
3149 * represent an unprivileged user DOS attack --- we'd be
3150 * in trouble if mortal users could trigger this path at
3151 * will.)
3152 *
Mingming Cao617ba132006-10-11 01:20:53 -07003153 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003154 * regular files. If somebody wants to bmap a directory
3155 * or symlink and gets confused because the buffer
3156 * hasn't yet been flushed to disk, they deserve
3157 * everything they get.
3158 */
3159
Mingming Cao617ba132006-10-11 01:20:53 -07003160 EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA;
3161 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07003162 jbd2_journal_lock_updates(journal);
3163 err = jbd2_journal_flush(journal);
3164 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003165
3166 if (err)
3167 return 0;
3168 }
3169
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003170 return generic_block_bmap(mapping, block, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003171}
3172
3173static int bget_one(handle_t *handle, struct buffer_head *bh)
3174{
3175 get_bh(bh);
3176 return 0;
3177}
3178
3179static int bput_one(handle_t *handle, struct buffer_head *bh)
3180{
3181 put_bh(bh);
3182 return 0;
3183}
3184
Jan Karacf108bc2008-07-11 19:27:31 -04003185static int __ext4_journalled_writepage(struct page *page,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003186 struct writeback_control *wbc,
3187 unsigned int len)
Jan Karacf108bc2008-07-11 19:27:31 -04003188{
3189 struct address_space *mapping = page->mapping;
3190 struct inode *inode = mapping->host;
3191 struct buffer_head *page_bufs;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003192 handle_t *handle = NULL;
3193 int ret = 0;
3194 int err;
3195
Jan Karacf108bc2008-07-11 19:27:31 -04003196 page_bufs = page_buffers(page);
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003197 BUG_ON(!page_bufs);
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04003198 walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one);
Jan Karacf108bc2008-07-11 19:27:31 -04003199 /* As soon as we unlock the page, it can go away, but we have
3200 * references to buffers so we are safe */
3201 unlock_page(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003202
Mingming Cao617ba132006-10-11 01:20:53 -07003203 handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003204 if (IS_ERR(handle)) {
3205 ret = PTR_ERR(handle);
Jan Karacf108bc2008-07-11 19:27:31 -04003206 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003207 }
3208
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04003209 ret = walk_page_buffers(handle, page_bufs, 0, len, NULL,
3210 do_journal_get_write_access);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003211
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04003212 err = walk_page_buffers(handle, page_bufs, 0, len, NULL,
3213 write_end_fn);
Jan Karacf108bc2008-07-11 19:27:31 -04003214 if (ret == 0)
3215 ret = err;
Mingming Cao617ba132006-10-11 01:20:53 -07003216 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003217 if (!ret)
3218 ret = err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003219
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04003220 walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one);
Jan Karacf108bc2008-07-11 19:27:31 -04003221 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
Jan Karacf108bc2008-07-11 19:27:31 -04003222out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003223 return ret;
3224}
3225
Mingming Cao617ba132006-10-11 01:20:53 -07003226static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003227{
Mingming Cao617ba132006-10-11 01:20:53 -07003228 return mpage_readpage(page, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003229}
3230
3231static int
Mingming Cao617ba132006-10-11 01:20:53 -07003232ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003233 struct list_head *pages, unsigned nr_pages)
3234{
Mingming Cao617ba132006-10-11 01:20:53 -07003235 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003236}
3237
Mingming Cao617ba132006-10-11 01:20:53 -07003238static void ext4_invalidatepage(struct page *page, unsigned long offset)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003239{
Mingming Cao617ba132006-10-11 01:20:53 -07003240 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003241
3242 /*
3243 * If it's a full truncate we just forget about the pending dirtying
3244 */
3245 if (offset == 0)
3246 ClearPageChecked(page);
3247
Frank Mayhar03901312009-01-07 00:06:22 -05003248 if (journal)
3249 jbd2_journal_invalidatepage(journal, page, offset);
3250 else
3251 block_invalidatepage(page, offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003252}
3253
Mingming Cao617ba132006-10-11 01:20:53 -07003254static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003255{
Mingming Cao617ba132006-10-11 01:20:53 -07003256 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003257
3258 WARN_ON(PageChecked(page));
3259 if (!page_has_buffers(page))
3260 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003261 if (journal)
3262 return jbd2_journal_try_to_free_buffers(journal, page, wait);
3263 else
3264 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003265}
3266
3267/*
3268 * If the O_DIRECT write will extend the file then add this inode to the
3269 * orphan list. So recovery will truncate it back to the original size
3270 * if the machine crashes during the write.
3271 *
3272 * If the O_DIRECT write is intantiating holes inside i_size and the machine
Jan Kara7fb54092008-02-10 01:08:38 -05003273 * crashes then stale disk data _may_ be exposed inside the file. But current
3274 * VFS code falls back into buffered path in that case so we are safe.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003275 */
Mingming Cao617ba132006-10-11 01:20:53 -07003276static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003277 const struct iovec *iov, loff_t offset,
3278 unsigned long nr_segs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003279{
3280 struct file *file = iocb->ki_filp;
3281 struct inode *inode = file->f_mapping->host;
Mingming Cao617ba132006-10-11 01:20:53 -07003282 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003283 handle_t *handle;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003284 ssize_t ret;
3285 int orphan = 0;
3286 size_t count = iov_length(iov, nr_segs);
3287
3288 if (rw == WRITE) {
3289 loff_t final_size = offset + count;
3290
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003291 if (final_size > inode->i_size) {
Jan Kara7fb54092008-02-10 01:08:38 -05003292 /* Credits for sb + inode write */
3293 handle = ext4_journal_start(inode, 2);
3294 if (IS_ERR(handle)) {
3295 ret = PTR_ERR(handle);
3296 goto out;
3297 }
Mingming Cao617ba132006-10-11 01:20:53 -07003298 ret = ext4_orphan_add(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003299 if (ret) {
3300 ext4_journal_stop(handle);
3301 goto out;
3302 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003303 orphan = 1;
3304 ei->i_disksize = inode->i_size;
Jan Kara7fb54092008-02-10 01:08:38 -05003305 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003306 }
3307 }
3308
3309 ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
3310 offset, nr_segs,
Mingming Cao617ba132006-10-11 01:20:53 -07003311 ext4_get_block, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003312
Jan Kara7fb54092008-02-10 01:08:38 -05003313 if (orphan) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003314 int err;
3315
Jan Kara7fb54092008-02-10 01:08:38 -05003316 /* Credits for sb + inode write */
3317 handle = ext4_journal_start(inode, 2);
3318 if (IS_ERR(handle)) {
3319 /* This is really bad luck. We've written the data
3320 * but cannot extend i_size. Bail out and pretend
3321 * the write failed... */
3322 ret = PTR_ERR(handle);
3323 goto out;
3324 }
3325 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003326 ext4_orphan_del(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003327 if (ret > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003328 loff_t end = offset + ret;
3329 if (end > inode->i_size) {
3330 ei->i_disksize = end;
3331 i_size_write(inode, end);
3332 /*
3333 * We're going to return a positive `ret'
3334 * here due to non-zero-length I/O, so there's
3335 * no way of reporting error returns from
Mingming Cao617ba132006-10-11 01:20:53 -07003336 * ext4_mark_inode_dirty() to userspace. So
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003337 * ignore it.
3338 */
Mingming Cao617ba132006-10-11 01:20:53 -07003339 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003340 }
3341 }
Mingming Cao617ba132006-10-11 01:20:53 -07003342 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003343 if (ret == 0)
3344 ret = err;
3345 }
3346out:
3347 return ret;
3348}
3349
3350/*
Mingming Cao617ba132006-10-11 01:20:53 -07003351 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003352 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3353 * much here because ->set_page_dirty is called under VFS locks. The page is
3354 * not necessarily locked.
3355 *
3356 * We cannot just dirty the page and leave attached buffers clean, because the
3357 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3358 * or jbddirty because all the journalling code will explode.
3359 *
3360 * So what we do is to mark the page "pending dirty" and next time writepage
3361 * is called, propagate that into the buffers appropriately.
3362 */
Mingming Cao617ba132006-10-11 01:20:53 -07003363static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003364{
3365 SetPageChecked(page);
3366 return __set_page_dirty_nobuffers(page);
3367}
3368
Mingming Cao617ba132006-10-11 01:20:53 -07003369static const struct address_space_operations ext4_ordered_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003370 .readpage = ext4_readpage,
3371 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003372 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003373 .sync_page = block_sync_page,
3374 .write_begin = ext4_write_begin,
3375 .write_end = ext4_ordered_write_end,
3376 .bmap = ext4_bmap,
3377 .invalidatepage = ext4_invalidatepage,
3378 .releasepage = ext4_releasepage,
3379 .direct_IO = ext4_direct_IO,
3380 .migratepage = buffer_migrate_page,
3381 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003382};
3383
Mingming Cao617ba132006-10-11 01:20:53 -07003384static const struct address_space_operations ext4_writeback_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003385 .readpage = ext4_readpage,
3386 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003387 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003388 .sync_page = block_sync_page,
3389 .write_begin = ext4_write_begin,
3390 .write_end = ext4_writeback_write_end,
3391 .bmap = ext4_bmap,
3392 .invalidatepage = ext4_invalidatepage,
3393 .releasepage = ext4_releasepage,
3394 .direct_IO = ext4_direct_IO,
3395 .migratepage = buffer_migrate_page,
3396 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003397};
3398
Mingming Cao617ba132006-10-11 01:20:53 -07003399static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003400 .readpage = ext4_readpage,
3401 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003402 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003403 .sync_page = block_sync_page,
3404 .write_begin = ext4_write_begin,
3405 .write_end = ext4_journalled_write_end,
3406 .set_page_dirty = ext4_journalled_set_page_dirty,
3407 .bmap = ext4_bmap,
3408 .invalidatepage = ext4_invalidatepage,
3409 .releasepage = ext4_releasepage,
3410 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003411};
3412
Alex Tomas64769242008-07-11 19:27:31 -04003413static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003414 .readpage = ext4_readpage,
3415 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003416 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003417 .writepages = ext4_da_writepages,
3418 .sync_page = block_sync_page,
3419 .write_begin = ext4_da_write_begin,
3420 .write_end = ext4_da_write_end,
3421 .bmap = ext4_bmap,
3422 .invalidatepage = ext4_da_invalidatepage,
3423 .releasepage = ext4_releasepage,
3424 .direct_IO = ext4_direct_IO,
3425 .migratepage = buffer_migrate_page,
3426 .is_partially_uptodate = block_is_partially_uptodate,
Alex Tomas64769242008-07-11 19:27:31 -04003427};
3428
Mingming Cao617ba132006-10-11 01:20:53 -07003429void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003430{
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04003431 if (ext4_should_order_data(inode) &&
3432 test_opt(inode->i_sb, DELALLOC))
3433 inode->i_mapping->a_ops = &ext4_da_aops;
3434 else if (ext4_should_order_data(inode))
Mingming Cao617ba132006-10-11 01:20:53 -07003435 inode->i_mapping->a_ops = &ext4_ordered_aops;
Alex Tomas64769242008-07-11 19:27:31 -04003436 else if (ext4_should_writeback_data(inode) &&
3437 test_opt(inode->i_sb, DELALLOC))
3438 inode->i_mapping->a_ops = &ext4_da_aops;
Mingming Cao617ba132006-10-11 01:20:53 -07003439 else if (ext4_should_writeback_data(inode))
3440 inode->i_mapping->a_ops = &ext4_writeback_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003441 else
Mingming Cao617ba132006-10-11 01:20:53 -07003442 inode->i_mapping->a_ops = &ext4_journalled_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003443}
3444
3445/*
Mingming Cao617ba132006-10-11 01:20:53 -07003446 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003447 * up to the end of the block which corresponds to `from'.
3448 * This required during truncate. We need to physically zero the tail end
3449 * of that block so it doesn't yield old data if the file is later grown.
3450 */
Jan Karacf108bc2008-07-11 19:27:31 -04003451int ext4_block_truncate_page(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003452 struct address_space *mapping, loff_t from)
3453{
Mingming Cao617ba132006-10-11 01:20:53 -07003454 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003455 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003456 unsigned blocksize, length, pos;
3457 ext4_lblk_t iblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003458 struct inode *inode = mapping->host;
3459 struct buffer_head *bh;
Jan Karacf108bc2008-07-11 19:27:31 -04003460 struct page *page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003461 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003462
Theodore Ts'of4a01012009-07-05 22:08:16 -04003463 page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
3464 mapping_gfp_mask(mapping) & ~__GFP_FS);
Jan Karacf108bc2008-07-11 19:27:31 -04003465 if (!page)
3466 return -EINVAL;
3467
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003468 blocksize = inode->i_sb->s_blocksize;
3469 length = blocksize - (offset & (blocksize - 1));
3470 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
3471
3472 /*
3473 * For "nobh" option, we can only work if we don't need to
3474 * read-in the page - otherwise we create buffers to do the IO.
3475 */
3476 if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) &&
Mingming Cao617ba132006-10-11 01:20:53 -07003477 ext4_should_writeback_data(inode) && PageUptodate(page)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003478 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003479 set_page_dirty(page);
3480 goto unlock;
3481 }
3482
3483 if (!page_has_buffers(page))
3484 create_empty_buffers(page, blocksize, 0);
3485
3486 /* Find the buffer that contains "offset" */
3487 bh = page_buffers(page);
3488 pos = blocksize;
3489 while (offset >= pos) {
3490 bh = bh->b_this_page;
3491 iblock++;
3492 pos += blocksize;
3493 }
3494
3495 err = 0;
3496 if (buffer_freed(bh)) {
3497 BUFFER_TRACE(bh, "freed: skip");
3498 goto unlock;
3499 }
3500
3501 if (!buffer_mapped(bh)) {
3502 BUFFER_TRACE(bh, "unmapped");
Mingming Cao617ba132006-10-11 01:20:53 -07003503 ext4_get_block(inode, iblock, bh, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003504 /* unmapped? It's a hole - nothing to do */
3505 if (!buffer_mapped(bh)) {
3506 BUFFER_TRACE(bh, "still unmapped");
3507 goto unlock;
3508 }
3509 }
3510
3511 /* Ok, it's mapped. Make sure it's up-to-date */
3512 if (PageUptodate(page))
3513 set_buffer_uptodate(bh);
3514
3515 if (!buffer_uptodate(bh)) {
3516 err = -EIO;
3517 ll_rw_block(READ, 1, &bh);
3518 wait_on_buffer(bh);
3519 /* Uhhuh. Read error. Complain and punt. */
3520 if (!buffer_uptodate(bh))
3521 goto unlock;
3522 }
3523
Mingming Cao617ba132006-10-11 01:20:53 -07003524 if (ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003525 BUFFER_TRACE(bh, "get write access");
Mingming Cao617ba132006-10-11 01:20:53 -07003526 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003527 if (err)
3528 goto unlock;
3529 }
3530
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003531 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003532
3533 BUFFER_TRACE(bh, "zeroed end of block");
3534
3535 err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003536 if (ext4_should_journal_data(inode)) {
Frank Mayhar03901312009-01-07 00:06:22 -05003537 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003538 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003539 if (ext4_should_order_data(inode))
Jan Kara678aaf42008-07-11 19:27:31 -04003540 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003541 mark_buffer_dirty(bh);
3542 }
3543
3544unlock:
3545 unlock_page(page);
3546 page_cache_release(page);
3547 return err;
3548}
3549
3550/*
3551 * Probably it should be a library function... search for first non-zero word
3552 * or memcmp with zero_page, whatever is better for particular architecture.
3553 * Linus?
3554 */
3555static inline int all_zeroes(__le32 *p, __le32 *q)
3556{
3557 while (p < q)
3558 if (*p++)
3559 return 0;
3560 return 1;
3561}
3562
3563/**
Mingming Cao617ba132006-10-11 01:20:53 -07003564 * ext4_find_shared - find the indirect blocks for partial truncation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003565 * @inode: inode in question
3566 * @depth: depth of the affected branch
Mingming Cao617ba132006-10-11 01:20:53 -07003567 * @offsets: offsets of pointers in that branch (see ext4_block_to_path)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003568 * @chain: place to store the pointers to partial indirect blocks
3569 * @top: place to the (detached) top of branch
3570 *
Mingming Cao617ba132006-10-11 01:20:53 -07003571 * This is a helper function used by ext4_truncate().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003572 *
3573 * When we do truncate() we may have to clean the ends of several
3574 * indirect blocks but leave the blocks themselves alive. Block is
3575 * partially truncated if some data below the new i_size is refered
3576 * from it (and it is on the path to the first completely truncated
3577 * data block, indeed). We have to free the top of that path along
3578 * with everything to the right of the path. Since no allocation
Mingming Cao617ba132006-10-11 01:20:53 -07003579 * past the truncation point is possible until ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003580 * finishes, we may safely do the latter, but top of branch may
3581 * require special attention - pageout below the truncation point
3582 * might try to populate it.
3583 *
3584 * We atomically detach the top of branch from the tree, store the
3585 * block number of its root in *@top, pointers to buffer_heads of
3586 * partially truncated blocks - in @chain[].bh and pointers to
3587 * their last elements that should not be removed - in
3588 * @chain[].p. Return value is the pointer to last filled element
3589 * of @chain.
3590 *
3591 * The work left to caller to do the actual freeing of subtrees:
3592 * a) free the subtree starting from *@top
3593 * b) free the subtrees whose roots are stored in
3594 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
3595 * c) free the subtrees growing from the inode past the @chain[0].
3596 * (no partially truncated stuff there). */
3597
Mingming Cao617ba132006-10-11 01:20:53 -07003598static Indirect *ext4_find_shared(struct inode *inode, int depth,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003599 ext4_lblk_t offsets[4], Indirect chain[4],
3600 __le32 *top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003601{
3602 Indirect *partial, *p;
3603 int k, err;
3604
3605 *top = 0;
3606 /* Make k index the deepest non-null offest + 1 */
3607 for (k = depth; k > 1 && !offsets[k-1]; k--)
3608 ;
Mingming Cao617ba132006-10-11 01:20:53 -07003609 partial = ext4_get_branch(inode, k, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003610 /* Writer: pointers */
3611 if (!partial)
3612 partial = chain + k-1;
3613 /*
3614 * If the branch acquired continuation since we've looked at it -
3615 * fine, it should all survive and (new) top doesn't belong to us.
3616 */
3617 if (!partial->key && *partial->p)
3618 /* Writer: end */
3619 goto no_top;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003620 for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003621 ;
3622 /*
3623 * OK, we've found the last block that must survive. The rest of our
3624 * branch should be detached before unlocking. However, if that rest
3625 * of branch is all ours and does not grow immediately from the inode
3626 * it's easier to cheat and just decrement partial->p.
3627 */
3628 if (p == chain + k - 1 && p > chain) {
3629 p->p--;
3630 } else {
3631 *top = *p->p;
Mingming Cao617ba132006-10-11 01:20:53 -07003632 /* Nope, don't do this in ext4. Must leave the tree intact */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003633#if 0
3634 *p->p = 0;
3635#endif
3636 }
3637 /* Writer: end */
3638
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003639 while (partial > p) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003640 brelse(partial->bh);
3641 partial--;
3642 }
3643no_top:
3644 return partial;
3645}
3646
3647/*
3648 * Zero a number of block pointers in either an inode or an indirect block.
3649 * If we restart the transaction we must again get write access to the
3650 * indirect block for further modification.
3651 *
3652 * We release `count' blocks on disk, but (last - first) may be greater
3653 * than `count' because there can be holes in there.
3654 */
Mingming Cao617ba132006-10-11 01:20:53 -07003655static void ext4_clear_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003656 struct buffer_head *bh,
3657 ext4_fsblk_t block_to_free,
3658 unsigned long count, __le32 *first,
3659 __le32 *last)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003660{
3661 __le32 *p;
3662 if (try_to_extend_transaction(handle, inode)) {
3663 if (bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05003664 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
3665 ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003666 }
Mingming Cao617ba132006-10-11 01:20:53 -07003667 ext4_mark_inode_dirty(handle, inode);
3668 ext4_journal_test_restart(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003669 if (bh) {
3670 BUFFER_TRACE(bh, "retaking write access");
Mingming Cao617ba132006-10-11 01:20:53 -07003671 ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003672 }
3673 }
3674
3675 /*
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003676 * Any buffers which are on the journal will be in memory. We
3677 * find them on the hash table so jbd2_journal_revoke() will
3678 * run jbd2_journal_forget() on them. We've already detached
3679 * each block from the file, so bforget() in
3680 * jbd2_journal_forget() should be safe.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003681 *
Mingming Caodab291a2006-10-11 01:21:01 -07003682 * AKPM: turn on bforget in jbd2_journal_forget()!!!
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003683 */
3684 for (p = first; p < last; p++) {
3685 u32 nr = le32_to_cpu(*p);
3686 if (nr) {
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003687 struct buffer_head *tbh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003688
3689 *p = 0;
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003690 tbh = sb_find_get_block(inode->i_sb, nr);
3691 ext4_forget(handle, 0, inode, tbh, nr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003692 }
3693 }
3694
Alex Tomasc9de5602008-01-29 00:19:52 -05003695 ext4_free_blocks(handle, inode, block_to_free, count, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003696}
3697
3698/**
Mingming Cao617ba132006-10-11 01:20:53 -07003699 * ext4_free_data - free a list of data blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003700 * @handle: handle for this transaction
3701 * @inode: inode we are dealing with
3702 * @this_bh: indirect buffer_head which contains *@first and *@last
3703 * @first: array of block numbers
3704 * @last: points immediately past the end of array
3705 *
3706 * We are freeing all blocks refered from that array (numbers are stored as
3707 * little-endian 32-bit) and updating @inode->i_blocks appropriately.
3708 *
3709 * We accumulate contiguous runs of blocks to free. Conveniently, if these
3710 * blocks are contiguous then releasing them at one time will only affect one
3711 * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
3712 * actually use a lot of journal space.
3713 *
3714 * @this_bh will be %NULL if @first and @last point into the inode's direct
3715 * block pointers.
3716 */
Mingming Cao617ba132006-10-11 01:20:53 -07003717static void ext4_free_data(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003718 struct buffer_head *this_bh,
3719 __le32 *first, __le32 *last)
3720{
Mingming Cao617ba132006-10-11 01:20:53 -07003721 ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003722 unsigned long count = 0; /* Number of blocks in the run */
3723 __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
3724 corresponding to
3725 block_to_free */
Mingming Cao617ba132006-10-11 01:20:53 -07003726 ext4_fsblk_t nr; /* Current block # */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003727 __le32 *p; /* Pointer into inode/ind
3728 for current block */
3729 int err;
3730
3731 if (this_bh) { /* For indirect block */
3732 BUFFER_TRACE(this_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003733 err = ext4_journal_get_write_access(handle, this_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003734 /* Important: if we can't update the indirect pointers
3735 * to the blocks, we can't free them. */
3736 if (err)
3737 return;
3738 }
3739
3740 for (p = first; p < last; p++) {
3741 nr = le32_to_cpu(*p);
3742 if (nr) {
3743 /* accumulate blocks to free if they're contiguous */
3744 if (count == 0) {
3745 block_to_free = nr;
3746 block_to_free_p = p;
3747 count = 1;
3748 } else if (nr == block_to_free + count) {
3749 count++;
3750 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003751 ext4_clear_blocks(handle, inode, this_bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003752 block_to_free,
3753 count, block_to_free_p, p);
3754 block_to_free = nr;
3755 block_to_free_p = p;
3756 count = 1;
3757 }
3758 }
3759 }
3760
3761 if (count > 0)
Mingming Cao617ba132006-10-11 01:20:53 -07003762 ext4_clear_blocks(handle, inode, this_bh, block_to_free,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003763 count, block_to_free_p, p);
3764
3765 if (this_bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05003766 BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata");
Duane Griffin71dc8fb2008-07-11 19:27:31 -04003767
3768 /*
3769 * The buffer head should have an attached journal head at this
3770 * point. However, if the data is corrupted and an indirect
3771 * block pointed to itself, it would have been detached when
3772 * the block was cleared. Check for this instead of OOPSing.
3773 */
Theodore Ts'oe7f07962009-01-20 09:50:19 -05003774 if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
Frank Mayhar03901312009-01-07 00:06:22 -05003775 ext4_handle_dirty_metadata(handle, inode, this_bh);
Duane Griffin71dc8fb2008-07-11 19:27:31 -04003776 else
3777 ext4_error(inode->i_sb, __func__,
3778 "circular indirect block detected, "
3779 "inode=%lu, block=%llu",
3780 inode->i_ino,
3781 (unsigned long long) this_bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003782 }
3783}
3784
3785/**
Mingming Cao617ba132006-10-11 01:20:53 -07003786 * ext4_free_branches - free an array of branches
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003787 * @handle: JBD handle for this transaction
3788 * @inode: inode we are dealing with
3789 * @parent_bh: the buffer_head which contains *@first and *@last
3790 * @first: array of block numbers
3791 * @last: pointer immediately past the end of array
3792 * @depth: depth of the branches to free
3793 *
3794 * We are freeing all blocks refered from these branches (numbers are
3795 * stored as little-endian 32-bit) and updating @inode->i_blocks
3796 * appropriately.
3797 */
Mingming Cao617ba132006-10-11 01:20:53 -07003798static void ext4_free_branches(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003799 struct buffer_head *parent_bh,
3800 __le32 *first, __le32 *last, int depth)
3801{
Mingming Cao617ba132006-10-11 01:20:53 -07003802 ext4_fsblk_t nr;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003803 __le32 *p;
3804
Frank Mayhar03901312009-01-07 00:06:22 -05003805 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003806 return;
3807
3808 if (depth--) {
3809 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07003810 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003811 p = last;
3812 while (--p >= first) {
3813 nr = le32_to_cpu(*p);
3814 if (!nr)
3815 continue; /* A hole */
3816
3817 /* Go read the buffer for the next level down */
3818 bh = sb_bread(inode->i_sb, nr);
3819
3820 /*
3821 * A read failure? Report error and clear slot
3822 * (should be rare).
3823 */
3824 if (!bh) {
Mingming Cao617ba132006-10-11 01:20:53 -07003825 ext4_error(inode->i_sb, "ext4_free_branches",
Mingming Cao2ae02102006-10-11 01:21:11 -07003826 "Read failure, inode=%lu, block=%llu",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003827 inode->i_ino, nr);
3828 continue;
3829 }
3830
3831 /* This zaps the entire block. Bottom up. */
3832 BUFFER_TRACE(bh, "free child branches");
Mingming Cao617ba132006-10-11 01:20:53 -07003833 ext4_free_branches(handle, inode, bh,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003834 (__le32 *) bh->b_data,
3835 (__le32 *) bh->b_data + addr_per_block,
3836 depth);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003837
3838 /*
3839 * We've probably journalled the indirect block several
3840 * times during the truncate. But it's no longer
3841 * needed and we now drop it from the transaction via
Mingming Caodab291a2006-10-11 01:21:01 -07003842 * jbd2_journal_revoke().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003843 *
3844 * That's easy if it's exclusively part of this
3845 * transaction. But if it's part of the committing
Mingming Caodab291a2006-10-11 01:21:01 -07003846 * transaction then jbd2_journal_forget() will simply
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003847 * brelse() it. That means that if the underlying
Mingming Cao617ba132006-10-11 01:20:53 -07003848 * block is reallocated in ext4_get_block(),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003849 * unmap_underlying_metadata() will find this block
3850 * and will try to get rid of it. damn, damn.
3851 *
3852 * If this block has already been committed to the
3853 * journal, a revoke record will be written. And
3854 * revoke records must be emitted *before* clearing
3855 * this block's bit in the bitmaps.
3856 */
Mingming Cao617ba132006-10-11 01:20:53 -07003857 ext4_forget(handle, 1, inode, bh, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003858
3859 /*
3860 * Everything below this this pointer has been
3861 * released. Now let this top-of-subtree go.
3862 *
3863 * We want the freeing of this indirect block to be
3864 * atomic in the journal with the updating of the
3865 * bitmap block which owns it. So make some room in
3866 * the journal.
3867 *
3868 * We zero the parent pointer *after* freeing its
3869 * pointee in the bitmaps, so if extend_transaction()
3870 * for some reason fails to put the bitmap changes and
3871 * the release into the same transaction, recovery
3872 * will merely complain about releasing a free block,
3873 * rather than leaking blocks.
3874 */
Frank Mayhar03901312009-01-07 00:06:22 -05003875 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003876 return;
3877 if (try_to_extend_transaction(handle, inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003878 ext4_mark_inode_dirty(handle, inode);
3879 ext4_journal_test_restart(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003880 }
3881
Alex Tomasc9de5602008-01-29 00:19:52 -05003882 ext4_free_blocks(handle, inode, nr, 1, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003883
3884 if (parent_bh) {
3885 /*
3886 * The block which we have just freed is
3887 * pointed to by an indirect block: journal it
3888 */
3889 BUFFER_TRACE(parent_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003890 if (!ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003891 parent_bh)){
3892 *p = 0;
3893 BUFFER_TRACE(parent_bh,
Frank Mayhar03901312009-01-07 00:06:22 -05003894 "call ext4_handle_dirty_metadata");
3895 ext4_handle_dirty_metadata(handle,
3896 inode,
3897 parent_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003898 }
3899 }
3900 }
3901 } else {
3902 /* We have reached the bottom of the tree. */
3903 BUFFER_TRACE(parent_bh, "free data blocks");
Mingming Cao617ba132006-10-11 01:20:53 -07003904 ext4_free_data(handle, inode, parent_bh, first, last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003905 }
3906}
3907
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003908int ext4_can_truncate(struct inode *inode)
3909{
3910 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3911 return 0;
3912 if (S_ISREG(inode->i_mode))
3913 return 1;
3914 if (S_ISDIR(inode->i_mode))
3915 return 1;
3916 if (S_ISLNK(inode->i_mode))
3917 return !ext4_inode_is_fast_symlink(inode);
3918 return 0;
3919}
3920
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003921/*
Mingming Cao617ba132006-10-11 01:20:53 -07003922 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003923 *
Mingming Cao617ba132006-10-11 01:20:53 -07003924 * We block out ext4_get_block() block instantiations across the entire
3925 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003926 * simultaneously on behalf of the same inode.
3927 *
3928 * As we work through the truncate and commmit bits of it to the journal there
3929 * is one core, guiding principle: the file's tree must always be consistent on
3930 * disk. We must be able to restart the truncate after a crash.
3931 *
3932 * The file's tree may be transiently inconsistent in memory (although it
3933 * probably isn't), but whenever we close off and commit a journal transaction,
3934 * the contents of (the filesystem + the journal) must be consistent and
3935 * restartable. It's pretty simple, really: bottom up, right to left (although
3936 * left-to-right works OK too).
3937 *
3938 * Note that at recovery time, journal replay occurs *before* the restart of
3939 * truncate against the orphan inode list.
3940 *
3941 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07003942 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003943 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07003944 * ext4_truncate() to have another go. So there will be instantiated blocks
3945 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003946 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07003947 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003948 */
Mingming Cao617ba132006-10-11 01:20:53 -07003949void ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003950{
3951 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -07003952 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003953 __le32 *i_data = ei->i_data;
Mingming Cao617ba132006-10-11 01:20:53 -07003954 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003955 struct address_space *mapping = inode->i_mapping;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003956 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003957 Indirect chain[4];
3958 Indirect *partial;
3959 __le32 nr = 0;
3960 int n;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003961 ext4_lblk_t last_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003962 unsigned blocksize = inode->i_sb->s_blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003963
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003964 if (!ext4_can_truncate(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003965 return;
3966
Theodore Ts'o0eab9282009-06-09 09:54:40 -04003967 if (ei->i_disksize && inode->i_size == 0 &&
3968 !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o7d8f9f72009-02-24 08:21:14 -05003969 ei->i_state |= EXT4_STATE_DA_ALLOC_CLOSE;
3970
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003971 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
Jan Karacf108bc2008-07-11 19:27:31 -04003972 ext4_ext_truncate(inode);
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003973 return;
3974 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003975
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003976 handle = start_transaction(inode);
Jan Karacf108bc2008-07-11 19:27:31 -04003977 if (IS_ERR(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003978 return; /* AKPM: return what? */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003979
3980 last_block = (inode->i_size + blocksize-1)
Mingming Cao617ba132006-10-11 01:20:53 -07003981 >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003982
Jan Karacf108bc2008-07-11 19:27:31 -04003983 if (inode->i_size & (blocksize - 1))
3984 if (ext4_block_truncate_page(handle, mapping, inode->i_size))
3985 goto out_stop;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003986
Mingming Cao617ba132006-10-11 01:20:53 -07003987 n = ext4_block_to_path(inode, last_block, offsets, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003988 if (n == 0)
3989 goto out_stop; /* error */
3990
3991 /*
3992 * OK. This truncate is going to happen. We add the inode to the
3993 * orphan list, so that if this truncate spans multiple transactions,
3994 * and we crash, we will resume the truncate when the filesystem
3995 * recovers. It also marks the inode dirty, to catch the new size.
3996 *
3997 * Implication: the file must always be in a sane, consistent
3998 * truncatable state while each transaction commits.
3999 */
Mingming Cao617ba132006-10-11 01:20:53 -07004000 if (ext4_orphan_add(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004001 goto out_stop;
4002
4003 /*
Mingming Cao632eaea2008-07-11 19:27:31 -04004004 * From here we block out all ext4_get_block() callers who want to
4005 * modify the block allocation tree.
4006 */
4007 down_write(&ei->i_data_sem);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04004008
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004009 ext4_discard_preallocations(inode);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04004010
Mingming Cao632eaea2008-07-11 19:27:31 -04004011 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004012 * The orphan list entry will now protect us from any crash which
4013 * occurs before the truncate completes, so it is now safe to propagate
4014 * the new, shorter inode size (held for now in i_size) into the
4015 * on-disk inode. We do this via i_disksize, which is the value which
Mingming Cao617ba132006-10-11 01:20:53 -07004016 * ext4 *really* writes onto the disk inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004017 */
4018 ei->i_disksize = inode->i_size;
4019
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004020 if (n == 1) { /* direct blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07004021 ext4_free_data(handle, inode, NULL, i_data+offsets[0],
4022 i_data + EXT4_NDIR_BLOCKS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004023 goto do_indirects;
4024 }
4025
Mingming Cao617ba132006-10-11 01:20:53 -07004026 partial = ext4_find_shared(inode, n, offsets, chain, &nr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004027 /* Kill the top of shared branch (not detached) */
4028 if (nr) {
4029 if (partial == chain) {
4030 /* Shared branch grows from the inode */
Mingming Cao617ba132006-10-11 01:20:53 -07004031 ext4_free_branches(handle, inode, NULL,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004032 &nr, &nr+1, (chain+n-1) - partial);
4033 *partial->p = 0;
4034 /*
4035 * We mark the inode dirty prior to restart,
4036 * and prior to stop. No need for it here.
4037 */
4038 } else {
4039 /* Shared branch grows from an indirect block */
4040 BUFFER_TRACE(partial->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07004041 ext4_free_branches(handle, inode, partial->bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004042 partial->p,
4043 partial->p+1, (chain+n-1) - partial);
4044 }
4045 }
4046 /* Clear the ends of indirect blocks on the shared branch */
4047 while (partial > chain) {
Mingming Cao617ba132006-10-11 01:20:53 -07004048 ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004049 (__le32*)partial->bh->b_data+addr_per_block,
4050 (chain+n-1) - partial);
4051 BUFFER_TRACE(partial->bh, "call brelse");
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004052 brelse(partial->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004053 partial--;
4054 }
4055do_indirects:
4056 /* Kill the remaining (whole) subtrees */
4057 switch (offsets[0]) {
4058 default:
Mingming Cao617ba132006-10-11 01:20:53 -07004059 nr = i_data[EXT4_IND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004060 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004061 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
4062 i_data[EXT4_IND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004063 }
Mingming Cao617ba132006-10-11 01:20:53 -07004064 case EXT4_IND_BLOCK:
4065 nr = i_data[EXT4_DIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004066 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004067 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
4068 i_data[EXT4_DIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004069 }
Mingming Cao617ba132006-10-11 01:20:53 -07004070 case EXT4_DIND_BLOCK:
4071 nr = i_data[EXT4_TIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004072 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004073 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
4074 i_data[EXT4_TIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004075 }
Mingming Cao617ba132006-10-11 01:20:53 -07004076 case EXT4_TIND_BLOCK:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004077 ;
4078 }
4079
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004080 up_write(&ei->i_data_sem);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004081 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07004082 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004083
4084 /*
4085 * In a multi-transaction truncate, we only make the final transaction
4086 * synchronous
4087 */
4088 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05004089 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004090out_stop:
4091 /*
4092 * If this was a simple ftruncate(), and the file will remain alive
4093 * then we need to clear up the orphan record which we created above.
4094 * However, if this was a real unlink then we were called by
Mingming Cao617ba132006-10-11 01:20:53 -07004095 * ext4_delete_inode(), and we allow that function to clean up the
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004096 * orphan info for us.
4097 */
4098 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07004099 ext4_orphan_del(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004100
Mingming Cao617ba132006-10-11 01:20:53 -07004101 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004102}
4103
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004104/*
Mingming Cao617ba132006-10-11 01:20:53 -07004105 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004106 * underlying buffer_head on success. If 'in_mem' is true, we have all
4107 * data in memory that is needed to recreate the on-disk version of this
4108 * inode.
4109 */
Mingming Cao617ba132006-10-11 01:20:53 -07004110static int __ext4_get_inode_loc(struct inode *inode,
4111 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004112{
Theodore Ts'o240799c2008-10-09 23:53:47 -04004113 struct ext4_group_desc *gdp;
4114 struct buffer_head *bh;
4115 struct super_block *sb = inode->i_sb;
4116 ext4_fsblk_t block;
4117 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004118
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004119 iloc->bh = NULL;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004120 if (!ext4_valid_inum(sb, inode->i_ino))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004121 return -EIO;
4122
Theodore Ts'o240799c2008-10-09 23:53:47 -04004123 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
4124 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
4125 if (!gdp)
4126 return -EIO;
4127
4128 /*
4129 * Figure out the offset within the block group inode table
4130 */
4131 inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb));
4132 inode_offset = ((inode->i_ino - 1) %
4133 EXT4_INODES_PER_GROUP(sb));
4134 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4135 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4136
4137 bh = sb_getblk(sb, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004138 if (!bh) {
Theodore Ts'o240799c2008-10-09 23:53:47 -04004139 ext4_error(sb, "ext4_get_inode_loc", "unable to read "
4140 "inode block - inode=%lu, block=%llu",
4141 inode->i_ino, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004142 return -EIO;
4143 }
4144 if (!buffer_uptodate(bh)) {
4145 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04004146
4147 /*
4148 * If the buffer has the write error flag, we have failed
4149 * to write out another inode in the same block. In this
4150 * case, we don't have to read the block because we may
4151 * read the old inode data successfully.
4152 */
4153 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
4154 set_buffer_uptodate(bh);
4155
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004156 if (buffer_uptodate(bh)) {
4157 /* someone brought it uptodate while we waited */
4158 unlock_buffer(bh);
4159 goto has_buffer;
4160 }
4161
4162 /*
4163 * If we have all information of the inode in memory and this
4164 * is the only valid inode in the block, we need not read the
4165 * block.
4166 */
4167 if (in_mem) {
4168 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004169 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004170
Theodore Ts'o240799c2008-10-09 23:53:47 -04004171 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004172
4173 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004174 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004175 if (!bitmap_bh)
4176 goto make_io;
4177
4178 /*
4179 * If the inode bitmap isn't in cache then the
4180 * optimisation may end up performing two reads instead
4181 * of one, so skip it.
4182 */
4183 if (!buffer_uptodate(bitmap_bh)) {
4184 brelse(bitmap_bh);
4185 goto make_io;
4186 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04004187 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004188 if (i == inode_offset)
4189 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07004190 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004191 break;
4192 }
4193 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004194 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004195 /* all other inodes are free, so skip I/O */
4196 memset(bh->b_data, 0, bh->b_size);
4197 set_buffer_uptodate(bh);
4198 unlock_buffer(bh);
4199 goto has_buffer;
4200 }
4201 }
4202
4203make_io:
4204 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04004205 * If we need to do any I/O, try to pre-readahead extra
4206 * blocks from the inode table.
4207 */
4208 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4209 ext4_fsblk_t b, end, table;
4210 unsigned num;
4211
4212 table = ext4_inode_table(sb, gdp);
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004213 /* s_inode_readahead_blks is always a power of 2 */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004214 b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
4215 if (table > b)
4216 b = table;
4217 end = b + EXT4_SB(sb)->s_inode_readahead_blks;
4218 num = EXT4_INODES_PER_GROUP(sb);
4219 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4220 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004221 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004222 table += num / inodes_per_block;
4223 if (end > table)
4224 end = table;
4225 while (b <= end)
4226 sb_breadahead(sb, b++);
4227 }
4228
4229 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004230 * There are other valid inodes in the buffer, this inode
4231 * has in-inode xattrs, or we don't have this inode in memory.
4232 * Read the block from disk.
4233 */
4234 get_bh(bh);
4235 bh->b_end_io = end_buffer_read_sync;
4236 submit_bh(READ_META, bh);
4237 wait_on_buffer(bh);
4238 if (!buffer_uptodate(bh)) {
Theodore Ts'o240799c2008-10-09 23:53:47 -04004239 ext4_error(sb, __func__,
4240 "unable to read inode block - inode=%lu, "
4241 "block=%llu", inode->i_ino, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004242 brelse(bh);
4243 return -EIO;
4244 }
4245 }
4246has_buffer:
4247 iloc->bh = bh;
4248 return 0;
4249}
4250
Mingming Cao617ba132006-10-11 01:20:53 -07004251int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004252{
4253 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07004254 return __ext4_get_inode_loc(inode, iloc,
4255 !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004256}
4257
Mingming Cao617ba132006-10-11 01:20:53 -07004258void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004259{
Mingming Cao617ba132006-10-11 01:20:53 -07004260 unsigned int flags = EXT4_I(inode)->i_flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004261
4262 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
Mingming Cao617ba132006-10-11 01:20:53 -07004263 if (flags & EXT4_SYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004264 inode->i_flags |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07004265 if (flags & EXT4_APPEND_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004266 inode->i_flags |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07004267 if (flags & EXT4_IMMUTABLE_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004268 inode->i_flags |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07004269 if (flags & EXT4_NOATIME_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004270 inode->i_flags |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004271 if (flags & EXT4_DIRSYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004272 inode->i_flags |= S_DIRSYNC;
4273}
4274
Jan Karaff9ddf72007-07-18 09:24:20 -04004275/* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4276void ext4_get_inode_flags(struct ext4_inode_info *ei)
4277{
4278 unsigned int flags = ei->vfs_inode.i_flags;
4279
4280 ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
4281 EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL);
4282 if (flags & S_SYNC)
4283 ei->i_flags |= EXT4_SYNC_FL;
4284 if (flags & S_APPEND)
4285 ei->i_flags |= EXT4_APPEND_FL;
4286 if (flags & S_IMMUTABLE)
4287 ei->i_flags |= EXT4_IMMUTABLE_FL;
4288 if (flags & S_NOATIME)
4289 ei->i_flags |= EXT4_NOATIME_FL;
4290 if (flags & S_DIRSYNC)
4291 ei->i_flags |= EXT4_DIRSYNC_FL;
4292}
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004293
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004294static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004295 struct ext4_inode_info *ei)
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004296{
4297 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004298 struct inode *inode = &(ei->vfs_inode);
4299 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004300
4301 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4302 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
4303 /* we are using combined 48 bit field */
4304 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4305 le32_to_cpu(raw_inode->i_blocks_lo);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004306 if (ei->i_flags & EXT4_HUGE_FILE_FL) {
4307 /* i_blocks represent file system block size */
4308 return i_blocks << (inode->i_blkbits - 9);
4309 } else {
4310 return i_blocks;
4311 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004312 } else {
4313 return le32_to_cpu(raw_inode->i_blocks_lo);
4314 }
4315}
Jan Karaff9ddf72007-07-18 09:24:20 -04004316
David Howells1d1fe1e2008-02-07 00:15:37 -08004317struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004318{
Mingming Cao617ba132006-10-11 01:20:53 -07004319 struct ext4_iloc iloc;
4320 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004321 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004322 struct buffer_head *bh;
David Howells1d1fe1e2008-02-07 00:15:37 -08004323 struct inode *inode;
4324 long ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004325 int block;
4326
David Howells1d1fe1e2008-02-07 00:15:37 -08004327 inode = iget_locked(sb, ino);
4328 if (!inode)
4329 return ERR_PTR(-ENOMEM);
4330 if (!(inode->i_state & I_NEW))
4331 return inode;
4332
4333 ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004334
David Howells1d1fe1e2008-02-07 00:15:37 -08004335 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4336 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004337 goto bad_inode;
4338 bh = iloc.bh;
Mingming Cao617ba132006-10-11 01:20:53 -07004339 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004340 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
4341 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4342 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004343 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004344 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4345 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
4346 }
4347 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004348
4349 ei->i_state = 0;
4350 ei->i_dir_start_lookup = 0;
4351 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4352 /* We now have enough fields to check if the inode was active or not.
4353 * This is needed because nfsd might try to access dead inodes
4354 * the test is that same one that e2fsck uses
4355 * NeilBrown 1999oct15
4356 */
4357 if (inode->i_nlink == 0) {
4358 if (inode->i_mode == 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07004359 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004360 /* this inode is deleted */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004361 brelse(bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004362 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004363 goto bad_inode;
4364 }
4365 /* The only unlinked inodes we let through here have
4366 * valid i_mode and are being read by the orphan
4367 * recovery code: that's fine, we're about to complete
4368 * the process of deleting those. */
4369 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004370 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004371 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004372 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Theodore Ts'oa9e81742009-04-24 16:11:18 -04004373 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004374 ei->i_file_acl |=
4375 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004376 inode->i_size = ext4_isize(raw_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004377 ei->i_disksize = inode->i_size;
4378 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4379 ei->i_block_group = iloc.block_group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04004380 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004381 /*
4382 * NOTE! The in-memory inode i_data array is in little-endian order
4383 * even on big-endian machines: we do NOT byteswap the block numbers!
4384 */
Mingming Cao617ba132006-10-11 01:20:53 -07004385 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004386 ei->i_data[block] = raw_inode->i_block[block];
4387 INIT_LIST_HEAD(&ei->i_orphan);
4388
Eric Sandeen0040d982008-02-05 22:36:43 -05004389 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004390 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
Mingming Cao617ba132006-10-11 01:20:53 -07004391 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004392 EXT4_INODE_SIZE(inode->i_sb)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004393 brelse(bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004394 ret = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004395 goto bad_inode;
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004396 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004397 if (ei->i_extra_isize == 0) {
4398 /* The extra space is currently unused. Use it. */
Mingming Cao617ba132006-10-11 01:20:53 -07004399 ei->i_extra_isize = sizeof(struct ext4_inode) -
4400 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004401 } else {
4402 __le32 *magic = (void *)raw_inode +
Mingming Cao617ba132006-10-11 01:20:53 -07004403 EXT4_GOOD_OLD_INODE_SIZE +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004404 ei->i_extra_isize;
Mingming Cao617ba132006-10-11 01:20:53 -07004405 if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC))
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004406 ei->i_state |= EXT4_STATE_XATTR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004407 }
4408 } else
4409 ei->i_extra_isize = 0;
4410
Kalpak Shahef7f3832007-07-18 09:15:20 -04004411 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4412 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4413 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4414 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4415
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004416 inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
4417 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4418 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4419 inode->i_version |=
4420 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4421 }
4422
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004423 ret = 0;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004424 if (ei->i_file_acl &&
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004425 ((ei->i_file_acl <
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004426 (le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) +
4427 EXT4_SB(sb)->s_gdb_count)) ||
4428 (ei->i_file_acl >= ext4_blocks_count(EXT4_SB(sb)->s_es)))) {
4429 ext4_error(sb, __func__,
4430 "bad extended attribute block %llu in inode #%lu",
4431 ei->i_file_acl, inode->i_ino);
4432 ret = -EIO;
4433 goto bad_inode;
4434 } else if (ei->i_flags & EXT4_EXTENTS_FL) {
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004435 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4436 (S_ISLNK(inode->i_mode) &&
4437 !ext4_inode_is_fast_symlink(inode)))
4438 /* Validate extent which is part of inode */
4439 ret = ext4_ext_check_inode(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004440 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004441 (S_ISLNK(inode->i_mode) &&
4442 !ext4_inode_is_fast_symlink(inode))) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004443 /* Validate block references which are part of inode */
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004444 ret = ext4_check_inode_blockref(inode);
4445 }
4446 if (ret) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004447 brelse(bh);
4448 goto bad_inode;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -04004449 }
4450
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004451 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004452 inode->i_op = &ext4_file_inode_operations;
4453 inode->i_fop = &ext4_file_operations;
4454 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004455 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004456 inode->i_op = &ext4_dir_inode_operations;
4457 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004458 } else if (S_ISLNK(inode->i_mode)) {
Duane Griffine83c1392008-12-19 20:47:15 +00004459 if (ext4_inode_is_fast_symlink(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004460 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00004461 nd_terminate_link(ei->i_data, inode->i_size,
4462 sizeof(ei->i_data) - 1);
4463 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004464 inode->i_op = &ext4_symlink_inode_operations;
4465 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004466 }
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004467 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4468 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004469 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004470 if (raw_inode->i_block[0])
4471 init_special_inode(inode, inode->i_mode,
4472 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4473 else
4474 init_special_inode(inode, inode->i_mode,
4475 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004476 } else {
4477 brelse(bh);
4478 ret = -EIO;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004479 ext4_error(inode->i_sb, __func__,
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004480 "bogus i_mode (%o) for inode=%lu",
4481 inode->i_mode, inode->i_ino);
4482 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004483 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004484 brelse(iloc.bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004485 ext4_set_inode_flags(inode);
David Howells1d1fe1e2008-02-07 00:15:37 -08004486 unlock_new_inode(inode);
4487 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004488
4489bad_inode:
David Howells1d1fe1e2008-02-07 00:15:37 -08004490 iget_failed(inode);
4491 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004492}
4493
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004494static int ext4_inode_blocks_set(handle_t *handle,
4495 struct ext4_inode *raw_inode,
4496 struct ext4_inode_info *ei)
4497{
4498 struct inode *inode = &(ei->vfs_inode);
4499 u64 i_blocks = inode->i_blocks;
4500 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004501
4502 if (i_blocks <= ~0U) {
4503 /*
4504 * i_blocks can be represnted in a 32 bit variable
4505 * as multiple of 512 bytes
4506 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004507 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004508 raw_inode->i_blocks_high = 0;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004509 ei->i_flags &= ~EXT4_HUGE_FILE_FL;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004510 return 0;
4511 }
4512 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
4513 return -EFBIG;
4514
4515 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004516 /*
4517 * i_blocks can be represented in a 48 bit variable
4518 * as multiple of 512 bytes
4519 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004520 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004521 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004522 ei->i_flags &= ~EXT4_HUGE_FILE_FL;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004523 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004524 ei->i_flags |= EXT4_HUGE_FILE_FL;
4525 /* i_block is stored in file system block size */
4526 i_blocks = i_blocks >> (inode->i_blkbits - 9);
4527 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4528 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004529 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004530 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004531}
4532
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004533/*
4534 * Post the struct inode info into an on-disk inode location in the
4535 * buffer-cache. This gobbles the caller's reference to the
4536 * buffer_head in the inode location struct.
4537 *
4538 * The caller must have write access to iloc->bh.
4539 */
Mingming Cao617ba132006-10-11 01:20:53 -07004540static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004541 struct inode *inode,
Mingming Cao617ba132006-10-11 01:20:53 -07004542 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004543{
Mingming Cao617ba132006-10-11 01:20:53 -07004544 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4545 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004546 struct buffer_head *bh = iloc->bh;
4547 int err = 0, rc, block;
4548
4549 /* For fields not not tracking in the in-memory inode,
4550 * initialise them to zero for new inodes. */
Mingming Cao617ba132006-10-11 01:20:53 -07004551 if (ei->i_state & EXT4_STATE_NEW)
4552 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004553
Jan Karaff9ddf72007-07-18 09:24:20 -04004554 ext4_get_inode_flags(ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004555 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004556 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004557 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
4558 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
4559/*
4560 * Fix up interoperability with old kernels. Otherwise, old inodes get
4561 * re-used with the upper 16 bits of the uid/gid intact
4562 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004563 if (!ei->i_dtime) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004564 raw_inode->i_uid_high =
4565 cpu_to_le16(high_16_bits(inode->i_uid));
4566 raw_inode->i_gid_high =
4567 cpu_to_le16(high_16_bits(inode->i_gid));
4568 } else {
4569 raw_inode->i_uid_high = 0;
4570 raw_inode->i_gid_high = 0;
4571 }
4572 } else {
4573 raw_inode->i_uid_low =
4574 cpu_to_le16(fs_high2lowuid(inode->i_uid));
4575 raw_inode->i_gid_low =
4576 cpu_to_le16(fs_high2lowgid(inode->i_gid));
4577 raw_inode->i_uid_high = 0;
4578 raw_inode->i_gid_high = 0;
4579 }
4580 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004581
4582 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4583 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4584 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4585 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4586
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004587 if (ext4_inode_blocks_set(handle, raw_inode, ei))
4588 goto out_brelse;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004589 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04004590 /* clear the migrate flag in the raw_inode */
4591 raw_inode->i_flags = cpu_to_le32(ei->i_flags & ~EXT4_EXT_MIGRATE);
Mingming Cao9b8f1f02006-10-11 01:21:13 -07004592 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
4593 cpu_to_le32(EXT4_OS_HURD))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004594 raw_inode->i_file_acl_high =
4595 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004596 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004597 ext4_isize_set(raw_inode, ei->i_disksize);
4598 if (ei->i_disksize > 0x7fffffffULL) {
4599 struct super_block *sb = inode->i_sb;
4600 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
4601 EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
4602 EXT4_SB(sb)->s_es->s_rev_level ==
4603 cpu_to_le32(EXT4_GOOD_OLD_REV)) {
4604 /* If this is the first large file
4605 * created, add a flag to the superblock.
4606 */
4607 err = ext4_journal_get_write_access(handle,
4608 EXT4_SB(sb)->s_sbh);
4609 if (err)
4610 goto out_brelse;
4611 ext4_update_dynamic_rev(sb);
4612 EXT4_SET_RO_COMPAT_FEATURE(sb,
Mingming Cao617ba132006-10-11 01:20:53 -07004613 EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004614 sb->s_dirt = 1;
Frank Mayhar03901312009-01-07 00:06:22 -05004615 ext4_handle_sync(handle);
4616 err = ext4_handle_dirty_metadata(handle, inode,
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004617 EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004618 }
4619 }
4620 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
4621 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4622 if (old_valid_dev(inode->i_rdev)) {
4623 raw_inode->i_block[0] =
4624 cpu_to_le32(old_encode_dev(inode->i_rdev));
4625 raw_inode->i_block[1] = 0;
4626 } else {
4627 raw_inode->i_block[0] = 0;
4628 raw_inode->i_block[1] =
4629 cpu_to_le32(new_encode_dev(inode->i_rdev));
4630 raw_inode->i_block[2] = 0;
4631 }
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004632 } else
4633 for (block = 0; block < EXT4_N_BLOCKS; block++)
4634 raw_inode->i_block[block] = ei->i_data[block];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004635
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004636 raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
4637 if (ei->i_extra_isize) {
4638 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4639 raw_inode->i_version_hi =
4640 cpu_to_le32(inode->i_version >> 32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004641 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004642 }
4643
Frank Mayhar03901312009-01-07 00:06:22 -05004644 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
4645 rc = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004646 if (!err)
4647 err = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07004648 ei->i_state &= ~EXT4_STATE_NEW;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004649
4650out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004651 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004652 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004653 return err;
4654}
4655
4656/*
Mingming Cao617ba132006-10-11 01:20:53 -07004657 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004658 *
4659 * We are called from a few places:
4660 *
4661 * - Within generic_file_write() for O_SYNC files.
4662 * Here, there will be no transaction running. We wait for any running
4663 * trasnaction to commit.
4664 *
4665 * - Within sys_sync(), kupdate and such.
4666 * We wait on commit, if tol to.
4667 *
4668 * - Within prune_icache() (PF_MEMALLOC == true)
4669 * Here we simply return. We can't afford to block kswapd on the
4670 * journal commit.
4671 *
4672 * In all cases it is actually safe for us to return without doing anything,
4673 * because the inode has been copied into a raw inode buffer in
Mingming Cao617ba132006-10-11 01:20:53 -07004674 * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004675 * knfsd.
4676 *
4677 * Note that we are absolutely dependent upon all inode dirtiers doing the
4678 * right thing: they *must* call mark_inode_dirty() after dirtying info in
4679 * which we are interested.
4680 *
4681 * It would be a bug for them to not do this. The code:
4682 *
4683 * mark_inode_dirty(inode)
4684 * stuff();
4685 * inode->i_size = expr;
4686 *
4687 * is in error because a kswapd-driven write_inode() could occur while
4688 * `stuff()' is running, and the new i_size will be lost. Plus the inode
4689 * will no longer be on the superblock's dirty inode list.
4690 */
Mingming Cao617ba132006-10-11 01:20:53 -07004691int ext4_write_inode(struct inode *inode, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004692{
4693 if (current->flags & PF_MEMALLOC)
4694 return 0;
4695
Mingming Cao617ba132006-10-11 01:20:53 -07004696 if (ext4_journal_current_handle()) {
Mingming Caob38bd332007-07-19 01:48:35 -07004697 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004698 dump_stack();
4699 return -EIO;
4700 }
4701
4702 if (!wait)
4703 return 0;
4704
Mingming Cao617ba132006-10-11 01:20:53 -07004705 return ext4_force_commit(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004706}
4707
4708/*
Mingming Cao617ba132006-10-11 01:20:53 -07004709 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004710 *
4711 * Called from notify_change.
4712 *
4713 * We want to trap VFS attempts to truncate the file as soon as
4714 * possible. In particular, we want to make sure that when the VFS
4715 * shrinks i_size, we put the inode on the orphan list and modify
4716 * i_disksize immediately, so that during the subsequent flushing of
4717 * dirty pages and freeing of disk blocks, we can guarantee that any
4718 * commit will leave the blocks being flushed in an unused state on
4719 * disk. (On recovery, the inode will get truncated and the blocks will
4720 * be freed, so we have a strong guarantee that no future commit will
4721 * leave these blocks visible to the user.)
4722 *
Jan Kara678aaf42008-07-11 19:27:31 -04004723 * Another thing we have to assure is that if we are in ordered mode
4724 * and inode is still attached to the committing transaction, we must
4725 * we start writeout of all the dirty pages which are being truncated.
4726 * This way we are sure that all the data written in the previous
4727 * transaction are already on disk (truncate waits for pages under
4728 * writeback).
4729 *
4730 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004731 */
Mingming Cao617ba132006-10-11 01:20:53 -07004732int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004733{
4734 struct inode *inode = dentry->d_inode;
4735 int error, rc = 0;
4736 const unsigned int ia_valid = attr->ia_valid;
4737
4738 error = inode_change_ok(inode, attr);
4739 if (error)
4740 return error;
4741
4742 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
4743 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
4744 handle_t *handle;
4745
4746 /* (user+group)*(old+new) structure, inode write (sb,
4747 * inode block, ? - but truncate inode update has it) */
Mingming Cao617ba132006-10-11 01:20:53 -07004748 handle = ext4_journal_start(inode, 2*(EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)+
4749 EXT4_QUOTA_DEL_BLOCKS(inode->i_sb))+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004750 if (IS_ERR(handle)) {
4751 error = PTR_ERR(handle);
4752 goto err_out;
4753 }
Jan Karaa269eb12009-01-26 17:04:39 +01004754 error = vfs_dq_transfer(inode, attr) ? -EDQUOT : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004755 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07004756 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004757 return error;
4758 }
4759 /* Update corresponding info in inode so that everything is in
4760 * one transaction */
4761 if (attr->ia_valid & ATTR_UID)
4762 inode->i_uid = attr->ia_uid;
4763 if (attr->ia_valid & ATTR_GID)
4764 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07004765 error = ext4_mark_inode_dirty(handle, inode);
4766 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004767 }
4768
Eric Sandeene2b46572008-01-28 23:58:27 -05004769 if (attr->ia_valid & ATTR_SIZE) {
4770 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) {
4771 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4772
4773 if (attr->ia_size > sbi->s_bitmap_maxbytes) {
4774 error = -EFBIG;
4775 goto err_out;
4776 }
4777 }
4778 }
4779
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004780 if (S_ISREG(inode->i_mode) &&
4781 attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
4782 handle_t *handle;
4783
Mingming Cao617ba132006-10-11 01:20:53 -07004784 handle = ext4_journal_start(inode, 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004785 if (IS_ERR(handle)) {
4786 error = PTR_ERR(handle);
4787 goto err_out;
4788 }
4789
Mingming Cao617ba132006-10-11 01:20:53 -07004790 error = ext4_orphan_add(handle, inode);
4791 EXT4_I(inode)->i_disksize = attr->ia_size;
4792 rc = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004793 if (!error)
4794 error = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07004795 ext4_journal_stop(handle);
Jan Kara678aaf42008-07-11 19:27:31 -04004796
4797 if (ext4_should_order_data(inode)) {
4798 error = ext4_begin_ordered_truncate(inode,
4799 attr->ia_size);
4800 if (error) {
4801 /* Do as much error cleanup as possible */
4802 handle = ext4_journal_start(inode, 3);
4803 if (IS_ERR(handle)) {
4804 ext4_orphan_del(NULL, inode);
4805 goto err_out;
4806 }
4807 ext4_orphan_del(handle, inode);
4808 ext4_journal_stop(handle);
4809 goto err_out;
4810 }
4811 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004812 }
4813
4814 rc = inode_setattr(inode, attr);
4815
Mingming Cao617ba132006-10-11 01:20:53 -07004816 /* If inode_setattr's call to ext4_truncate failed to get a
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004817 * transaction handle at all, we need to clean up the in-core
4818 * orphan list manually. */
4819 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07004820 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004821
4822 if (!rc && (ia_valid & ATTR_MODE))
Mingming Cao617ba132006-10-11 01:20:53 -07004823 rc = ext4_acl_chmod(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004824
4825err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07004826 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004827 if (!error)
4828 error = rc;
4829 return error;
4830}
4831
Mingming Cao3e3398a2008-07-11 19:27:31 -04004832int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
4833 struct kstat *stat)
4834{
4835 struct inode *inode;
4836 unsigned long delalloc_blocks;
4837
4838 inode = dentry->d_inode;
4839 generic_fillattr(inode, stat);
4840
4841 /*
4842 * We can't update i_blocks if the block allocation is delayed
4843 * otherwise in the case of system crash before the real block
4844 * allocation is done, we will have i_blocks inconsistent with
4845 * on-disk file blocks.
4846 * We always keep i_blocks updated together with real
4847 * allocation. But to not confuse with user, stat
4848 * will return the blocks that include the delayed allocation
4849 * blocks for this file.
4850 */
4851 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
4852 delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks;
4853 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
4854
4855 stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
4856 return 0;
4857}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004858
Mingming Caoa02908f2008-08-19 22:16:07 -04004859static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks,
4860 int chunk)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004861{
Mingming Caoa02908f2008-08-19 22:16:07 -04004862 int indirects;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004863
Mingming Caoa02908f2008-08-19 22:16:07 -04004864 /* if nrblocks are contiguous */
4865 if (chunk) {
4866 /*
4867 * With N contiguous data blocks, it need at most
4868 * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks
4869 * 2 dindirect blocks
4870 * 1 tindirect block
4871 */
4872 indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb);
4873 return indirects + 3;
4874 }
4875 /*
4876 * if nrblocks are not contiguous, worse case, each block touch
4877 * a indirect block, and each indirect block touch a double indirect
4878 * block, plus a triple indirect block
4879 */
4880 indirects = nrblocks * 2 + 1;
4881 return indirects;
4882}
Alex Tomasa86c6182006-10-11 01:21:03 -07004883
Mingming Caoa02908f2008-08-19 22:16:07 -04004884static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4885{
4886 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
Theodore Ts'oac51d832008-11-06 16:49:36 -05004887 return ext4_indirect_trans_blocks(inode, nrblocks, chunk);
4888 return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
Mingming Caoa02908f2008-08-19 22:16:07 -04004889}
Theodore Ts'oac51d832008-11-06 16:49:36 -05004890
Mingming Caoa02908f2008-08-19 22:16:07 -04004891/*
4892 * Account for index blocks, block groups bitmaps and block group
4893 * descriptor blocks if modify datablocks and index blocks
4894 * worse case, the indexs blocks spread over different block groups
4895 *
4896 * If datablocks are discontiguous, they are possible to spread over
4897 * different block groups too. If they are contiugous, with flexbg,
4898 * they could still across block group boundary.
4899 *
4900 * Also account for superblock, inode, quota and xattr blocks
4901 */
4902int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4903{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004904 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
4905 int gdpblocks;
Mingming Caoa02908f2008-08-19 22:16:07 -04004906 int idxblocks;
4907 int ret = 0;
4908
4909 /*
4910 * How many index blocks need to touch to modify nrblocks?
4911 * The "Chunk" flag indicating whether the nrblocks is
4912 * physically contiguous on disk
4913 *
4914 * For Direct IO and fallocate, they calls get_block to allocate
4915 * one single extent at a time, so they could set the "Chunk" flag
4916 */
4917 idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
4918
4919 ret = idxblocks;
4920
4921 /*
4922 * Now let's see how many group bitmaps and group descriptors need
4923 * to account
4924 */
4925 groups = idxblocks;
4926 if (chunk)
4927 groups += 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004928 else
Mingming Caoa02908f2008-08-19 22:16:07 -04004929 groups += nrblocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004930
Mingming Caoa02908f2008-08-19 22:16:07 -04004931 gdpblocks = groups;
Theodore Ts'o8df96752009-05-01 08:50:38 -04004932 if (groups > ngroups)
4933 groups = ngroups;
Mingming Caoa02908f2008-08-19 22:16:07 -04004934 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
4935 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
4936
4937 /* bitmaps and block group descriptor blocks */
4938 ret += groups + gdpblocks;
4939
4940 /* Blocks for super block, inode, quota and xattr blocks */
4941 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004942
4943 return ret;
4944}
4945
4946/*
Mingming Caoa02908f2008-08-19 22:16:07 -04004947 * Calulate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04004948 * the modification of a single pages into a single transaction,
4949 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04004950 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004951 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04004952 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004953 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04004954 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04004955 */
4956int ext4_writepage_trans_blocks(struct inode *inode)
4957{
4958 int bpp = ext4_journal_blocks_per_page(inode);
4959 int ret;
4960
4961 ret = ext4_meta_trans_blocks(inode, bpp, 0);
4962
4963 /* Account for data blocks for journalled mode */
4964 if (ext4_should_journal_data(inode))
4965 ret += bpp;
4966 return ret;
4967}
Mingming Caof3bd1f32008-08-19 22:16:03 -04004968
4969/*
4970 * Calculate the journal credits for a chunk of data modification.
4971 *
4972 * This is called from DIO, fallocate or whoever calling
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04004973 * ext4_get_blocks() to map/allocate a chunk of contigous disk blocks.
Mingming Caof3bd1f32008-08-19 22:16:03 -04004974 *
4975 * journal buffers for data blocks are not included here, as DIO
4976 * and fallocate do no need to journal data buffers.
4977 */
4978int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
4979{
4980 return ext4_meta_trans_blocks(inode, nrblocks, 1);
4981}
4982
Mingming Caoa02908f2008-08-19 22:16:07 -04004983/*
Mingming Cao617ba132006-10-11 01:20:53 -07004984 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004985 * Give this, we know that the caller already has write access to iloc->bh.
4986 */
Mingming Cao617ba132006-10-11 01:20:53 -07004987int ext4_mark_iloc_dirty(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004988 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004989{
4990 int err = 0;
4991
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004992 if (test_opt(inode->i_sb, I_VERSION))
4993 inode_inc_iversion(inode);
4994
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004995 /* the do_update_inode consumes one bh->b_count */
4996 get_bh(iloc->bh);
4997
Mingming Caodab291a2006-10-11 01:21:01 -07004998 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Mingming Cao617ba132006-10-11 01:20:53 -07004999 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005000 put_bh(iloc->bh);
5001 return err;
5002}
5003
5004/*
5005 * On success, We end up with an outstanding reference count against
5006 * iloc->bh. This _must_ be cleaned up later.
5007 */
5008
5009int
Mingming Cao617ba132006-10-11 01:20:53 -07005010ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
5011 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005012{
Frank Mayhar03901312009-01-07 00:06:22 -05005013 int err;
5014
5015 err = ext4_get_inode_loc(inode, iloc);
5016 if (!err) {
5017 BUFFER_TRACE(iloc->bh, "get_write_access");
5018 err = ext4_journal_get_write_access(handle, iloc->bh);
5019 if (err) {
5020 brelse(iloc->bh);
5021 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005022 }
5023 }
Mingming Cao617ba132006-10-11 01:20:53 -07005024 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005025 return err;
5026}
5027
5028/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005029 * Expand an inode by new_extra_isize bytes.
5030 * Returns 0 on success or negative error number on failure.
5031 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05005032static int ext4_expand_extra_isize(struct inode *inode,
5033 unsigned int new_extra_isize,
5034 struct ext4_iloc iloc,
5035 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005036{
5037 struct ext4_inode *raw_inode;
5038 struct ext4_xattr_ibody_header *header;
5039 struct ext4_xattr_entry *entry;
5040
5041 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
5042 return 0;
5043
5044 raw_inode = ext4_raw_inode(&iloc);
5045
5046 header = IHDR(inode, raw_inode);
5047 entry = IFIRST(header);
5048
5049 /* No extended attributes present */
5050 if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) ||
5051 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
5052 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
5053 new_extra_isize);
5054 EXT4_I(inode)->i_extra_isize = new_extra_isize;
5055 return 0;
5056 }
5057
5058 /* try to expand with EAs present */
5059 return ext4_expand_extra_isize_ea(inode, new_extra_isize,
5060 raw_inode, handle);
5061}
5062
5063/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005064 * What we do here is to mark the in-core inode as clean with respect to inode
5065 * dirtiness (it may still be data-dirty).
5066 * This means that the in-core inode may be reaped by prune_icache
5067 * without having to perform any I/O. This is a very good thing,
5068 * because *any* task may call prune_icache - even ones which
5069 * have a transaction open against a different journal.
5070 *
5071 * Is this cheating? Not really. Sure, we haven't written the
5072 * inode out, but prune_icache isn't a user-visible syncing function.
5073 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5074 * we start and wait on commits.
5075 *
5076 * Is this efficient/effective? Well, we're being nice to the system
5077 * by cleaning up our inodes proactively so they can be reaped
5078 * without I/O. But we are potentially leaving up to five seconds'
5079 * worth of inodes floating about which prune_icache wants us to
5080 * write out. One way to fix that would be to get prune_icache()
5081 * to do a write_super() to free up some memory. It has the desired
5082 * effect.
5083 */
Mingming Cao617ba132006-10-11 01:20:53 -07005084int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005085{
Mingming Cao617ba132006-10-11 01:20:53 -07005086 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005087 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5088 static unsigned int mnt_count;
5089 int err, ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005090
5091 might_sleep();
Mingming Cao617ba132006-10-11 01:20:53 -07005092 err = ext4_reserve_inode_write(handle, inode, &iloc);
Frank Mayhar03901312009-01-07 00:06:22 -05005093 if (ext4_handle_valid(handle) &&
5094 EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005095 !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) {
5096 /*
5097 * We need extra buffer credits since we may write into EA block
5098 * with this same handle. If journal_extend fails, then it will
5099 * only result in a minor loss of functionality for that inode.
5100 * If this is felt to be critical, then e2fsck should be run to
5101 * force a large enough s_min_extra_isize.
5102 */
5103 if ((jbd2_journal_extend(handle,
5104 EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
5105 ret = ext4_expand_extra_isize(inode,
5106 sbi->s_want_extra_isize,
5107 iloc, handle);
5108 if (ret) {
5109 EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND;
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005110 if (mnt_count !=
5111 le16_to_cpu(sbi->s_es->s_mnt_count)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04005112 ext4_warning(inode->i_sb, __func__,
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005113 "Unable to expand inode %lu. Delete"
5114 " some EAs or run e2fsck.",
5115 inode->i_ino);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005116 mnt_count =
5117 le16_to_cpu(sbi->s_es->s_mnt_count);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005118 }
5119 }
5120 }
5121 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005122 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07005123 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005124 return err;
5125}
5126
5127/*
Mingming Cao617ba132006-10-11 01:20:53 -07005128 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005129 *
5130 * We're really interested in the case where a file is being extended.
5131 * i_size has been changed by generic_commit_write() and we thus need
5132 * to include the updated inode in the current transaction.
5133 *
Jan Karaa269eb12009-01-26 17:04:39 +01005134 * Also, vfs_dq_alloc_block() will always dirty the inode when blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005135 * are allocated to the file.
5136 *
5137 * If the inode is marked synchronous, we don't honour that here - doing
5138 * so would cause a commit on atime updates, which we don't bother doing.
5139 * We handle synchronous inodes at the highest possible level.
5140 */
Mingming Cao617ba132006-10-11 01:20:53 -07005141void ext4_dirty_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005142{
Mingming Cao617ba132006-10-11 01:20:53 -07005143 handle_t *current_handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005144 handle_t *handle;
5145
Frank Mayhar03901312009-01-07 00:06:22 -05005146 if (!ext4_handle_valid(current_handle)) {
5147 ext4_mark_inode_dirty(current_handle, inode);
5148 return;
5149 }
5150
Mingming Cao617ba132006-10-11 01:20:53 -07005151 handle = ext4_journal_start(inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005152 if (IS_ERR(handle))
5153 goto out;
5154 if (current_handle &&
5155 current_handle->h_transaction != handle->h_transaction) {
5156 /* This task has a transaction open against a different fs */
5157 printk(KERN_EMERG "%s: transactions do not match!\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04005158 __func__);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005159 } else {
5160 jbd_debug(5, "marking dirty. outer handle=%p\n",
5161 current_handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005162 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005163 }
Mingming Cao617ba132006-10-11 01:20:53 -07005164 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005165out:
5166 return;
5167}
5168
5169#if 0
5170/*
5171 * Bind an inode's backing buffer_head into this transaction, to prevent
5172 * it from being flushed to disk early. Unlike
Mingming Cao617ba132006-10-11 01:20:53 -07005173 * ext4_reserve_inode_write, this leaves behind no bh reference and
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005174 * returns no iloc structure, so the caller needs to repeat the iloc
5175 * lookup to mark the inode dirty later.
5176 */
Mingming Cao617ba132006-10-11 01:20:53 -07005177static int ext4_pin_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005178{
Mingming Cao617ba132006-10-11 01:20:53 -07005179 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005180
5181 int err = 0;
5182 if (handle) {
Mingming Cao617ba132006-10-11 01:20:53 -07005183 err = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005184 if (!err) {
5185 BUFFER_TRACE(iloc.bh, "get_write_access");
Mingming Caodab291a2006-10-11 01:21:01 -07005186 err = jbd2_journal_get_write_access(handle, iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005187 if (!err)
Frank Mayhar03901312009-01-07 00:06:22 -05005188 err = ext4_handle_dirty_metadata(handle,
5189 inode,
5190 iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005191 brelse(iloc.bh);
5192 }
5193 }
Mingming Cao617ba132006-10-11 01:20:53 -07005194 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005195 return err;
5196}
5197#endif
5198
Mingming Cao617ba132006-10-11 01:20:53 -07005199int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005200{
5201 journal_t *journal;
5202 handle_t *handle;
5203 int err;
5204
5205 /*
5206 * We have to be very careful here: changing a data block's
5207 * journaling status dynamically is dangerous. If we write a
5208 * data block to the journal, change the status and then delete
5209 * that block, we risk forgetting to revoke the old log record
5210 * from the journal and so a subsequent replay can corrupt data.
5211 * So, first we make sure that the journal is empty and that
5212 * nobody is changing anything.
5213 */
5214
Mingming Cao617ba132006-10-11 01:20:53 -07005215 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005216 if (!journal)
5217 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04005218 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005219 return -EROFS;
5220
Mingming Caodab291a2006-10-11 01:21:01 -07005221 jbd2_journal_lock_updates(journal);
5222 jbd2_journal_flush(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005223
5224 /*
5225 * OK, there are no updates running now, and all cached data is
5226 * synced to disk. We are now in a completely consistent state
5227 * which doesn't have anything in the journal, and we know that
5228 * no filesystem updates are running, so it is safe to modify
5229 * the inode's in-core data-journaling state flag now.
5230 */
5231
5232 if (val)
Mingming Cao617ba132006-10-11 01:20:53 -07005233 EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005234 else
Mingming Cao617ba132006-10-11 01:20:53 -07005235 EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL;
5236 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005237
Mingming Caodab291a2006-10-11 01:21:01 -07005238 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005239
5240 /* Finally we can mark the inode as dirty. */
5241
Mingming Cao617ba132006-10-11 01:20:53 -07005242 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005243 if (IS_ERR(handle))
5244 return PTR_ERR(handle);
5245
Mingming Cao617ba132006-10-11 01:20:53 -07005246 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005247 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005248 ext4_journal_stop(handle);
5249 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005250
5251 return err;
5252}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005253
5254static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5255{
5256 return !buffer_mapped(bh);
5257}
5258
Nick Pigginc2ec1752009-03-31 15:23:21 -07005259int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005260{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005261 struct page *page = vmf->page;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005262 loff_t size;
5263 unsigned long len;
5264 int ret = -EINVAL;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005265 void *fsdata;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005266 struct file *file = vma->vm_file;
5267 struct inode *inode = file->f_path.dentry->d_inode;
5268 struct address_space *mapping = inode->i_mapping;
5269
5270 /*
5271 * Get i_alloc_sem to stop truncates messing with the inode. We cannot
5272 * get i_mutex because we are already holding mmap_sem.
5273 */
5274 down_read(&inode->i_alloc_sem);
5275 size = i_size_read(inode);
5276 if (page->mapping != mapping || size <= page_offset(page)
5277 || !PageUptodate(page)) {
5278 /* page got truncated from under us? */
5279 goto out_unlock;
5280 }
5281 ret = 0;
5282 if (PageMappedToDisk(page))
5283 goto out_unlock;
5284
5285 if (page->index == size >> PAGE_CACHE_SHIFT)
5286 len = size & ~PAGE_CACHE_MASK;
5287 else
5288 len = PAGE_CACHE_SIZE;
5289
5290 if (page_has_buffers(page)) {
5291 /* return if we have all the buffers mapped */
5292 if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
5293 ext4_bh_unmapped))
5294 goto out_unlock;
5295 }
5296 /*
5297 * OK, we need to fill the hole... Do write_begin write_end
5298 * to do block allocation/reservation.We are not holding
5299 * inode.i__mutex here. That allow * parallel write_begin,
5300 * write_end call. lock_page prevent this from happening
5301 * on the same page though
5302 */
5303 ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005304 len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005305 if (ret < 0)
5306 goto out_unlock;
5307 ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005308 len, len, page, fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005309 if (ret < 0)
5310 goto out_unlock;
5311 ret = 0;
5312out_unlock:
Nick Pigginc2ec1752009-03-31 15:23:21 -07005313 if (ret)
5314 ret = VM_FAULT_SIGBUS;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005315 up_read(&inode->i_alloc_sem);
5316 return ret;
5317}