blob: 9a4c929b16dccecb848e01ca5f9d489af6dd677f [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
Jan Kara678aaf42008-07-11 19:27:31 -040050static inline int ext4_begin_ordered_truncate(struct inode *inode,
51 loff_t new_size)
52{
Jan Kara7f5aa212009-02-10 11:15:34 -050053 return jbd2_journal_begin_ordered_truncate(
54 EXT4_SB(inode->i_sb)->s_journal,
55 &EXT4_I(inode)->jinode,
56 new_size);
Jan Kara678aaf42008-07-11 19:27:31 -040057}
58
Alex Tomas64769242008-07-11 19:27:31 -040059static void ext4_invalidatepage(struct page *page, unsigned long offset);
60
Dave Kleikampac27a0e2006-10-11 01:20:50 -070061/*
62 * Test whether an inode is a fast symlink.
63 */
Mingming Cao617ba132006-10-11 01:20:53 -070064static int ext4_inode_is_fast_symlink(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070065{
Mingming Cao617ba132006-10-11 01:20:53 -070066 int ea_blocks = EXT4_I(inode)->i_file_acl ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -070067 (inode->i_sb->s_blocksize >> 9) : 0;
68
69 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
70}
71
72/*
Mingming Cao617ba132006-10-11 01:20:53 -070073 * The ext4 forget function must perform a revoke if we are freeing data
Dave Kleikampac27a0e2006-10-11 01:20:50 -070074 * which has been journaled. Metadata (eg. indirect blocks) must be
75 * revoked in all cases.
76 *
77 * "bh" may be NULL: a metadata block may have been freed from memory
78 * but there may still be a record of it in the journal, and that record
79 * still needs to be revoked.
Frank Mayhar03901312009-01-07 00:06:22 -050080 *
Curt Wohlgemuthe6b5d302009-07-13 09:07:20 -040081 * If the handle isn't valid we're not journaling, but we still need to
82 * call into ext4_journal_revoke() to put the buffer head.
Dave Kleikampac27a0e2006-10-11 01:20:50 -070083 */
Mingming Cao617ba132006-10-11 01:20:53 -070084int ext4_forget(handle_t *handle, int is_metadata, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -040085 struct buffer_head *bh, ext4_fsblk_t blocknr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070086{
87 int err;
88
89 might_sleep();
90
91 BUFFER_TRACE(bh, "enter");
92
93 jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
Theodore Ts'o7f4520c2009-06-13 10:09:41 -040094 "data mode %x\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -070095 bh, is_metadata, inode->i_mode,
96 test_opt(inode->i_sb, DATA_FLAGS));
97
98 /* Never use the revoke function if we are doing full data
99 * journaling: there is no need to, and a V1 superblock won't
100 * support it. Otherwise, only skip the revoke on un-journaled
101 * data blocks. */
102
Mingming Cao617ba132006-10-11 01:20:53 -0700103 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
104 (!is_metadata && !ext4_should_journal_data(inode))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700105 if (bh) {
Mingming Caodab291a2006-10-11 01:21:01 -0700106 BUFFER_TRACE(bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700107 return ext4_journal_forget(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700108 }
109 return 0;
110 }
111
112 /*
113 * data!=journal && (is_metadata || should_journal_data(inode))
114 */
Mingming Cao617ba132006-10-11 01:20:53 -0700115 BUFFER_TRACE(bh, "call ext4_journal_revoke");
116 err = ext4_journal_revoke(handle, blocknr, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700117 if (err)
Harvey Harrison46e665e2008-04-17 10:38:59 -0400118 ext4_abort(inode->i_sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700119 "error %d when attempting revoke", err);
120 BUFFER_TRACE(bh, "exit");
121 return err;
122}
123
124/*
125 * Work out how many blocks we need to proceed with the next chunk of a
126 * truncate transaction.
127 */
128static unsigned long blocks_for_truncate(struct inode *inode)
129{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500130 ext4_lblk_t needed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700131
132 needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
133
134 /* Give ourselves just enough room to cope with inodes in which
135 * i_blocks is corrupt: we've seen disk corruptions in the past
136 * which resulted in random data in an inode which looked enough
Mingming Cao617ba132006-10-11 01:20:53 -0700137 * like a regular file for ext4 to try to delete it. Things
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700138 * will go a bit crazy if that happens, but at least we should
139 * try not to panic the whole kernel. */
140 if (needed < 2)
141 needed = 2;
142
143 /* But we need to bound the transaction so we don't overflow the
144 * journal. */
Mingming Cao617ba132006-10-11 01:20:53 -0700145 if (needed > EXT4_MAX_TRANS_DATA)
146 needed = EXT4_MAX_TRANS_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700147
Mingming Cao617ba132006-10-11 01:20:53 -0700148 return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700149}
150
151/*
152 * Truncate transactions can be complex and absolutely huge. So we need to
153 * be able to restart the transaction at a conventient checkpoint to make
154 * sure we don't overflow the journal.
155 *
156 * start_transaction gets us a new handle for a truncate transaction,
157 * and extend_transaction tries to extend the existing one a bit. If
158 * extend fails, we need to propagate the failure up and restart the
159 * transaction in the top-level truncate loop. --sct
160 */
161static handle_t *start_transaction(struct inode *inode)
162{
163 handle_t *result;
164
Mingming Cao617ba132006-10-11 01:20:53 -0700165 result = ext4_journal_start(inode, blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700166 if (!IS_ERR(result))
167 return result;
168
Mingming Cao617ba132006-10-11 01:20:53 -0700169 ext4_std_error(inode->i_sb, PTR_ERR(result));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700170 return result;
171}
172
173/*
174 * Try to extend this transaction for the purposes of truncation.
175 *
176 * Returns 0 if we managed to create more room. If we can't create more
177 * room, and the transaction must be restarted we return 1.
178 */
179static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
180{
Frank Mayhar03901312009-01-07 00:06:22 -0500181 if (!ext4_handle_valid(handle))
182 return 0;
183 if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700184 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700185 if (!ext4_journal_extend(handle, blocks_for_truncate(inode)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700186 return 0;
187 return 1;
188}
189
190/*
191 * Restart the transaction associated with *handle. This does a commit,
192 * so before we call here everything must be consistently dirtied against
193 * this transaction.
194 */
Mingming Cao617ba132006-10-11 01:20:53 -0700195static int ext4_journal_test_restart(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700196{
Frank Mayhar03901312009-01-07 00:06:22 -0500197 BUG_ON(EXT4_JOURNAL(inode) == NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700198 jbd_debug(2, "restarting handle %p\n", handle);
Mingming Cao617ba132006-10-11 01:20:53 -0700199 return ext4_journal_restart(handle, blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700200}
201
202/*
203 * Called at the last iput() if i_nlink is zero.
204 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400205void ext4_delete_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700206{
207 handle_t *handle;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400208 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700209
Jan Kara678aaf42008-07-11 19:27:31 -0400210 if (ext4_should_order_data(inode))
211 ext4_begin_ordered_truncate(inode, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700212 truncate_inode_pages(&inode->i_data, 0);
213
214 if (is_bad_inode(inode))
215 goto no_delete;
216
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400217 handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700218 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400219 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700220 /*
221 * If we're going to skip the normal cleanup, we still need to
222 * make sure that the in-core orphan linked list is properly
223 * cleaned up.
224 */
Mingming Cao617ba132006-10-11 01:20:53 -0700225 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700226 goto no_delete;
227 }
228
229 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500230 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400232 err = ext4_mark_inode_dirty(handle, inode);
233 if (err) {
234 ext4_warning(inode->i_sb, __func__,
235 "couldn't mark inode dirty (err %d)", err);
236 goto stop_handle;
237 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700238 if (inode->i_blocks)
Mingming Cao617ba132006-10-11 01:20:53 -0700239 ext4_truncate(inode);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400240
241 /*
242 * ext4_ext_truncate() doesn't reserve any slop when it
243 * restarts journal transactions; therefore there may not be
244 * enough credits left in the handle to remove the inode from
245 * the orphan list and set the dtime field.
246 */
Frank Mayhar03901312009-01-07 00:06:22 -0500247 if (!ext4_handle_has_enough_credits(handle, 3)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400248 err = ext4_journal_extend(handle, 3);
249 if (err > 0)
250 err = ext4_journal_restart(handle, 3);
251 if (err != 0) {
252 ext4_warning(inode->i_sb, __func__,
253 "couldn't extend journal (err %d)", err);
254 stop_handle:
255 ext4_journal_stop(handle);
256 goto no_delete;
257 }
258 }
259
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700260 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700261 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700262 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700263 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700264 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700265 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700266 * (Well, we could do this if we need to, but heck - it works)
267 */
Mingming Cao617ba132006-10-11 01:20:53 -0700268 ext4_orphan_del(handle, inode);
269 EXT4_I(inode)->i_dtime = get_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700270
271 /*
272 * One subtle ordering requirement: if anything has gone wrong
273 * (transaction abort, IO errors, whatever), then we can still
274 * do these next steps (the fs will already have been marked as
275 * having errors), but we can't free the inode if the mark_dirty
276 * fails.
277 */
Mingming Cao617ba132006-10-11 01:20:53 -0700278 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700279 /* If that failed, just do the required in-core inode clear. */
280 clear_inode(inode);
281 else
Mingming Cao617ba132006-10-11 01:20:53 -0700282 ext4_free_inode(handle, inode);
283 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700284 return;
285no_delete:
286 clear_inode(inode); /* We must guarantee clearing of inode... */
287}
288
289typedef struct {
290 __le32 *p;
291 __le32 key;
292 struct buffer_head *bh;
293} Indirect;
294
295static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
296{
297 p->key = *(p->p = v);
298 p->bh = bh;
299}
300
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700301/**
Mingming Cao617ba132006-10-11 01:20:53 -0700302 * ext4_block_to_path - parse the block number into array of offsets
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700303 * @inode: inode in question (we are only interested in its superblock)
304 * @i_block: block number to be parsed
305 * @offsets: array to store the offsets in
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400306 * @boundary: set this non-zero if the referred-to block is likely to be
307 * followed (on disk) by an indirect block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700308 *
Mingming Cao617ba132006-10-11 01:20:53 -0700309 * To store the locations of file's data ext4 uses a data structure common
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700310 * for UNIX filesystems - tree of pointers anchored in the inode, with
311 * data blocks at leaves and indirect blocks in intermediate nodes.
312 * This function translates the block number into path in that tree -
313 * return value is the path length and @offsets[n] is the offset of
314 * pointer to (n+1)th node in the nth one. If @block is out of range
315 * (negative or too large) warning is printed and zero returned.
316 *
317 * Note: function doesn't find node addresses, so no IO is needed. All
318 * we need to know is the capacity of indirect blocks (taken from the
319 * inode->i_sb).
320 */
321
322/*
323 * Portability note: the last comparison (check that we fit into triple
324 * indirect block) is spelled differently, because otherwise on an
325 * architecture with 32-bit longs and 8Kb pages we might get into trouble
326 * if our filesystem had 8Kb blocks. We might use long long, but that would
327 * kill us on x86. Oh, well, at least the sign propagation does not matter -
328 * i_block would have to be negative in the very beginning, so we would not
329 * get there at all.
330 */
331
Mingming Cao617ba132006-10-11 01:20:53 -0700332static int ext4_block_to_path(struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400333 ext4_lblk_t i_block,
334 ext4_lblk_t offsets[4], int *boundary)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700335{
Mingming Cao617ba132006-10-11 01:20:53 -0700336 int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb);
337 int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb);
338 const long direct_blocks = EXT4_NDIR_BLOCKS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700339 indirect_blocks = ptrs,
340 double_blocks = (1 << (ptrs_bits * 2));
341 int n = 0;
342 int final = 0;
343
Roel Kluinc333e072009-08-10 22:47:22 -0400344 if (i_block < direct_blocks) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700345 offsets[n++] = i_block;
346 final = direct_blocks;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400347 } else if ((i_block -= direct_blocks) < indirect_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700348 offsets[n++] = EXT4_IND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700349 offsets[n++] = i_block;
350 final = ptrs;
351 } else if ((i_block -= indirect_blocks) < double_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700352 offsets[n++] = EXT4_DIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700353 offsets[n++] = i_block >> ptrs_bits;
354 offsets[n++] = i_block & (ptrs - 1);
355 final = ptrs;
356 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
Mingming Cao617ba132006-10-11 01:20:53 -0700357 offsets[n++] = EXT4_TIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700358 offsets[n++] = i_block >> (ptrs_bits * 2);
359 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
360 offsets[n++] = i_block & (ptrs - 1);
361 final = ptrs;
362 } else {
Eric Sandeene2b46572008-01-28 23:58:27 -0500363 ext4_warning(inode->i_sb, "ext4_block_to_path",
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400364 "block %lu > max in inode %lu",
365 i_block + direct_blocks +
366 indirect_blocks + double_blocks, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700367 }
368 if (boundary)
369 *boundary = final - 1 - (i_block & (ptrs - 1));
370 return n;
371}
372
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400373static int __ext4_check_blockref(const char *function, struct inode *inode,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400374 __le32 *p, unsigned int max)
375{
Thiemo Nagelf73953c2009-04-07 18:46:47 -0400376 __le32 *bref = p;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400377 unsigned int blk;
378
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400379 while (bref < p+max) {
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400380 blk = le32_to_cpu(*bref++);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400381 if (blk &&
382 unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb),
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400383 blk, 1))) {
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400384 ext4_error(inode->i_sb, function,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400385 "invalid block reference %u "
386 "in inode #%lu", blk, inode->i_ino);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400387 return -EIO;
388 }
389 }
390 return 0;
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400391}
392
393
394#define ext4_check_indirect_blockref(inode, bh) \
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400395 __ext4_check_blockref(__func__, inode, (__le32 *)(bh)->b_data, \
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400396 EXT4_ADDR_PER_BLOCK((inode)->i_sb))
397
398#define ext4_check_inode_blockref(inode) \
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400399 __ext4_check_blockref(__func__, inode, EXT4_I(inode)->i_data, \
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400400 EXT4_NDIR_BLOCKS)
401
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700402/**
Mingming Cao617ba132006-10-11 01:20:53 -0700403 * ext4_get_branch - read the chain of indirect blocks leading to data
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700404 * @inode: inode in question
405 * @depth: depth of the chain (1 - direct pointer, etc.)
406 * @offsets: offsets of pointers in inode/indirect blocks
407 * @chain: place to store the result
408 * @err: here we store the error value
409 *
410 * Function fills the array of triples <key, p, bh> and returns %NULL
411 * if everything went OK or the pointer to the last filled triple
412 * (incomplete one) otherwise. Upon the return chain[i].key contains
413 * the number of (i+1)-th block in the chain (as it is stored in memory,
414 * i.e. little-endian 32-bit), chain[i].p contains the address of that
415 * number (it points into struct inode for i==0 and into the bh->b_data
416 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
417 * block for i>0 and NULL for i==0. In other words, it holds the block
418 * numbers of the chain, addresses they were taken from (and where we can
419 * verify that chain did not change) and buffer_heads hosting these
420 * numbers.
421 *
422 * Function stops when it stumbles upon zero pointer (absent block)
423 * (pointer to last triple returned, *@err == 0)
424 * or when it gets an IO error reading an indirect block
425 * (ditto, *@err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700426 * or when it reads all @depth-1 indirect blocks successfully and finds
427 * the whole chain, all way to the data (returns %NULL, *err == 0).
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500428 *
429 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500430 * down_read(&EXT4_I(inode)->i_data_sem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700431 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500432static Indirect *ext4_get_branch(struct inode *inode, int depth,
433 ext4_lblk_t *offsets,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700434 Indirect chain[4], int *err)
435{
436 struct super_block *sb = inode->i_sb;
437 Indirect *p = chain;
438 struct buffer_head *bh;
439
440 *err = 0;
441 /* i_data is not going away, no lock needed */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400442 add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700443 if (!p->key)
444 goto no_block;
445 while (--depth) {
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400446 bh = sb_getblk(sb, le32_to_cpu(p->key));
447 if (unlikely(!bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700448 goto failure;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400449
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400450 if (!bh_uptodate_or_lock(bh)) {
451 if (bh_submit_read(bh) < 0) {
452 put_bh(bh);
453 goto failure;
454 }
455 /* validate block references */
456 if (ext4_check_indirect_blockref(inode, bh)) {
457 put_bh(bh);
458 goto failure;
459 }
460 }
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400461
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400462 add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700463 /* Reader: end */
464 if (!p->key)
465 goto no_block;
466 }
467 return NULL;
468
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700469failure:
470 *err = -EIO;
471no_block:
472 return p;
473}
474
475/**
Mingming Cao617ba132006-10-11 01:20:53 -0700476 * ext4_find_near - find a place for allocation with sufficient locality
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700477 * @inode: owner
478 * @ind: descriptor of indirect block.
479 *
Benoit Boissinot1cc8dcf52008-04-21 22:45:55 +0000480 * This function returns the preferred place for block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700481 * It is used when heuristic for sequential allocation fails.
482 * Rules are:
483 * + if there is a block to the left of our position - allocate near it.
484 * + if pointer will live in indirect block - allocate near that block.
485 * + if pointer will live in inode - allocate in the same
486 * cylinder group.
487 *
488 * In the latter case we colour the starting block by the callers PID to
489 * prevent it from clashing with concurrent allocations for a different inode
490 * in the same block group. The PID is used here so that functionally related
491 * files will be close-by on-disk.
492 *
493 * Caller must make sure that @ind is valid and will stay that way.
494 */
Mingming Cao617ba132006-10-11 01:20:53 -0700495static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700496{
Mingming Cao617ba132006-10-11 01:20:53 -0700497 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400498 __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700499 __le32 *p;
Mingming Cao617ba132006-10-11 01:20:53 -0700500 ext4_fsblk_t bg_start;
Valerie Clement74d34872008-02-15 13:43:07 -0500501 ext4_fsblk_t last_block;
Mingming Cao617ba132006-10-11 01:20:53 -0700502 ext4_grpblk_t colour;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400503 ext4_group_t block_group;
504 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700505
506 /* Try to find previous block */
507 for (p = ind->p - 1; p >= start; p--) {
508 if (*p)
509 return le32_to_cpu(*p);
510 }
511
512 /* No such thing, so let's try location of indirect block */
513 if (ind->bh)
514 return ind->bh->b_blocknr;
515
516 /*
517 * It is going to be referred to from the inode itself? OK, just put it
518 * into the same cylinder group then.
519 */
Theodore Ts'oa4912122009-03-12 12:18:34 -0400520 block_group = ei->i_block_group;
521 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
522 block_group &= ~(flex_size-1);
523 if (S_ISREG(inode->i_mode))
524 block_group++;
525 }
526 bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
Valerie Clement74d34872008-02-15 13:43:07 -0500527 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
528
Theodore Ts'oa4912122009-03-12 12:18:34 -0400529 /*
530 * If we are doing delayed allocation, we don't need take
531 * colour into account.
532 */
533 if (test_opt(inode->i_sb, DELALLOC))
534 return bg_start;
535
Valerie Clement74d34872008-02-15 13:43:07 -0500536 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
537 colour = (current->pid % 16) *
Mingming Cao617ba132006-10-11 01:20:53 -0700538 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
Valerie Clement74d34872008-02-15 13:43:07 -0500539 else
540 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700541 return bg_start + colour;
542}
543
544/**
Benoit Boissinot1cc8dcf52008-04-21 22:45:55 +0000545 * ext4_find_goal - find a preferred place for allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700546 * @inode: owner
547 * @block: block we want
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700548 * @partial: pointer to the last triple within a chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700549 *
Benoit Boissinot1cc8dcf52008-04-21 22:45:55 +0000550 * Normally this function find the preferred place for block allocation,
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800551 * returns it.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700552 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500553static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400554 Indirect *partial)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700555{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700556 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400557 * XXX need to get goal block from mballoc's data structures
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700558 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700559
Mingming Cao617ba132006-10-11 01:20:53 -0700560 return ext4_find_near(inode, partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700561}
562
563/**
Mingming Cao617ba132006-10-11 01:20:53 -0700564 * ext4_blks_to_allocate: Look up the block map and count the number
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700565 * of direct blocks need to be allocated for the given branch.
566 *
567 * @branch: chain of indirect blocks
568 * @k: number of blocks need for indirect blocks
569 * @blks: number of data blocks to be mapped.
570 * @blocks_to_boundary: the offset in the indirect block
571 *
572 * return the total number of blocks to be allocate, including the
573 * direct and indirect blocks.
574 */
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500575static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400576 int blocks_to_boundary)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700577{
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500578 unsigned int count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700579
580 /*
581 * Simple case, [t,d]Indirect block(s) has not allocated yet
582 * then it's clear blocks on that path have not allocated
583 */
584 if (k > 0) {
585 /* right now we don't handle cross boundary allocation */
586 if (blks < blocks_to_boundary + 1)
587 count += blks;
588 else
589 count += blocks_to_boundary + 1;
590 return count;
591 }
592
593 count++;
594 while (count < blks && count <= blocks_to_boundary &&
595 le32_to_cpu(*(branch[0].p + count)) == 0) {
596 count++;
597 }
598 return count;
599}
600
601/**
Mingming Cao617ba132006-10-11 01:20:53 -0700602 * ext4_alloc_blocks: multiple allocate blocks needed for a branch
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700603 * @indirect_blks: the number of blocks need to allocate for indirect
604 * blocks
605 *
606 * @new_blocks: on return it will store the new block numbers for
607 * the indirect blocks(if needed) and the first direct block,
608 * @blks: on return it will store the total number of allocated
609 * direct blocks
610 */
Mingming Cao617ba132006-10-11 01:20:53 -0700611static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400612 ext4_lblk_t iblock, ext4_fsblk_t goal,
613 int indirect_blks, int blks,
614 ext4_fsblk_t new_blocks[4], int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700615{
Theodore Ts'o815a1132009-01-01 23:59:43 -0500616 struct ext4_allocation_request ar;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700617 int target, i;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400618 unsigned long count = 0, blk_allocated = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700619 int index = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700620 ext4_fsblk_t current_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700621 int ret = 0;
622
623 /*
624 * Here we try to allocate the requested multiple blocks at once,
625 * on a best-effort basis.
626 * To build a branch, we should allocate blocks for
627 * the indirect blocks(if not allocated yet), and at least
628 * the first direct block of this branch. That's the
629 * minimum number of blocks need to allocate(required)
630 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400631 /* first we try to allocate the indirect blocks */
632 target = indirect_blks;
633 while (target > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700634 count = target;
635 /* allocating blocks for indirect blocks and direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400636 current_block = ext4_new_meta_blocks(handle, inode,
637 goal, &count, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700638 if (*err)
639 goto failed_out;
640
641 target -= count;
642 /* allocate blocks for indirect blocks */
643 while (index < indirect_blks && count) {
644 new_blocks[index++] = current_block++;
645 count--;
646 }
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400647 if (count > 0) {
648 /*
649 * save the new block number
650 * for the first direct block
651 */
652 new_blocks[index] = current_block;
653 printk(KERN_INFO "%s returned more blocks than "
654 "requested\n", __func__);
655 WARN_ON(1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700656 break;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400657 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700658 }
659
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400660 target = blks - count ;
661 blk_allocated = count;
662 if (!target)
663 goto allocated;
664 /* Now allocate data blocks */
Theodore Ts'o815a1132009-01-01 23:59:43 -0500665 memset(&ar, 0, sizeof(ar));
666 ar.inode = inode;
667 ar.goal = goal;
668 ar.len = target;
669 ar.logical = iblock;
670 if (S_ISREG(inode->i_mode))
671 /* enable in-core preallocation only for regular files */
672 ar.flags = EXT4_MB_HINT_DATA;
673
674 current_block = ext4_mb_new_blocks(handle, &ar, err);
675
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400676 if (*err && (target == blks)) {
677 /*
678 * if the allocation failed and we didn't allocate
679 * any blocks before
680 */
681 goto failed_out;
682 }
683 if (!*err) {
684 if (target == blks) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400685 /*
686 * save the new block number
687 * for the first direct block
688 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400689 new_blocks[index] = current_block;
690 }
Theodore Ts'o815a1132009-01-01 23:59:43 -0500691 blk_allocated += ar.len;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400692 }
693allocated:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700694 /* total number of blocks allocated for direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400695 ret = blk_allocated;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700696 *err = 0;
697 return ret;
698failed_out:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400699 for (i = 0; i < index; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500700 ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700701 return ret;
702}
703
704/**
Mingming Cao617ba132006-10-11 01:20:53 -0700705 * ext4_alloc_branch - allocate and set up a chain of blocks.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700706 * @inode: owner
707 * @indirect_blks: number of allocated indirect blocks
708 * @blks: number of allocated direct blocks
709 * @offsets: offsets (in the blocks) to store the pointers to next.
710 * @branch: place to store the chain in.
711 *
712 * This function allocates blocks, zeroes out all but the last one,
713 * links them into chain and (if we are synchronous) writes them to disk.
714 * In other words, it prepares a branch that can be spliced onto the
715 * inode. It stores the information about that chain in the branch[], in
Mingming Cao617ba132006-10-11 01:20:53 -0700716 * the same format as ext4_get_branch() would do. We are calling it after
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700717 * we had read the existing part of chain and partial points to the last
718 * triple of that (one with zero ->key). Upon the exit we have the same
Mingming Cao617ba132006-10-11 01:20:53 -0700719 * picture as after the successful ext4_get_block(), except that in one
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700720 * place chain is disconnected - *branch->p is still zero (we did not
721 * set the last link), but branch->key contains the number that should
722 * be placed into *branch->p to fill that gap.
723 *
724 * If allocation fails we free all blocks we've allocated (and forget
725 * their buffer_heads) and return the error value the from failed
Mingming Cao617ba132006-10-11 01:20:53 -0700726 * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700727 * as described above and return 0.
728 */
Mingming Cao617ba132006-10-11 01:20:53 -0700729static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400730 ext4_lblk_t iblock, int indirect_blks,
731 int *blks, ext4_fsblk_t goal,
732 ext4_lblk_t *offsets, Indirect *branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700733{
734 int blocksize = inode->i_sb->s_blocksize;
735 int i, n = 0;
736 int err = 0;
737 struct buffer_head *bh;
738 int num;
Mingming Cao617ba132006-10-11 01:20:53 -0700739 ext4_fsblk_t new_blocks[4];
740 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700741
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400742 num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700743 *blks, new_blocks, &err);
744 if (err)
745 return err;
746
747 branch[0].key = cpu_to_le32(new_blocks[0]);
748 /*
749 * metadata blocks and data blocks are allocated.
750 */
751 for (n = 1; n <= indirect_blks; n++) {
752 /*
753 * Get buffer_head for parent block, zero it out
754 * and set the pointer to new one, then send
755 * parent to disk.
756 */
757 bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
758 branch[n].bh = bh;
759 lock_buffer(bh);
760 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700761 err = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700762 if (err) {
Curt Wohlgemuth6487a9d2009-07-17 10:54:08 -0400763 /* Don't brelse(bh) here; it's done in
764 * ext4_journal_forget() below */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700765 unlock_buffer(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700766 goto failed;
767 }
768
769 memset(bh->b_data, 0, blocksize);
770 branch[n].p = (__le32 *) bh->b_data + offsets[n];
771 branch[n].key = cpu_to_le32(new_blocks[n]);
772 *branch[n].p = branch[n].key;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400773 if (n == indirect_blks) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700774 current_block = new_blocks[n];
775 /*
776 * End of chain, update the last new metablock of
777 * the chain to point to the new allocated
778 * data blocks numbers
779 */
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400780 for (i = 1; i < num; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700781 *(branch[n].p + i) = cpu_to_le32(++current_block);
782 }
783 BUFFER_TRACE(bh, "marking uptodate");
784 set_buffer_uptodate(bh);
785 unlock_buffer(bh);
786
Frank Mayhar03901312009-01-07 00:06:22 -0500787 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
788 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700789 if (err)
790 goto failed;
791 }
792 *blks = num;
793 return err;
794failed:
795 /* Allocation failed, free what we already allocated */
796 for (i = 1; i <= n ; i++) {
Mingming Caodab291a2006-10-11 01:21:01 -0700797 BUFFER_TRACE(branch[i].bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700798 ext4_journal_forget(handle, branch[i].bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700799 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400800 for (i = 0; i < indirect_blks; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500801 ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700802
Alex Tomasc9de5602008-01-29 00:19:52 -0500803 ext4_free_blocks(handle, inode, new_blocks[i], num, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700804
805 return err;
806}
807
808/**
Mingming Cao617ba132006-10-11 01:20:53 -0700809 * ext4_splice_branch - splice the allocated branch onto inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700810 * @inode: owner
811 * @block: (logical) number of block we are adding
812 * @chain: chain of indirect blocks (with a missing link - see
Mingming Cao617ba132006-10-11 01:20:53 -0700813 * ext4_alloc_branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700814 * @where: location of missing link
815 * @num: number of indirect blocks we are adding
816 * @blks: number of direct blocks we are adding
817 *
818 * This function fills the missing link and does all housekeeping needed in
819 * inode (->i_blocks, etc.). In case of success we end up with the full
820 * chain to new block and return 0.
821 */
Mingming Cao617ba132006-10-11 01:20:53 -0700822static int ext4_splice_branch(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400823 ext4_lblk_t block, Indirect *where, int num,
824 int blks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700825{
826 int i;
827 int err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700828 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700829
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700830 /*
831 * If we're splicing into a [td]indirect block (as opposed to the
832 * inode) then we need to get write access to the [td]indirect block
833 * before the splice.
834 */
835 if (where->bh) {
836 BUFFER_TRACE(where->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700837 err = ext4_journal_get_write_access(handle, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700838 if (err)
839 goto err_out;
840 }
841 /* That's it */
842
843 *where->p = where->key;
844
845 /*
846 * Update the host buffer_head or inode to point to more just allocated
847 * direct blocks blocks
848 */
849 if (num == 0 && blks > 1) {
850 current_block = le32_to_cpu(where->key) + 1;
851 for (i = 1; i < blks; i++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400852 *(where->p + i) = cpu_to_le32(current_block++);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700853 }
854
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700855 /* We are done with atomic stuff, now do the rest of housekeeping */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700856 /* had we spliced it onto indirect block? */
857 if (where->bh) {
858 /*
859 * If we spliced it onto an indirect block, we haven't
860 * altered the inode. Note however that if it is being spliced
861 * onto an indirect block at the very end of the file (the
862 * file is growing) then we *will* alter the inode to reflect
863 * the new i_size. But that is not done here - it is done in
Mingming Cao617ba132006-10-11 01:20:53 -0700864 * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700865 */
866 jbd_debug(5, "splicing indirect only\n");
Frank Mayhar03901312009-01-07 00:06:22 -0500867 BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata");
868 err = ext4_handle_dirty_metadata(handle, inode, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700869 if (err)
870 goto err_out;
871 } else {
872 /*
873 * OK, we spliced it into the inode itself on a direct block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700874 */
Theodore Ts'o41591752009-06-15 03:41:23 -0400875 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700876 jbd_debug(5, "splicing direct\n");
877 }
878 return err;
879
880err_out:
881 for (i = 1; i <= num; i++) {
Mingming Caodab291a2006-10-11 01:21:01 -0700882 BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700883 ext4_journal_forget(handle, where[i].bh);
Alex Tomasc9de5602008-01-29 00:19:52 -0500884 ext4_free_blocks(handle, inode,
885 le32_to_cpu(where[i-1].key), 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700886 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500887 ext4_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700888
889 return err;
890}
891
892/*
Theodore Ts'ob920c752009-05-14 00:54:29 -0400893 * The ext4_ind_get_blocks() function handles non-extents inodes
894 * (i.e., using the traditional indirect/double-indirect i_blocks
895 * scheme) for ext4_get_blocks().
896 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700897 * Allocation strategy is simple: if we have to allocate something, we will
898 * have to go the whole way to leaf. So let's do it before attaching anything
899 * to tree, set linkage between the newborn blocks, write them if sync is
900 * required, recheck the path, free and repeat if check fails, otherwise
901 * set the last missing link (that will protect us from any truncate-generated
902 * removals - all blocks on the path are immune now) and possibly force the
903 * write on the parent block.
904 * That has a nice additional property: no special recovery from the failed
905 * allocations is needed - we simply release blocks and do not touch anything
906 * reachable from inode.
907 *
908 * `handle' can be NULL if create == 0.
909 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700910 * return > 0, # of blocks mapped or allocated.
911 * return = 0, if plain lookup failed.
912 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500913 *
Theodore Ts'ob920c752009-05-14 00:54:29 -0400914 * The ext4_ind_get_blocks() function should be called with
915 * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem
916 * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or
917 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system
918 * blocks.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700919 */
Theodore Ts'oe4d996c2009-05-12 00:25:28 -0400920static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400921 ext4_lblk_t iblock, unsigned int maxblocks,
922 struct buffer_head *bh_result,
923 int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700924{
925 int err = -EIO;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500926 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700927 Indirect chain[4];
928 Indirect *partial;
Mingming Cao617ba132006-10-11 01:20:53 -0700929 ext4_fsblk_t goal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700930 int indirect_blks;
931 int blocks_to_boundary = 0;
932 int depth;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700933 int count = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700934 ext4_fsblk_t first_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700935
Alex Tomasa86c6182006-10-11 01:21:03 -0700936 J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL));
Theodore Ts'oc2177052009-05-14 00:58:52 -0400937 J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500938 depth = ext4_block_to_path(inode, iblock, offsets,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400939 &blocks_to_boundary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700940
941 if (depth == 0)
942 goto out;
943
Mingming Cao617ba132006-10-11 01:20:53 -0700944 partial = ext4_get_branch(inode, depth, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700945
946 /* Simplest case - block found, no allocation needed */
947 if (!partial) {
948 first_block = le32_to_cpu(chain[depth - 1].key);
949 clear_buffer_new(bh_result);
950 count++;
951 /*map more blocks*/
952 while (count < maxblocks && count <= blocks_to_boundary) {
Mingming Cao617ba132006-10-11 01:20:53 -0700953 ext4_fsblk_t blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700954
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700955 blk = le32_to_cpu(*(chain[depth-1].p + count));
956
957 if (blk == first_block + count)
958 count++;
959 else
960 break;
961 }
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500962 goto got_it;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700963 }
964
965 /* Next simple case - plain lookup or failed read of indirect block */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400966 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700967 goto cleanup;
968
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700969 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400970 * Okay, we need to do block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700971 */
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800972 goal = ext4_find_goal(inode, iblock, partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700973
974 /* the number of blocks need to allocate for [d,t]indirect blocks */
975 indirect_blks = (chain + depth) - partial - 1;
976
977 /*
978 * Next look up the indirect map to count the totoal number of
979 * direct blocks to allocate for this branch.
980 */
Mingming Cao617ba132006-10-11 01:20:53 -0700981 count = ext4_blks_to_allocate(partial, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700982 maxblocks, blocks_to_boundary);
983 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700984 * Block out ext4_truncate while we alter the tree
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700985 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400986 err = ext4_alloc_branch(handle, inode, iblock, indirect_blks,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400987 &count, goal,
988 offsets + (partial - chain), partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700989
990 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700991 * The ext4_splice_branch call will free and forget any buffers
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700992 * on the new chain if there is a failure, but that risks using
993 * up transaction credits, especially for bitmaps where the
994 * credits cannot be returned. Can we handle this somehow? We
995 * may need to return -EAGAIN upwards in the worst case. --sct
996 */
997 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -0700998 err = ext4_splice_branch(handle, inode, iblock,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400999 partial, indirect_blks, count);
1000 else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001001 goto cleanup;
1002
1003 set_buffer_new(bh_result);
1004got_it:
1005 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
1006 if (count > blocks_to_boundary)
1007 set_buffer_boundary(bh_result);
1008 err = count;
1009 /* Clean up and exit */
1010 partial = chain + depth - 1; /* the whole chain */
1011cleanup:
1012 while (partial > chain) {
1013 BUFFER_TRACE(partial->bh, "call brelse");
1014 brelse(partial->bh);
1015 partial--;
1016 }
1017 BUFFER_TRACE(bh_result, "returned");
1018out:
1019 return err;
1020}
1021
Mingming Cao60e58e02009-01-22 18:13:05 +01001022qsize_t ext4_get_reserved_space(struct inode *inode)
1023{
1024 unsigned long long total;
1025
1026 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1027 total = EXT4_I(inode)->i_reserved_data_blocks +
1028 EXT4_I(inode)->i_reserved_meta_blocks;
1029 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1030
1031 return total;
1032}
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001033/*
1034 * Calculate the number of metadata blocks need to reserve
1035 * to allocate @blocks for non extent file based file
1036 */
1037static int ext4_indirect_calc_metadata_amount(struct inode *inode, int blocks)
1038{
1039 int icap = EXT4_ADDR_PER_BLOCK(inode->i_sb);
1040 int ind_blks, dind_blks, tind_blks;
1041
1042 /* number of new indirect blocks needed */
1043 ind_blks = (blocks + icap - 1) / icap;
1044
1045 dind_blks = (ind_blks + icap - 1) / icap;
1046
1047 tind_blks = 1;
1048
1049 return ind_blks + dind_blks + tind_blks;
1050}
1051
1052/*
1053 * Calculate the number of metadata blocks need to reserve
1054 * to allocate given number of blocks
1055 */
1056static int ext4_calc_metadata_amount(struct inode *inode, int blocks)
1057{
Mingming Caocd213222008-08-19 22:16:59 -04001058 if (!blocks)
1059 return 0;
1060
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001061 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)
1062 return ext4_ext_calc_metadata_amount(inode, blocks);
1063
1064 return ext4_indirect_calc_metadata_amount(inode, blocks);
1065}
1066
1067static void ext4_da_update_reserve_space(struct inode *inode, int used)
1068{
1069 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1070 int total, mdb, mdb_free;
1071
1072 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1073 /* recalculate the number of metablocks still need to be reserved */
1074 total = EXT4_I(inode)->i_reserved_data_blocks - used;
1075 mdb = ext4_calc_metadata_amount(inode, total);
1076
1077 /* figure out how many metablocks to release */
1078 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1079 mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
1080
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001081 if (mdb_free) {
1082 /* Account for allocated meta_blocks */
1083 mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001084
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001085 /* update fs dirty blocks counter */
1086 percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free);
1087 EXT4_I(inode)->i_allocated_meta_blocks = 0;
1088 EXT4_I(inode)->i_reserved_meta_blocks = mdb;
1089 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001090
1091 /* update per-inode reservations */
1092 BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks);
1093 EXT4_I(inode)->i_reserved_data_blocks -= used;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001094 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001095
1096 /*
1097 * free those over-booking quota for metadata blocks
1098 */
Mingming Cao60e58e02009-01-22 18:13:05 +01001099 if (mdb_free)
1100 vfs_dq_release_reservation_block(inode, mdb_free);
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -04001101
1102 /*
1103 * If we have done all the pending block allocations and if
1104 * there aren't any writers on the inode, we can discard the
1105 * inode's preallocations.
1106 */
1107 if (!total && (atomic_read(&inode->i_writecount) == 0))
1108 ext4_discard_preallocations(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001109}
1110
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001111static int check_block_validity(struct inode *inode, sector_t logical,
1112 sector_t phys, int len)
1113{
1114 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), phys, len)) {
1115 ext4_error(inode->i_sb, "check_block_validity",
1116 "inode #%lu logical block %llu mapped to %llu "
1117 "(size %d)", inode->i_ino,
1118 (unsigned long long) logical,
1119 (unsigned long long) phys, len);
1120 WARN_ON(1);
1121 return -EIO;
1122 }
1123 return 0;
1124}
1125
Mingming Caof5ab0d12008-02-25 15:29:55 -05001126/*
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001127 * The ext4_get_blocks() function tries to look up the requested blocks,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001128 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -05001129 *
Mingming Caof5ab0d12008-02-25 15:29:55 -05001130 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
1131 * and store the allocated blocks in the result buffer head and mark it
1132 * mapped.
1133 *
1134 * If file type is extents based, it will call ext4_ext_get_blocks(),
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001135 * Otherwise, call with ext4_ind_get_blocks() to handle indirect mapping
Mingming Caof5ab0d12008-02-25 15:29:55 -05001136 * based files
1137 *
1138 * On success, it returns the number of blocks being mapped or allocate.
1139 * if create==0 and the blocks are pre-allocated and uninitialized block,
1140 * the result buffer head is unmapped. If the create ==1, it will make sure
1141 * the buffer head is mapped.
1142 *
1143 * It returns 0 if plain look up failed (blocks have not been allocated), in
1144 * that casem, buffer head is unmapped
1145 *
1146 * It returns the error in case of allocation failure.
1147 */
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001148int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block,
1149 unsigned int max_blocks, struct buffer_head *bh,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001150 int flags)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001151{
1152 int retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -05001153
1154 clear_buffer_mapped(bh);
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -04001155 clear_buffer_unwritten(bh);
Mingming Caof5ab0d12008-02-25 15:29:55 -05001156
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001157 /*
Theodore Ts'ob920c752009-05-14 00:54:29 -04001158 * Try to see if we can get the block without requesting a new
1159 * file system block.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001160 */
1161 down_read((&EXT4_I(inode)->i_data_sem));
1162 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1163 retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001164 bh, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001165 } else {
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001166 retval = ext4_ind_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001167 bh, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001168 }
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001169 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -05001170
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001171 if (retval > 0 && buffer_mapped(bh)) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001172 int ret = check_block_validity(inode, block,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001173 bh->b_blocknr, retval);
1174 if (ret != 0)
1175 return ret;
1176 }
1177
Mingming Caof5ab0d12008-02-25 15:29:55 -05001178 /* If it is only a block(s) look up */
Theodore Ts'oc2177052009-05-14 00:58:52 -04001179 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001180 return retval;
1181
1182 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001183 * Returns if the blocks have already allocated
1184 *
1185 * Note that if blocks have been preallocated
1186 * ext4_ext_get_block() returns th create = 0
1187 * with buffer head unmapped.
1188 */
1189 if (retval > 0 && buffer_mapped(bh))
1190 return retval;
1191
1192 /*
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -04001193 * When we call get_blocks without the create flag, the
1194 * BH_Unwritten flag could have gotten set if the blocks
1195 * requested were part of a uninitialized extent. We need to
1196 * clear this flag now that we are committed to convert all or
1197 * part of the uninitialized extent to be an initialized
1198 * extent. This is because we need to avoid the combination
1199 * of BH_Unwritten and BH_Mapped flags being simultaneously
1200 * set on the buffer_head.
1201 */
1202 clear_buffer_unwritten(bh);
1203
1204 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001205 * New blocks allocate and/or writing to uninitialized extent
1206 * will possibly result in updating i_data, so we take
1207 * the write lock of i_data_sem, and call get_blocks()
1208 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001209 */
1210 down_write((&EXT4_I(inode)->i_data_sem));
Mingming Caod2a17632008-07-14 17:52:37 -04001211
1212 /*
1213 * if the caller is from delayed allocation writeout path
1214 * we have already reserved fs blocks for allocation
1215 * let the underlying get_block() function know to
1216 * avoid double accounting
1217 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04001218 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Mingming Caod2a17632008-07-14 17:52:37 -04001219 EXT4_I(inode)->i_delalloc_reserved_flag = 1;
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001220 /*
1221 * We need to check for EXT4 here because migrate
1222 * could have changed the inode type in between
1223 */
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001224 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1225 retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001226 bh, flags);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001227 } else {
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001228 retval = ext4_ind_get_blocks(handle, inode, block,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001229 max_blocks, bh, flags);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04001230
1231 if (retval > 0 && buffer_new(bh)) {
1232 /*
1233 * We allocated new blocks which will result in
1234 * i_data's format changing. Force the migrate
1235 * to fail by clearing migrate flags
1236 */
1237 EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags &
1238 ~EXT4_EXT_MIGRATE;
1239 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001240 }
Mingming Caod2a17632008-07-14 17:52:37 -04001241
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001242 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Mingming Caod2a17632008-07-14 17:52:37 -04001243 EXT4_I(inode)->i_delalloc_reserved_flag = 0;
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001244
1245 /*
1246 * Update reserved blocks/metadata blocks after successful
1247 * block allocation which had been deferred till now.
1248 */
1249 if ((retval > 0) && (flags & EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE))
1250 ext4_da_update_reserve_space(inode, retval);
Mingming Caod2a17632008-07-14 17:52:37 -04001251
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001252 up_write((&EXT4_I(inode)->i_data_sem));
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001253 if (retval > 0 && buffer_mapped(bh)) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001254 int ret = check_block_validity(inode, block,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001255 bh->b_blocknr, retval);
1256 if (ret != 0)
1257 return ret;
1258 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001259 return retval;
1260}
1261
Mingming Caof3bd1f32008-08-19 22:16:03 -04001262/* Maximum number of blocks we map for direct IO at once. */
1263#define DIO_MAX_BLOCKS 4096
1264
Eric Sandeen6873fa02008-10-07 00:46:36 -04001265int ext4_get_block(struct inode *inode, sector_t iblock,
1266 struct buffer_head *bh_result, int create)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001267{
Dmitriy Monakhov3e4fdaf2007-02-10 01:46:35 -08001268 handle_t *handle = ext4_journal_current_handle();
Jan Kara7fb54092008-02-10 01:08:38 -05001269 int ret = 0, started = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001270 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001271 int dio_credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001272
Jan Kara7fb54092008-02-10 01:08:38 -05001273 if (create && !handle) {
1274 /* Direct IO write... */
1275 if (max_blocks > DIO_MAX_BLOCKS)
1276 max_blocks = DIO_MAX_BLOCKS;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001277 dio_credits = ext4_chunk_trans_blocks(inode, max_blocks);
1278 handle = ext4_journal_start(inode, dio_credits);
Jan Kara7fb54092008-02-10 01:08:38 -05001279 if (IS_ERR(handle)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001280 ret = PTR_ERR(handle);
Jan Kara7fb54092008-02-10 01:08:38 -05001281 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001282 }
Jan Kara7fb54092008-02-10 01:08:38 -05001283 started = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001284 }
1285
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001286 ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001287 create ? EXT4_GET_BLOCKS_CREATE : 0);
Jan Kara7fb54092008-02-10 01:08:38 -05001288 if (ret > 0) {
1289 bh_result->b_size = (ret << inode->i_blkbits);
1290 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001291 }
Jan Kara7fb54092008-02-10 01:08:38 -05001292 if (started)
1293 ext4_journal_stop(handle);
1294out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001295 return ret;
1296}
1297
1298/*
1299 * `handle' can be NULL if create is zero
1300 */
Mingming Cao617ba132006-10-11 01:20:53 -07001301struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001302 ext4_lblk_t block, int create, int *errp)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001303{
1304 struct buffer_head dummy;
1305 int fatal = 0, err;
Jan Kara03f5d8b2009-06-09 00:17:05 -04001306 int flags = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001307
1308 J_ASSERT(handle != NULL || create == 0);
1309
1310 dummy.b_state = 0;
1311 dummy.b_blocknr = -1000;
1312 buffer_trace_init(&dummy.b_history);
Theodore Ts'oc2177052009-05-14 00:58:52 -04001313 if (create)
1314 flags |= EXT4_GET_BLOCKS_CREATE;
1315 err = ext4_get_blocks(handle, inode, block, 1, &dummy, flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001316 /*
Theodore Ts'oc2177052009-05-14 00:58:52 -04001317 * ext4_get_blocks() returns number of blocks mapped. 0 in
1318 * case of a HOLE.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001319 */
1320 if (err > 0) {
1321 if (err > 1)
1322 WARN_ON(1);
1323 err = 0;
1324 }
1325 *errp = err;
1326 if (!err && buffer_mapped(&dummy)) {
1327 struct buffer_head *bh;
1328 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
1329 if (!bh) {
1330 *errp = -EIO;
1331 goto err;
1332 }
1333 if (buffer_new(&dummy)) {
1334 J_ASSERT(create != 0);
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001335 J_ASSERT(handle != NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001336
1337 /*
1338 * Now that we do not always journal data, we should
1339 * keep in mind whether this should always journal the
1340 * new buffer as metadata. For now, regular file
Mingming Cao617ba132006-10-11 01:20:53 -07001341 * writes use ext4_get_block instead, so it's not a
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001342 * problem.
1343 */
1344 lock_buffer(bh);
1345 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001346 fatal = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001347 if (!fatal && !buffer_uptodate(bh)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001348 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001349 set_buffer_uptodate(bh);
1350 }
1351 unlock_buffer(bh);
Frank Mayhar03901312009-01-07 00:06:22 -05001352 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
1353 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001354 if (!fatal)
1355 fatal = err;
1356 } else {
1357 BUFFER_TRACE(bh, "not a new buffer");
1358 }
1359 if (fatal) {
1360 *errp = fatal;
1361 brelse(bh);
1362 bh = NULL;
1363 }
1364 return bh;
1365 }
1366err:
1367 return NULL;
1368}
1369
Mingming Cao617ba132006-10-11 01:20:53 -07001370struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001371 ext4_lblk_t block, int create, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001372{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001373 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001374
Mingming Cao617ba132006-10-11 01:20:53 -07001375 bh = ext4_getblk(handle, inode, block, create, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001376 if (!bh)
1377 return bh;
1378 if (buffer_uptodate(bh))
1379 return bh;
1380 ll_rw_block(READ_META, 1, &bh);
1381 wait_on_buffer(bh);
1382 if (buffer_uptodate(bh))
1383 return bh;
1384 put_bh(bh);
1385 *err = -EIO;
1386 return NULL;
1387}
1388
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001389static int walk_page_buffers(handle_t *handle,
1390 struct buffer_head *head,
1391 unsigned from,
1392 unsigned to,
1393 int *partial,
1394 int (*fn)(handle_t *handle,
1395 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001396{
1397 struct buffer_head *bh;
1398 unsigned block_start, block_end;
1399 unsigned blocksize = head->b_size;
1400 int err, ret = 0;
1401 struct buffer_head *next;
1402
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001403 for (bh = head, block_start = 0;
1404 ret == 0 && (bh != head || !block_start);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001405 block_start = block_end, bh = next) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001406 next = bh->b_this_page;
1407 block_end = block_start + blocksize;
1408 if (block_end <= from || block_start >= to) {
1409 if (partial && !buffer_uptodate(bh))
1410 *partial = 1;
1411 continue;
1412 }
1413 err = (*fn)(handle, bh);
1414 if (!ret)
1415 ret = err;
1416 }
1417 return ret;
1418}
1419
1420/*
1421 * To preserve ordering, it is essential that the hole instantiation and
1422 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -07001423 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -07001424 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001425 * prepare_write() is the right place.
1426 *
Mingming Cao617ba132006-10-11 01:20:53 -07001427 * Also, this function can nest inside ext4_writepage() ->
1428 * block_write_full_page(). In that case, we *know* that ext4_writepage()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001429 * has generated enough buffer credits to do the whole page. So we won't
1430 * block on the journal in that case, which is good, because the caller may
1431 * be PF_MEMALLOC.
1432 *
Mingming Cao617ba132006-10-11 01:20:53 -07001433 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001434 * quota file writes. If we were to commit the transaction while thus
1435 * reentered, there can be a deadlock - we would be holding a quota
1436 * lock, and the commit would never complete if another thread had a
1437 * transaction open and was blocking on the quota lock - a ranking
1438 * violation.
1439 *
Mingming Caodab291a2006-10-11 01:21:01 -07001440 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001441 * will _not_ run commit under these circumstances because handle->h_ref
1442 * is elevated. We'll still have enough credits for the tiny quotafile
1443 * write.
1444 */
1445static int do_journal_get_write_access(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001446 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001447{
1448 if (!buffer_mapped(bh) || buffer_freed(bh))
1449 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001450 return ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001451}
1452
Nick Pigginbfc1af62007-10-16 01:25:05 -07001453static int ext4_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001454 loff_t pos, unsigned len, unsigned flags,
1455 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001456{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001457 struct inode *inode = mapping->host;
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001458 int ret, needed_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001459 handle_t *handle;
1460 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001461 struct page *page;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001462 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001463 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001464
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001465 trace_ext4_write_begin(inode, pos, len, flags);
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001466 /*
1467 * Reserve one block more for addition to orphan list in case
1468 * we allocate blocks but write fails for some reason
1469 */
1470 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001471 index = pos >> PAGE_CACHE_SHIFT;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001472 from = pos & (PAGE_CACHE_SIZE - 1);
1473 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001474
1475retry:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001476 handle = ext4_journal_start(inode, needed_blocks);
1477 if (IS_ERR(handle)) {
1478 ret = PTR_ERR(handle);
1479 goto out;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001480 }
1481
Jan Karaebd36102009-02-22 21:09:59 -05001482 /* We cannot recurse into the filesystem as the transaction is already
1483 * started */
1484 flags |= AOP_FLAG_NOFS;
1485
Nick Piggin54566b22009-01-04 12:00:53 -08001486 page = grab_cache_page_write_begin(mapping, index, flags);
Jan Karacf108bc2008-07-11 19:27:31 -04001487 if (!page) {
1488 ext4_journal_stop(handle);
1489 ret = -ENOMEM;
1490 goto out;
1491 }
1492 *pagep = page;
1493
Nick Pigginbfc1af62007-10-16 01:25:05 -07001494 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
Jan Karaebd36102009-02-22 21:09:59 -05001495 ext4_get_block);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001496
1497 if (!ret && ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001498 ret = walk_page_buffers(handle, page_buffers(page),
1499 from, to, NULL, do_journal_get_write_access);
1500 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001501
1502 if (ret) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001503 unlock_page(page);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001504 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001505 /*
1506 * block_write_begin may have instantiated a few blocks
1507 * outside i_size. Trim these off again. Don't need
1508 * i_size_read because we hold i_mutex.
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001509 *
1510 * Add inode to orphan list in case we crash before
1511 * truncate finishes
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001512 */
Jan Karaffacfa72009-07-13 16:22:22 -04001513 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001514 ext4_orphan_add(handle, inode);
1515
1516 ext4_journal_stop(handle);
1517 if (pos + len > inode->i_size) {
Jan Karaffacfa72009-07-13 16:22:22 -04001518 ext4_truncate(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001519 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001520 * If truncate failed early the inode might
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001521 * still be on the orphan list; we need to
1522 * make sure the inode is removed from the
1523 * orphan list in that case.
1524 */
1525 if (inode->i_nlink)
1526 ext4_orphan_del(NULL, inode);
1527 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001528 }
1529
Mingming Cao617ba132006-10-11 01:20:53 -07001530 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001531 goto retry;
Andrew Morton7479d2b2007-04-01 23:49:44 -07001532out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001533 return ret;
1534}
1535
Nick Pigginbfc1af62007-10-16 01:25:05 -07001536/* For write_end() in data=journal mode */
1537static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001538{
1539 if (!buffer_mapped(bh) || buffer_freed(bh))
1540 return 0;
1541 set_buffer_uptodate(bh);
Frank Mayhar03901312009-01-07 00:06:22 -05001542 return ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001543}
1544
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001545static int ext4_generic_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001546 struct address_space *mapping,
1547 loff_t pos, unsigned len, unsigned copied,
1548 struct page *page, void *fsdata)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001549{
1550 int i_size_changed = 0;
1551 struct inode *inode = mapping->host;
1552 handle_t *handle = ext4_journal_current_handle();
1553
1554 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
1555
1556 /*
1557 * No need to use i_size_read() here, the i_size
1558 * cannot change under us because we hold i_mutex.
1559 *
1560 * But it's important to update i_size while still holding page lock:
1561 * page writeout could otherwise come in and zero beyond i_size.
1562 */
1563 if (pos + copied > inode->i_size) {
1564 i_size_write(inode, pos + copied);
1565 i_size_changed = 1;
1566 }
1567
1568 if (pos + copied > EXT4_I(inode)->i_disksize) {
1569 /* We need to mark inode dirty even if
1570 * new_i_size is less that inode->i_size
1571 * bu greater than i_disksize.(hint delalloc)
1572 */
1573 ext4_update_i_disksize(inode, (pos + copied));
1574 i_size_changed = 1;
1575 }
1576 unlock_page(page);
1577 page_cache_release(page);
1578
1579 /*
1580 * Don't mark the inode dirty under page lock. First, it unnecessarily
1581 * makes the holding time of page lock longer. Second, it forces lock
1582 * ordering of page lock and transaction start for journaling
1583 * filesystems.
1584 */
1585 if (i_size_changed)
1586 ext4_mark_inode_dirty(handle, inode);
1587
1588 return copied;
1589}
1590
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001591/*
1592 * We need to pick up the new inode size which generic_commit_write gave us
1593 * `file' can be NULL - eg, when called from page_symlink().
1594 *
Mingming Cao617ba132006-10-11 01:20:53 -07001595 * ext4 never places buffers on inode->i_mapping->private_list. metadata
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001596 * buffers are managed internally.
1597 */
Nick Pigginbfc1af62007-10-16 01:25:05 -07001598static int ext4_ordered_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001599 struct address_space *mapping,
1600 loff_t pos, unsigned len, unsigned copied,
1601 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001602{
Mingming Cao617ba132006-10-11 01:20:53 -07001603 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001604 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001605 int ret = 0, ret2;
1606
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001607 trace_ext4_ordered_write_end(inode, pos, len, copied);
Jan Kara678aaf42008-07-11 19:27:31 -04001608 ret = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001609
1610 if (ret == 0) {
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001611 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001612 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001613 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -04001614 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001615 /* if we have allocated more blocks and copied
1616 * less. We will have blocks allocated outside
1617 * inode->i_size. So truncate them
1618 */
1619 ext4_orphan_add(handle, inode);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001620 if (ret2 < 0)
1621 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001622 }
Mingming Cao617ba132006-10-11 01:20:53 -07001623 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001624 if (!ret)
1625 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001626
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001627 if (pos + len > inode->i_size) {
Jan Karaffacfa72009-07-13 16:22:22 -04001628 ext4_truncate(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001629 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001630 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001631 * on the orphan list; we need to make sure the inode
1632 * is removed from the orphan list in that case.
1633 */
1634 if (inode->i_nlink)
1635 ext4_orphan_del(NULL, inode);
1636 }
1637
1638
Nick Pigginbfc1af62007-10-16 01:25:05 -07001639 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001640}
1641
Nick Pigginbfc1af62007-10-16 01:25:05 -07001642static int ext4_writeback_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001643 struct address_space *mapping,
1644 loff_t pos, unsigned len, unsigned copied,
1645 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001646{
Mingming Cao617ba132006-10-11 01:20:53 -07001647 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001648 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001649 int ret = 0, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001650
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001651 trace_ext4_writeback_write_end(inode, pos, len, copied);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001652 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001653 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001654 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -04001655 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001656 /* if we have allocated more blocks and copied
1657 * less. We will have blocks allocated outside
1658 * inode->i_size. So truncate them
1659 */
1660 ext4_orphan_add(handle, inode);
1661
Roel Kluinf8a87d82008-04-29 22:01:18 -04001662 if (ret2 < 0)
1663 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001664
Mingming Cao617ba132006-10-11 01:20:53 -07001665 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001666 if (!ret)
1667 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001668
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001669 if (pos + len > inode->i_size) {
Jan Karaffacfa72009-07-13 16:22:22 -04001670 ext4_truncate(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001671 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001672 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001673 * on the orphan list; we need to make sure the inode
1674 * is removed from the orphan list in that case.
1675 */
1676 if (inode->i_nlink)
1677 ext4_orphan_del(NULL, inode);
1678 }
1679
Nick Pigginbfc1af62007-10-16 01:25:05 -07001680 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001681}
1682
Nick Pigginbfc1af62007-10-16 01:25:05 -07001683static int ext4_journalled_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001684 struct address_space *mapping,
1685 loff_t pos, unsigned len, unsigned copied,
1686 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001687{
Mingming Cao617ba132006-10-11 01:20:53 -07001688 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001689 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001690 int ret = 0, ret2;
1691 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001692 unsigned from, to;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001693 loff_t new_i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001694
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001695 trace_ext4_journalled_write_end(inode, pos, len, copied);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001696 from = pos & (PAGE_CACHE_SIZE - 1);
1697 to = from + len;
1698
1699 if (copied < len) {
1700 if (!PageUptodate(page))
1701 copied = 0;
1702 page_zero_new_buffers(page, from+copied, to);
1703 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001704
1705 ret = walk_page_buffers(handle, page_buffers(page), from,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001706 to, &partial, write_end_fn);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001707 if (!partial)
1708 SetPageUptodate(page);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001709 new_i_size = pos + copied;
1710 if (new_i_size > inode->i_size)
Nick Pigginbfc1af62007-10-16 01:25:05 -07001711 i_size_write(inode, pos+copied);
Mingming Cao617ba132006-10-11 01:20:53 -07001712 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001713 if (new_i_size > EXT4_I(inode)->i_disksize) {
1714 ext4_update_i_disksize(inode, new_i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001715 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001716 if (!ret)
1717 ret = ret2;
1718 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001719
Jan Karacf108bc2008-07-11 19:27:31 -04001720 unlock_page(page);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001721 page_cache_release(page);
Jan Karaffacfa72009-07-13 16:22:22 -04001722 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001723 /* if we have allocated more blocks and copied
1724 * less. We will have blocks allocated outside
1725 * inode->i_size. So truncate them
1726 */
1727 ext4_orphan_add(handle, inode);
1728
Mingming Cao617ba132006-10-11 01:20:53 -07001729 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001730 if (!ret)
1731 ret = ret2;
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001732 if (pos + len > inode->i_size) {
Jan Karaffacfa72009-07-13 16:22:22 -04001733 ext4_truncate(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001734 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001735 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001736 * on the orphan list; we need to make sure the inode
1737 * is removed from the orphan list in that case.
1738 */
1739 if (inode->i_nlink)
1740 ext4_orphan_del(NULL, inode);
1741 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001742
1743 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001744}
Mingming Caod2a17632008-07-14 17:52:37 -04001745
1746static int ext4_da_reserve_space(struct inode *inode, int nrblocks)
1747{
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001748 int retries = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01001749 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1750 unsigned long md_needed, mdblocks, total = 0;
Mingming Caod2a17632008-07-14 17:52:37 -04001751
1752 /*
1753 * recalculate the amount of metadata blocks to reserve
1754 * in order to allocate nrblocks
1755 * worse case is one extent per block
1756 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001757repeat:
Mingming Caod2a17632008-07-14 17:52:37 -04001758 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1759 total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks;
1760 mdblocks = ext4_calc_metadata_amount(inode, total);
1761 BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks);
1762
1763 md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks;
1764 total = md_needed + nrblocks;
1765
Mingming Cao60e58e02009-01-22 18:13:05 +01001766 /*
1767 * Make quota reservation here to prevent quota overflow
1768 * later. Real quota accounting is done at pages writeout
1769 * time.
1770 */
1771 if (vfs_dq_reserve_block(inode, total)) {
1772 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1773 return -EDQUOT;
1774 }
1775
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04001776 if (ext4_claim_free_blocks(sbi, total)) {
Mingming Caod2a17632008-07-14 17:52:37 -04001777 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001778 if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1779 yield();
1780 goto repeat;
1781 }
Mingming Cao60e58e02009-01-22 18:13:05 +01001782 vfs_dq_release_reservation_block(inode, total);
Mingming Caod2a17632008-07-14 17:52:37 -04001783 return -ENOSPC;
1784 }
Mingming Caod2a17632008-07-14 17:52:37 -04001785 EXT4_I(inode)->i_reserved_data_blocks += nrblocks;
1786 EXT4_I(inode)->i_reserved_meta_blocks = mdblocks;
1787
1788 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1789 return 0; /* success */
1790}
1791
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001792static void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001793{
1794 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1795 int total, mdb, mdb_free, release;
1796
Mingming Caocd213222008-08-19 22:16:59 -04001797 if (!to_free)
1798 return; /* Nothing to release, exit */
1799
Mingming Caod2a17632008-07-14 17:52:37 -04001800 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001801
1802 if (!EXT4_I(inode)->i_reserved_data_blocks) {
1803 /*
1804 * if there is no reserved blocks, but we try to free some
1805 * then the counter is messed up somewhere.
1806 * but since this function is called from invalidate
1807 * page, it's harmless to return without any action
1808 */
1809 printk(KERN_INFO "ext4 delalloc try to release %d reserved "
1810 "blocks for inode %lu, but there is no reserved "
1811 "data blocks\n", to_free, inode->i_ino);
1812 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1813 return;
1814 }
1815
Mingming Caod2a17632008-07-14 17:52:37 -04001816 /* recalculate the number of metablocks still need to be reserved */
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001817 total = EXT4_I(inode)->i_reserved_data_blocks - to_free;
Mingming Caod2a17632008-07-14 17:52:37 -04001818 mdb = ext4_calc_metadata_amount(inode, total);
1819
1820 /* figure out how many metablocks to release */
1821 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1822 mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
1823
Mingming Caod2a17632008-07-14 17:52:37 -04001824 release = to_free + mdb_free;
1825
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001826 /* update fs dirty blocks counter for truncate case */
1827 percpu_counter_sub(&sbi->s_dirtyblocks_counter, release);
Mingming Caod2a17632008-07-14 17:52:37 -04001828
1829 /* update per-inode reservations */
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001830 BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks);
1831 EXT4_I(inode)->i_reserved_data_blocks -= to_free;
Mingming Caod2a17632008-07-14 17:52:37 -04001832
1833 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1834 EXT4_I(inode)->i_reserved_meta_blocks = mdb;
Mingming Caod2a17632008-07-14 17:52:37 -04001835 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001836
1837 vfs_dq_release_reservation_block(inode, release);
Mingming Caod2a17632008-07-14 17:52:37 -04001838}
1839
1840static void ext4_da_page_release_reservation(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001841 unsigned long offset)
Mingming Caod2a17632008-07-14 17:52:37 -04001842{
1843 int to_release = 0;
1844 struct buffer_head *head, *bh;
1845 unsigned int curr_off = 0;
1846
1847 head = page_buffers(page);
1848 bh = head;
1849 do {
1850 unsigned int next_off = curr_off + bh->b_size;
1851
1852 if ((offset <= curr_off) && (buffer_delay(bh))) {
1853 to_release++;
1854 clear_buffer_delay(bh);
1855 }
1856 curr_off = next_off;
1857 } while ((bh = bh->b_this_page) != head);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001858 ext4_da_release_space(page->mapping->host, to_release);
Mingming Caod2a17632008-07-14 17:52:37 -04001859}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001860
1861/*
Alex Tomas64769242008-07-11 19:27:31 -04001862 * Delayed allocation stuff
1863 */
1864
1865struct mpage_da_data {
1866 struct inode *inode;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001867 sector_t b_blocknr; /* start block number of extent */
1868 size_t b_size; /* size of extent */
1869 unsigned long b_state; /* state of the extent */
Alex Tomas64769242008-07-11 19:27:31 -04001870 unsigned long first_page, next_page; /* extent of pages */
Alex Tomas64769242008-07-11 19:27:31 -04001871 struct writeback_control *wbc;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001872 int io_done;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001873 int pages_written;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001874 int retval;
Alex Tomas64769242008-07-11 19:27:31 -04001875};
1876
1877/*
1878 * mpage_da_submit_io - walks through extent of pages and try to write
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001879 * them with writepage() call back
Alex Tomas64769242008-07-11 19:27:31 -04001880 *
1881 * @mpd->inode: inode
1882 * @mpd->first_page: first page of the extent
1883 * @mpd->next_page: page after the last page of the extent
Alex Tomas64769242008-07-11 19:27:31 -04001884 *
1885 * By the time mpage_da_submit_io() is called we expect all blocks
1886 * to be allocated. this may be wrong if allocation failed.
1887 *
1888 * As pages are already locked by write_cache_pages(), we can't use it
1889 */
1890static int mpage_da_submit_io(struct mpage_da_data *mpd)
1891{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001892 long pages_skipped;
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001893 struct pagevec pvec;
1894 unsigned long index, end;
1895 int ret = 0, err, nr_pages, i;
1896 struct inode *inode = mpd->inode;
1897 struct address_space *mapping = inode->i_mapping;
Alex Tomas64769242008-07-11 19:27:31 -04001898
1899 BUG_ON(mpd->next_page <= mpd->first_page);
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001900 /*
1901 * We need to start from the first_page to the next_page - 1
1902 * to make sure we also write the mapped dirty buffer_heads.
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001903 * If we look at mpd->b_blocknr we would only be looking
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001904 * at the currently mapped buffer_heads.
1905 */
Alex Tomas64769242008-07-11 19:27:31 -04001906 index = mpd->first_page;
1907 end = mpd->next_page - 1;
1908
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001909 pagevec_init(&pvec, 0);
Alex Tomas64769242008-07-11 19:27:31 -04001910 while (index <= end) {
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001911 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
Alex Tomas64769242008-07-11 19:27:31 -04001912 if (nr_pages == 0)
1913 break;
1914 for (i = 0; i < nr_pages; i++) {
1915 struct page *page = pvec.pages[i];
1916
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001917 index = page->index;
1918 if (index > end)
1919 break;
1920 index++;
1921
1922 BUG_ON(!PageLocked(page));
1923 BUG_ON(PageWriteback(page));
1924
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001925 pages_skipped = mpd->wbc->pages_skipped;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001926 err = mapping->a_ops->writepage(page, mpd->wbc);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001927 if (!err && (pages_skipped == mpd->wbc->pages_skipped))
1928 /*
1929 * have successfully written the page
1930 * without skipping the same
1931 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001932 mpd->pages_written++;
Alex Tomas64769242008-07-11 19:27:31 -04001933 /*
1934 * In error case, we have to continue because
1935 * remaining pages are still locked
1936 * XXX: unlock and re-dirty them?
1937 */
1938 if (ret == 0)
1939 ret = err;
1940 }
1941 pagevec_release(&pvec);
1942 }
Alex Tomas64769242008-07-11 19:27:31 -04001943 return ret;
1944}
1945
1946/*
1947 * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers
1948 *
1949 * @mpd->inode - inode to walk through
1950 * @exbh->b_blocknr - first block on a disk
1951 * @exbh->b_size - amount of space in bytes
1952 * @logical - first logical block to start assignment with
1953 *
1954 * the function goes through all passed space and put actual disk
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001955 * block numbers into buffer heads, dropping BH_Delay and BH_Unwritten
Alex Tomas64769242008-07-11 19:27:31 -04001956 */
1957static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical,
1958 struct buffer_head *exbh)
1959{
1960 struct inode *inode = mpd->inode;
1961 struct address_space *mapping = inode->i_mapping;
1962 int blocks = exbh->b_size >> inode->i_blkbits;
1963 sector_t pblock = exbh->b_blocknr, cur_logical;
1964 struct buffer_head *head, *bh;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001965 pgoff_t index, end;
Alex Tomas64769242008-07-11 19:27:31 -04001966 struct pagevec pvec;
1967 int nr_pages, i;
1968
1969 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
1970 end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
1971 cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1972
1973 pagevec_init(&pvec, 0);
1974
1975 while (index <= end) {
1976 /* XXX: optimize tail */
1977 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1978 if (nr_pages == 0)
1979 break;
1980 for (i = 0; i < nr_pages; i++) {
1981 struct page *page = pvec.pages[i];
1982
1983 index = page->index;
1984 if (index > end)
1985 break;
1986 index++;
1987
1988 BUG_ON(!PageLocked(page));
1989 BUG_ON(PageWriteback(page));
1990 BUG_ON(!page_has_buffers(page));
1991
1992 bh = page_buffers(page);
1993 head = bh;
1994
1995 /* skip blocks out of the range */
1996 do {
1997 if (cur_logical >= logical)
1998 break;
1999 cur_logical++;
2000 } while ((bh = bh->b_this_page) != head);
2001
2002 do {
2003 if (cur_logical >= logical + blocks)
2004 break;
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002005
2006 if (buffer_delay(bh) ||
2007 buffer_unwritten(bh)) {
2008
2009 BUG_ON(bh->b_bdev != inode->i_sb->s_bdev);
2010
2011 if (buffer_delay(bh)) {
2012 clear_buffer_delay(bh);
2013 bh->b_blocknr = pblock;
2014 } else {
2015 /*
2016 * unwritten already should have
2017 * blocknr assigned. Verify that
2018 */
2019 clear_buffer_unwritten(bh);
2020 BUG_ON(bh->b_blocknr != pblock);
2021 }
2022
Mingming Cao61628a32008-07-11 19:27:31 -04002023 } else if (buffer_mapped(bh))
Alex Tomas64769242008-07-11 19:27:31 -04002024 BUG_ON(bh->b_blocknr != pblock);
Alex Tomas64769242008-07-11 19:27:31 -04002025
2026 cur_logical++;
2027 pblock++;
2028 } while ((bh = bh->b_this_page) != head);
2029 }
2030 pagevec_release(&pvec);
2031 }
2032}
2033
2034
2035/*
2036 * __unmap_underlying_blocks - just a helper function to unmap
2037 * set of blocks described by @bh
2038 */
2039static inline void __unmap_underlying_blocks(struct inode *inode,
2040 struct buffer_head *bh)
2041{
2042 struct block_device *bdev = inode->i_sb->s_bdev;
2043 int blocks, i;
2044
2045 blocks = bh->b_size >> inode->i_blkbits;
2046 for (i = 0; i < blocks; i++)
2047 unmap_underlying_metadata(bdev, bh->b_blocknr + i);
2048}
2049
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002050static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd,
2051 sector_t logical, long blk_cnt)
2052{
2053 int nr_pages, i;
2054 pgoff_t index, end;
2055 struct pagevec pvec;
2056 struct inode *inode = mpd->inode;
2057 struct address_space *mapping = inode->i_mapping;
2058
2059 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
2060 end = (logical + blk_cnt - 1) >>
2061 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2062 while (index <= end) {
2063 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
2064 if (nr_pages == 0)
2065 break;
2066 for (i = 0; i < nr_pages; i++) {
2067 struct page *page = pvec.pages[i];
2068 index = page->index;
2069 if (index > end)
2070 break;
2071 index++;
2072
2073 BUG_ON(!PageLocked(page));
2074 BUG_ON(PageWriteback(page));
2075 block_invalidatepage(page, 0);
2076 ClearPageUptodate(page);
2077 unlock_page(page);
2078 }
2079 }
2080 return;
2081}
2082
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002083static void ext4_print_free_blocks(struct inode *inode)
2084{
2085 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2086 printk(KERN_EMERG "Total free blocks count %lld\n",
2087 ext4_count_free_blocks(inode->i_sb));
2088 printk(KERN_EMERG "Free/Dirty block details\n");
2089 printk(KERN_EMERG "free_blocks=%lld\n",
Alexander Beregalov8f72fbd2008-10-29 17:13:08 -04002090 (long long)percpu_counter_sum(&sbi->s_freeblocks_counter));
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002091 printk(KERN_EMERG "dirty_blocks=%lld\n",
Alexander Beregalov8f72fbd2008-10-29 17:13:08 -04002092 (long long)percpu_counter_sum(&sbi->s_dirtyblocks_counter));
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002093 printk(KERN_EMERG "Block reservation details\n");
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002094 printk(KERN_EMERG "i_reserved_data_blocks=%u\n",
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002095 EXT4_I(inode)->i_reserved_data_blocks);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002096 printk(KERN_EMERG "i_reserved_meta_blocks=%u\n",
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002097 EXT4_I(inode)->i_reserved_meta_blocks);
2098 return;
2099}
2100
Theodore Ts'ob920c752009-05-14 00:54:29 -04002101/*
Alex Tomas64769242008-07-11 19:27:31 -04002102 * mpage_da_map_blocks - go through given space
2103 *
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002104 * @mpd - bh describing space
Alex Tomas64769242008-07-11 19:27:31 -04002105 *
2106 * The function skips space we know is already mapped to disk blocks.
2107 *
Alex Tomas64769242008-07-11 19:27:31 -04002108 */
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002109static int mpage_da_map_blocks(struct mpage_da_data *mpd)
Alex Tomas64769242008-07-11 19:27:31 -04002110{
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002111 int err, blks, get_blocks_flags;
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002112 struct buffer_head new;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002113 sector_t next = mpd->b_blocknr;
2114 unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
2115 loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
2116 handle_t *handle = NULL;
Alex Tomas64769242008-07-11 19:27:31 -04002117
2118 /*
2119 * We consider only non-mapped and non-allocated blocks
2120 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002121 if ((mpd->b_state & (1 << BH_Mapped)) &&
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002122 !(mpd->b_state & (1 << BH_Delay)) &&
2123 !(mpd->b_state & (1 << BH_Unwritten)))
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002124 return 0;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002125
2126 /*
2127 * If we didn't accumulate anything to write simply return
2128 */
2129 if (!mpd->b_size)
2130 return 0;
2131
2132 handle = ext4_journal_current_handle();
2133 BUG_ON(!handle);
2134
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04002135 /*
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002136 * Call ext4_get_blocks() to allocate any delayed allocation
2137 * blocks, or to convert an uninitialized extent to be
2138 * initialized (in the case where we have written into
2139 * one or more preallocated blocks).
2140 *
2141 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
2142 * indicate that we are on the delayed allocation path. This
2143 * affects functions in many different parts of the allocation
2144 * call path. This flag exists primarily because we don't
2145 * want to change *many* call functions, so ext4_get_blocks()
2146 * will set the magic i_delalloc_reserved_flag once the
2147 * inode's allocation semaphore is taken.
2148 *
2149 * If the blocks in questions were delalloc blocks, set
2150 * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
2151 * variables are updated after the blocks have been allocated.
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04002152 */
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002153 new.b_state = 0;
2154 get_blocks_flags = (EXT4_GET_BLOCKS_CREATE |
2155 EXT4_GET_BLOCKS_DELALLOC_RESERVE);
2156 if (mpd->b_state & (1 << BH_Delay))
2157 get_blocks_flags |= EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002158 blks = ext4_get_blocks(handle, mpd->inode, next, max_blocks,
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002159 &new, get_blocks_flags);
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002160 if (blks < 0) {
2161 err = blks;
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002162 /*
2163 * If get block returns with error we simply
2164 * return. Later writepage will redirty the page and
2165 * writepages will find the dirty page again
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002166 */
2167 if (err == -EAGAIN)
2168 return 0;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002169
2170 if (err == -ENOSPC &&
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002171 ext4_count_free_blocks(mpd->inode->i_sb)) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002172 mpd->retval = err;
2173 return 0;
2174 }
2175
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002176 /*
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002177 * get block failure will cause us to loop in
2178 * writepages, because a_ops->writepage won't be able
2179 * to make progress. The page will be redirtied by
2180 * writepage and writepages will again try to write
2181 * the same.
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002182 */
2183 printk(KERN_EMERG "%s block allocation failed for inode %lu "
2184 "at logical offset %llu with max blocks "
2185 "%zd with error %d\n",
2186 __func__, mpd->inode->i_ino,
2187 (unsigned long long)next,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002188 mpd->b_size >> mpd->inode->i_blkbits, err);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002189 printk(KERN_EMERG "This should not happen.!! "
2190 "Data will be lost\n");
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002191 if (err == -ENOSPC) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002192 ext4_print_free_blocks(mpd->inode);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002193 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002194 /* invalidate all the pages */
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002195 ext4_da_block_invalidatepages(mpd, next,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002196 mpd->b_size >> mpd->inode->i_blkbits);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002197 return err;
2198 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002199 BUG_ON(blks == 0);
2200
2201 new.b_size = (blks << mpd->inode->i_blkbits);
Alex Tomas64769242008-07-11 19:27:31 -04002202
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002203 if (buffer_new(&new))
2204 __unmap_underlying_blocks(mpd->inode, &new);
Alex Tomas64769242008-07-11 19:27:31 -04002205
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002206 /*
2207 * If blocks are delayed marked, we need to
2208 * put actual blocknr and drop delayed bit
2209 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002210 if ((mpd->b_state & (1 << BH_Delay)) ||
2211 (mpd->b_state & (1 << BH_Unwritten)))
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002212 mpage_put_bnr_to_bhs(mpd, next, &new);
2213
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002214 if (ext4_should_order_data(mpd->inode)) {
2215 err = ext4_jbd2_file_inode(handle, mpd->inode);
2216 if (err)
2217 return err;
2218 }
2219
2220 /*
Jan Kara03f5d8b2009-06-09 00:17:05 -04002221 * Update on-disk size along with block allocation.
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002222 */
2223 disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
2224 if (disksize > i_size_read(mpd->inode))
2225 disksize = i_size_read(mpd->inode);
2226 if (disksize > EXT4_I(mpd->inode)->i_disksize) {
2227 ext4_update_i_disksize(mpd->inode, disksize);
2228 return ext4_mark_inode_dirty(handle, mpd->inode);
2229 }
2230
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002231 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04002232}
2233
Aneesh Kumar K.Vbf068ee2008-08-19 22:16:43 -04002234#define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
2235 (1 << BH_Delay) | (1 << BH_Unwritten))
Alex Tomas64769242008-07-11 19:27:31 -04002236
2237/*
2238 * mpage_add_bh_to_extent - try to add one more block to extent of blocks
2239 *
2240 * @mpd->lbh - extent of blocks
2241 * @logical - logical number of the block in the file
2242 * @bh - bh of the block (used to access block's state)
2243 *
2244 * the function is used to collect contig. blocks in same state
2245 */
2246static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002247 sector_t logical, size_t b_size,
2248 unsigned long b_state)
Alex Tomas64769242008-07-11 19:27:31 -04002249{
Alex Tomas64769242008-07-11 19:27:31 -04002250 sector_t next;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002251 int nrblocks = mpd->b_size >> mpd->inode->i_blkbits;
Alex Tomas64769242008-07-11 19:27:31 -04002252
Mingming Cao525f4ed2008-08-19 22:15:58 -04002253 /* check if thereserved journal credits might overflow */
2254 if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) {
2255 if (nrblocks >= EXT4_MAX_TRANS_DATA) {
2256 /*
2257 * With non-extent format we are limited by the journal
2258 * credit available. Total credit needed to insert
2259 * nrblocks contiguous blocks is dependent on the
2260 * nrblocks. So limit nrblocks.
2261 */
2262 goto flush_it;
2263 } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
2264 EXT4_MAX_TRANS_DATA) {
2265 /*
2266 * Adding the new buffer_head would make it cross the
2267 * allowed limit for which we have journal credit
2268 * reserved. So limit the new bh->b_size
2269 */
2270 b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
2271 mpd->inode->i_blkbits;
2272 /* we will do mpage_da_submit_io in the next loop */
2273 }
2274 }
Alex Tomas64769242008-07-11 19:27:31 -04002275 /*
2276 * First block in the extent
2277 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002278 if (mpd->b_size == 0) {
2279 mpd->b_blocknr = logical;
2280 mpd->b_size = b_size;
2281 mpd->b_state = b_state & BH_FLAGS;
Alex Tomas64769242008-07-11 19:27:31 -04002282 return;
2283 }
2284
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002285 next = mpd->b_blocknr + nrblocks;
Alex Tomas64769242008-07-11 19:27:31 -04002286 /*
2287 * Can we merge the block to our big extent?
2288 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002289 if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
2290 mpd->b_size += b_size;
Alex Tomas64769242008-07-11 19:27:31 -04002291 return;
2292 }
2293
Mingming Cao525f4ed2008-08-19 22:15:58 -04002294flush_it:
Alex Tomas64769242008-07-11 19:27:31 -04002295 /*
2296 * We couldn't merge the block to our extent, so we
2297 * need to flush current extent and start new one
2298 */
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002299 if (mpage_da_map_blocks(mpd) == 0)
2300 mpage_da_submit_io(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002301 mpd->io_done = 1;
2302 return;
Alex Tomas64769242008-07-11 19:27:31 -04002303}
2304
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002305static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002306{
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002307 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002308}
2309
Alex Tomas64769242008-07-11 19:27:31 -04002310/*
2311 * __mpage_da_writepage - finds extent of pages and blocks
2312 *
2313 * @page: page to consider
2314 * @wbc: not used, we just follow rules
2315 * @data: context
2316 *
2317 * The function finds extents of pages and scan them for all blocks.
2318 */
2319static int __mpage_da_writepage(struct page *page,
2320 struct writeback_control *wbc, void *data)
2321{
2322 struct mpage_da_data *mpd = data;
2323 struct inode *inode = mpd->inode;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002324 struct buffer_head *bh, *head;
Alex Tomas64769242008-07-11 19:27:31 -04002325 sector_t logical;
2326
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002327 if (mpd->io_done) {
2328 /*
2329 * Rest of the page in the page_vec
2330 * redirty then and skip then. We will
2331 * try to to write them again after
2332 * starting a new transaction
2333 */
2334 redirty_page_for_writepage(wbc, page);
2335 unlock_page(page);
2336 return MPAGE_DA_EXTENT_TAIL;
2337 }
Alex Tomas64769242008-07-11 19:27:31 -04002338 /*
2339 * Can we merge this page to current extent?
2340 */
2341 if (mpd->next_page != page->index) {
2342 /*
2343 * Nope, we can't. So, we map non-allocated blocks
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002344 * and start IO on them using writepage()
Alex Tomas64769242008-07-11 19:27:31 -04002345 */
2346 if (mpd->next_page != mpd->first_page) {
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002347 if (mpage_da_map_blocks(mpd) == 0)
2348 mpage_da_submit_io(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002349 /*
2350 * skip rest of the page in the page_vec
2351 */
2352 mpd->io_done = 1;
2353 redirty_page_for_writepage(wbc, page);
2354 unlock_page(page);
2355 return MPAGE_DA_EXTENT_TAIL;
Alex Tomas64769242008-07-11 19:27:31 -04002356 }
2357
2358 /*
2359 * Start next extent of pages ...
2360 */
2361 mpd->first_page = page->index;
2362
2363 /*
2364 * ... and blocks
2365 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002366 mpd->b_size = 0;
2367 mpd->b_state = 0;
2368 mpd->b_blocknr = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002369 }
2370
2371 mpd->next_page = page->index + 1;
2372 logical = (sector_t) page->index <<
2373 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2374
2375 if (!page_has_buffers(page)) {
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002376 mpage_add_bh_to_extent(mpd, logical, PAGE_CACHE_SIZE,
2377 (1 << BH_Dirty) | (1 << BH_Uptodate));
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002378 if (mpd->io_done)
2379 return MPAGE_DA_EXTENT_TAIL;
Alex Tomas64769242008-07-11 19:27:31 -04002380 } else {
2381 /*
2382 * Page with regular buffer heads, just add all dirty ones
2383 */
2384 head = page_buffers(page);
2385 bh = head;
2386 do {
2387 BUG_ON(buffer_locked(bh));
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002388 /*
2389 * We need to try to allocate
2390 * unmapped blocks in the same page.
2391 * Otherwise we won't make progress
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002392 * with the page in ext4_writepage
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002393 */
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002394 if (ext4_bh_delay_or_unwritten(NULL, bh)) {
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002395 mpage_add_bh_to_extent(mpd, logical,
2396 bh->b_size,
2397 bh->b_state);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002398 if (mpd->io_done)
2399 return MPAGE_DA_EXTENT_TAIL;
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002400 } else if (buffer_dirty(bh) && (buffer_mapped(bh))) {
2401 /*
2402 * mapped dirty buffer. We need to update
2403 * the b_state because we look at
2404 * b_state in mpage_da_map_blocks. We don't
2405 * update b_size because if we find an
2406 * unmapped buffer_head later we need to
2407 * use the b_state flag of that buffer_head.
2408 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002409 if (mpd->b_size == 0)
2410 mpd->b_state = bh->b_state & BH_FLAGS;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002411 }
Alex Tomas64769242008-07-11 19:27:31 -04002412 logical++;
2413 } while ((bh = bh->b_this_page) != head);
2414 }
2415
2416 return 0;
2417}
2418
2419/*
Theodore Ts'ob920c752009-05-14 00:54:29 -04002420 * This is a special get_blocks_t callback which is used by
2421 * ext4_da_write_begin(). It will either return mapped block or
2422 * reserve space for a single block.
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002423 *
2424 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
2425 * We also have b_blocknr = -1 and b_bdev initialized properly
2426 *
2427 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
2428 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
2429 * initialized properly.
Alex Tomas64769242008-07-11 19:27:31 -04002430 */
2431static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
2432 struct buffer_head *bh_result, int create)
2433{
2434 int ret = 0;
Aneesh Kumar K.V33b98172009-05-12 14:40:37 -04002435 sector_t invalid_block = ~((sector_t) 0xffff);
2436
2437 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
2438 invalid_block = ~0;
Alex Tomas64769242008-07-11 19:27:31 -04002439
2440 BUG_ON(create == 0);
2441 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
2442
2443 /*
2444 * first, we need to know whether the block is allocated already
2445 * preallocated blocks are unmapped but should treated
2446 * the same as allocated blocks.
2447 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04002448 ret = ext4_get_blocks(NULL, inode, iblock, 1, bh_result, 0);
Mingming Caod2a17632008-07-14 17:52:37 -04002449 if ((ret == 0) && !buffer_delay(bh_result)) {
2450 /* the block isn't (pre)allocated yet, let's reserve space */
Alex Tomas64769242008-07-11 19:27:31 -04002451 /*
2452 * XXX: __block_prepare_write() unmaps passed block,
2453 * is it OK?
2454 */
Mingming Caod2a17632008-07-14 17:52:37 -04002455 ret = ext4_da_reserve_space(inode, 1);
2456 if (ret)
2457 /* not enough space to reserve */
2458 return ret;
2459
Aneesh Kumar K.V33b98172009-05-12 14:40:37 -04002460 map_bh(bh_result, inode->i_sb, invalid_block);
Alex Tomas64769242008-07-11 19:27:31 -04002461 set_buffer_new(bh_result);
2462 set_buffer_delay(bh_result);
2463 } else if (ret > 0) {
2464 bh_result->b_size = (ret << inode->i_blkbits);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002465 if (buffer_unwritten(bh_result)) {
2466 /* A delayed write to unwritten bh should
2467 * be marked new and mapped. Mapped ensures
2468 * that we don't do get_block multiple times
2469 * when we write to the same offset and new
2470 * ensures that we do proper zero out for
2471 * partial write.
2472 */
Aneesh Kumar K.V9c1ee182009-05-13 18:36:58 -04002473 set_buffer_new(bh_result);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002474 set_buffer_mapped(bh_result);
2475 }
Alex Tomas64769242008-07-11 19:27:31 -04002476 ret = 0;
2477 }
2478
2479 return ret;
2480}
Mingming Cao61628a32008-07-11 19:27:31 -04002481
Theodore Ts'ob920c752009-05-14 00:54:29 -04002482/*
2483 * This function is used as a standard get_block_t calback function
2484 * when there is no desire to allocate any blocks. It is used as a
2485 * callback function for block_prepare_write(), nobh_writepage(), and
2486 * block_write_full_page(). These functions should only try to map a
2487 * single block at a time.
2488 *
2489 * Since this function doesn't do block allocations even if the caller
2490 * requests it by passing in create=1, it is critically important that
2491 * any caller checks to make sure that any buffer heads are returned
2492 * by this function are either all already mapped or marked for
2493 * delayed allocation before calling nobh_writepage() or
2494 * block_write_full_page(). Otherwise, b_blocknr could be left
2495 * unitialized, and the page write functions will be taken by
2496 * surprise.
2497 */
2498static int noalloc_get_block_write(struct inode *inode, sector_t iblock,
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002499 struct buffer_head *bh_result, int create)
2500{
2501 int ret = 0;
2502 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
2503
Theodore Ts'oa2dc52b2009-05-12 13:51:29 -04002504 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
2505
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002506 /*
2507 * we don't want to do block allocation in writepage
2508 * so call get_block_wrap with create = 0
2509 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04002510 ret = ext4_get_blocks(NULL, inode, iblock, max_blocks, bh_result, 0);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002511 if (ret > 0) {
2512 bh_result->b_size = (ret << inode->i_blkbits);
2513 ret = 0;
2514 }
2515 return ret;
Mingming Cao61628a32008-07-11 19:27:31 -04002516}
2517
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002518static int bget_one(handle_t *handle, struct buffer_head *bh)
2519{
2520 get_bh(bh);
2521 return 0;
2522}
2523
2524static int bput_one(handle_t *handle, struct buffer_head *bh)
2525{
2526 put_bh(bh);
2527 return 0;
2528}
2529
2530static int __ext4_journalled_writepage(struct page *page,
2531 struct writeback_control *wbc,
2532 unsigned int len)
2533{
2534 struct address_space *mapping = page->mapping;
2535 struct inode *inode = mapping->host;
2536 struct buffer_head *page_bufs;
2537 handle_t *handle = NULL;
2538 int ret = 0;
2539 int err;
2540
2541 page_bufs = page_buffers(page);
2542 BUG_ON(!page_bufs);
2543 walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one);
2544 /* As soon as we unlock the page, it can go away, but we have
2545 * references to buffers so we are safe */
2546 unlock_page(page);
2547
2548 handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
2549 if (IS_ERR(handle)) {
2550 ret = PTR_ERR(handle);
2551 goto out;
2552 }
2553
2554 ret = walk_page_buffers(handle, page_bufs, 0, len, NULL,
2555 do_journal_get_write_access);
2556
2557 err = walk_page_buffers(handle, page_bufs, 0, len, NULL,
2558 write_end_fn);
2559 if (ret == 0)
2560 ret = err;
2561 err = ext4_journal_stop(handle);
2562 if (!ret)
2563 ret = err;
2564
2565 walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one);
2566 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
2567out:
2568 return ret;
2569}
2570
Mingming Cao61628a32008-07-11 19:27:31 -04002571/*
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002572 * Note that we don't need to start a transaction unless we're journaling data
2573 * because we should have holes filled from ext4_page_mkwrite(). We even don't
2574 * need to file the inode to the transaction's list in ordered mode because if
2575 * we are writing back data added by write(), the inode is already there and if
2576 * we are writing back data modified via mmap(), noone guarantees in which
2577 * transaction the data will hit the disk. In case we are journaling data, we
2578 * cannot start transaction directly because transaction start ranks above page
2579 * lock so we have to do some magic.
2580 *
Theodore Ts'ob920c752009-05-14 00:54:29 -04002581 * This function can get called via...
2582 * - ext4_da_writepages after taking page lock (have journal handle)
2583 * - journal_submit_inode_data_buffers (no journal handle)
2584 * - shrink_page_list via pdflush (no journal handle)
2585 * - grab_page_cache when doing write_begin (have journal handle)
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002586 *
2587 * We don't do any block allocation in this function. If we have page with
2588 * multiple blocks we need to write those buffer_heads that are mapped. This
2589 * is important for mmaped based write. So if we do with blocksize 1K
2590 * truncate(f, 1024);
2591 * a = mmap(f, 0, 4096);
2592 * a[0] = 'a';
2593 * truncate(f, 4096);
2594 * we have in the page first buffer_head mapped via page_mkwrite call back
2595 * but other bufer_heads would be unmapped but dirty(dirty done via the
2596 * do_wp_page). So writepage should write the first block. If we modify
2597 * the mmap area beyond 1024 we will again get a page_fault and the
2598 * page_mkwrite callback will do the block allocation and mark the
2599 * buffer_heads mapped.
2600 *
2601 * We redirty the page if we have any buffer_heads that is either delay or
2602 * unwritten in the page.
2603 *
2604 * We can get recursively called as show below.
2605 *
2606 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
2607 * ext4_writepage()
2608 *
2609 * But since we don't do any block allocation we should not deadlock.
2610 * Page also have the dirty flag cleared so we don't get recurive page_lock.
Mingming Cao61628a32008-07-11 19:27:31 -04002611 */
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002612static int ext4_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002613 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002614{
Alex Tomas64769242008-07-11 19:27:31 -04002615 int ret = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002616 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002617 unsigned int len;
Mingming Cao61628a32008-07-11 19:27:31 -04002618 struct buffer_head *page_bufs;
2619 struct inode *inode = page->mapping->host;
Alex Tomas64769242008-07-11 19:27:31 -04002620
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002621 trace_ext4_writepage(inode, page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002622 size = i_size_read(inode);
2623 if (page->index == size >> PAGE_CACHE_SHIFT)
2624 len = size & ~PAGE_CACHE_MASK;
2625 else
2626 len = PAGE_CACHE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04002627
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002628 if (page_has_buffers(page)) {
Mingming Cao61628a32008-07-11 19:27:31 -04002629 page_bufs = page_buffers(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002630 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002631 ext4_bh_delay_or_unwritten)) {
Mingming Cao61628a32008-07-11 19:27:31 -04002632 /*
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002633 * We don't want to do block allocation
2634 * So redirty the page and return
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04002635 * We may reach here when we do a journal commit
2636 * via journal_submit_inode_data_buffers.
2637 * If we don't have mapping block we just ignore
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002638 * them. We can also reach here via shrink_page_list
2639 */
2640 redirty_page_for_writepage(wbc, page);
2641 unlock_page(page);
2642 return 0;
2643 }
2644 } else {
2645 /*
2646 * The test for page_has_buffers() is subtle:
2647 * We know the page is dirty but it lost buffers. That means
2648 * that at some moment in time after write_begin()/write_end()
2649 * has been called all buffers have been clean and thus they
2650 * must have been written at least once. So they are all
2651 * mapped and we can happily proceed with mapping them
2652 * and writing the page.
2653 *
2654 * Try to initialize the buffer_heads and check whether
2655 * all are mapped and non delay. We don't want to
2656 * do block allocation here.
2657 */
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04002658 ret = block_prepare_write(page, 0, len,
Theodore Ts'ob920c752009-05-14 00:54:29 -04002659 noalloc_get_block_write);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002660 if (!ret) {
2661 page_bufs = page_buffers(page);
2662 /* check whether all are mapped and non delay */
2663 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002664 ext4_bh_delay_or_unwritten)) {
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002665 redirty_page_for_writepage(wbc, page);
2666 unlock_page(page);
2667 return 0;
2668 }
2669 } else {
2670 /*
2671 * We can't do block allocation here
2672 * so just redity the page and unlock
2673 * and return
Mingming Cao61628a32008-07-11 19:27:31 -04002674 */
Mingming Cao61628a32008-07-11 19:27:31 -04002675 redirty_page_for_writepage(wbc, page);
2676 unlock_page(page);
2677 return 0;
2678 }
Aneesh Kumar K.Ved9b3e32008-11-07 09:06:45 -05002679 /* now mark the buffer_heads as dirty and uptodate */
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04002680 block_commit_write(page, 0, len);
Alex Tomas64769242008-07-11 19:27:31 -04002681 }
2682
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002683 if (PageChecked(page) && ext4_should_journal_data(inode)) {
2684 /*
2685 * It's mmapped pagecache. Add buffers and journal it. There
2686 * doesn't seem much point in redirtying the page here.
2687 */
2688 ClearPageChecked(page);
2689 return __ext4_journalled_writepage(page, wbc, len);
2690 }
2691
Alex Tomas64769242008-07-11 19:27:31 -04002692 if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode))
Theodore Ts'ob920c752009-05-14 00:54:29 -04002693 ret = nobh_writepage(page, noalloc_get_block_write, wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002694 else
Theodore Ts'ob920c752009-05-14 00:54:29 -04002695 ret = block_write_full_page(page, noalloc_get_block_write,
2696 wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002697
Alex Tomas64769242008-07-11 19:27:31 -04002698 return ret;
2699}
2700
Mingming Cao61628a32008-07-11 19:27:31 -04002701/*
Mingming Cao525f4ed2008-08-19 22:15:58 -04002702 * This is called via ext4_da_writepages() to
2703 * calulate the total number of credits to reserve to fit
2704 * a single extent allocation into a single transaction,
2705 * ext4_da_writpeages() will loop calling this before
2706 * the block allocation.
Mingming Cao61628a32008-07-11 19:27:31 -04002707 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002708
2709static int ext4_da_writepages_trans_blocks(struct inode *inode)
2710{
2711 int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
2712
2713 /*
2714 * With non-extent format the journal credit needed to
2715 * insert nrblocks contiguous block is dependent on
2716 * number of contiguous block. So we will limit
2717 * number of contiguous block to a sane value
2718 */
2719 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2720 (max_blocks > EXT4_MAX_TRANS_DATA))
2721 max_blocks = EXT4_MAX_TRANS_DATA;
2722
2723 return ext4_chunk_trans_blocks(inode, max_blocks);
2724}
Mingming Cao61628a32008-07-11 19:27:31 -04002725
Alex Tomas64769242008-07-11 19:27:31 -04002726static int ext4_da_writepages(struct address_space *mapping,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002727 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002728{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002729 pgoff_t index;
2730 int range_whole = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002731 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002732 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002733 struct inode *inode = mapping->host;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002734 int no_nrwrite_index_update;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002735 int pages_written = 0;
2736 long pages_skipped;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002737 int range_cyclic, cycled = 1, io_done = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002738 int needed_blocks, ret = 0, nr_to_writebump = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002739 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Mingming Cao61628a32008-07-11 19:27:31 -04002740
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002741 trace_ext4_da_writepages(inode, wbc);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002742
Mingming Cao61628a32008-07-11 19:27:31 -04002743 /*
2744 * No pages to write? This is mainly a kludge to avoid starting
2745 * a transaction for special inodes like journal inode on last iput()
2746 * because that could violate lock ordering on umount
2747 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002748 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Mingming Cao61628a32008-07-11 19:27:31 -04002749 return 0;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002750
2751 /*
2752 * If the filesystem has aborted, it is read-only, so return
2753 * right away instead of dumping stack traces later on that
2754 * will obscure the real source of the problem. We test
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002755 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002756 * the latter could be true if the filesystem is mounted
2757 * read-only, and in that case, ext4_da_writepages should
2758 * *never* be called, so if that ever happens, we would want
2759 * the stack trace.
2760 */
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002761 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002762 return -EROFS;
2763
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002764 /*
2765 * Make sure nr_to_write is >= sbi->s_mb_stream_request
2766 * This make sure small files blocks are allocated in
2767 * single attempt. This ensure that small files
2768 * get less fragmented.
2769 */
2770 if (wbc->nr_to_write < sbi->s_mb_stream_request) {
2771 nr_to_writebump = sbi->s_mb_stream_request - wbc->nr_to_write;
2772 wbc->nr_to_write = sbi->s_mb_stream_request;
2773 }
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002774 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2775 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002776
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002777 range_cyclic = wbc->range_cyclic;
2778 if (wbc->range_cyclic) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002779 index = mapping->writeback_index;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002780 if (index)
2781 cycled = 0;
2782 wbc->range_start = index << PAGE_CACHE_SHIFT;
2783 wbc->range_end = LLONG_MAX;
2784 wbc->range_cyclic = 0;
2785 } else
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002786 index = wbc->range_start >> PAGE_CACHE_SHIFT;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002787
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002788 mpd.wbc = wbc;
2789 mpd.inode = mapping->host;
2790
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002791 /*
2792 * we don't want write_cache_pages to update
2793 * nr_to_write and writeback_index
2794 */
2795 no_nrwrite_index_update = wbc->no_nrwrite_index_update;
2796 wbc->no_nrwrite_index_update = 1;
2797 pages_skipped = wbc->pages_skipped;
2798
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002799retry:
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002800 while (!ret && wbc->nr_to_write > 0) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002801
2802 /*
2803 * we insert one extent at a time. So we need
2804 * credit needed for single extent allocation.
2805 * journalled mode is currently not supported
2806 * by delalloc
2807 */
2808 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002809 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002810
Mingming Cao61628a32008-07-11 19:27:31 -04002811 /* start a new transaction*/
2812 handle = ext4_journal_start(inode, needed_blocks);
2813 if (IS_ERR(handle)) {
2814 ret = PTR_ERR(handle);
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002815 printk(KERN_CRIT "%s: jbd2_start: "
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002816 "%ld pages, ino %lu; err %d\n", __func__,
2817 wbc->nr_to_write, inode->i_ino, ret);
2818 dump_stack();
Mingming Cao61628a32008-07-11 19:27:31 -04002819 goto out_writepages;
2820 }
Theodore Ts'of63e6002009-02-23 16:42:39 -05002821
2822 /*
2823 * Now call __mpage_da_writepage to find the next
2824 * contiguous region of logical blocks that need
2825 * blocks to be allocated by ext4. We don't actually
2826 * submit the blocks for I/O here, even though
2827 * write_cache_pages thinks it will, and will set the
2828 * pages as clean for write before calling
2829 * __mpage_da_writepage().
2830 */
2831 mpd.b_size = 0;
2832 mpd.b_state = 0;
2833 mpd.b_blocknr = 0;
2834 mpd.first_page = 0;
2835 mpd.next_page = 0;
2836 mpd.io_done = 0;
2837 mpd.pages_written = 0;
2838 mpd.retval = 0;
2839 ret = write_cache_pages(mapping, wbc, __mpage_da_writepage,
2840 &mpd);
2841 /*
2842 * If we have a contigous extent of pages and we
2843 * haven't done the I/O yet, map the blocks and submit
2844 * them for I/O.
2845 */
2846 if (!mpd.io_done && mpd.next_page != mpd.first_page) {
2847 if (mpage_da_map_blocks(&mpd) == 0)
2848 mpage_da_submit_io(&mpd);
2849 mpd.io_done = 1;
2850 ret = MPAGE_DA_EXTENT_TAIL;
2851 }
2852 wbc->nr_to_write -= mpd.pages_written;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002853
Mingming Cao61628a32008-07-11 19:27:31 -04002854 ext4_journal_stop(handle);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002855
Eric Sandeen8f64b322009-02-26 00:57:35 -05002856 if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002857 /* commit the transaction which would
2858 * free blocks released in the transaction
2859 * and try again
2860 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002861 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002862 wbc->pages_skipped = pages_skipped;
2863 ret = 0;
2864 } else if (ret == MPAGE_DA_EXTENT_TAIL) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002865 /*
2866 * got one extent now try with
2867 * rest of the pages
2868 */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002869 pages_written += mpd.pages_written;
2870 wbc->pages_skipped = pages_skipped;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002871 ret = 0;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002872 io_done = 1;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002873 } else if (wbc->nr_to_write)
Mingming Cao61628a32008-07-11 19:27:31 -04002874 /*
2875 * There is no more writeout needed
2876 * or we requested for a noblocking writeout
2877 * and we found the device congested
2878 */
Mingming Cao61628a32008-07-11 19:27:31 -04002879 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002880 }
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002881 if (!io_done && !cycled) {
2882 cycled = 1;
2883 index = 0;
2884 wbc->range_start = index << PAGE_CACHE_SHIFT;
2885 wbc->range_end = mapping->writeback_index - 1;
2886 goto retry;
2887 }
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002888 if (pages_skipped != wbc->pages_skipped)
2889 printk(KERN_EMERG "This should not happen leaving %s "
2890 "with nr_to_write = %ld ret = %d\n",
2891 __func__, wbc->nr_to_write, ret);
Mingming Cao61628a32008-07-11 19:27:31 -04002892
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002893 /* Update index */
2894 index += pages_written;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002895 wbc->range_cyclic = range_cyclic;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002896 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2897 /*
2898 * set the writeback_index so that range_cyclic
2899 * mode will write it back later
2900 */
2901 mapping->writeback_index = index;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002902
Mingming Cao61628a32008-07-11 19:27:31 -04002903out_writepages:
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002904 if (!no_nrwrite_index_update)
2905 wbc->no_nrwrite_index_update = 0;
2906 wbc->nr_to_write -= nr_to_writebump;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002907 trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
Mingming Cao61628a32008-07-11 19:27:31 -04002908 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002909}
2910
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002911#define FALL_BACK_TO_NONDELALLOC 1
2912static int ext4_nonda_switch(struct super_block *sb)
2913{
2914 s64 free_blocks, dirty_blocks;
2915 struct ext4_sb_info *sbi = EXT4_SB(sb);
2916
2917 /*
2918 * switch to non delalloc mode if we are running low
2919 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08002920 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002921 * accumulated on each CPU without updating global counters
2922 * Delalloc need an accurate free block accounting. So switch
2923 * to non delalloc when we are near to error range.
2924 */
2925 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
2926 dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter);
2927 if (2 * free_blocks < 3 * dirty_blocks ||
2928 free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) {
2929 /*
2930 * free block count is less that 150% of dirty blocks
2931 * or free blocks is less that watermark
2932 */
2933 return 1;
2934 }
2935 return 0;
2936}
2937
Alex Tomas64769242008-07-11 19:27:31 -04002938static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002939 loff_t pos, unsigned len, unsigned flags,
2940 struct page **pagep, void **fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002941{
Mingming Caod2a17632008-07-14 17:52:37 -04002942 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002943 struct page *page;
2944 pgoff_t index;
2945 unsigned from, to;
2946 struct inode *inode = mapping->host;
2947 handle_t *handle;
2948
2949 index = pos >> PAGE_CACHE_SHIFT;
2950 from = pos & (PAGE_CACHE_SIZE - 1);
2951 to = from + len;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002952
2953 if (ext4_nonda_switch(inode->i_sb)) {
2954 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2955 return ext4_write_begin(file, mapping, pos,
2956 len, flags, pagep, fsdata);
2957 }
2958 *fsdata = (void *)0;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002959 trace_ext4_da_write_begin(inode, pos, len, flags);
Mingming Caod2a17632008-07-14 17:52:37 -04002960retry:
Alex Tomas64769242008-07-11 19:27:31 -04002961 /*
2962 * With delayed allocation, we don't log the i_disksize update
2963 * if there is delayed block allocation. But we still need
2964 * to journalling the i_disksize update if writes to the end
2965 * of file which has an already mapped buffer.
2966 */
2967 handle = ext4_journal_start(inode, 1);
2968 if (IS_ERR(handle)) {
2969 ret = PTR_ERR(handle);
2970 goto out;
2971 }
Jan Karaebd36102009-02-22 21:09:59 -05002972 /* We cannot recurse into the filesystem as the transaction is already
2973 * started */
2974 flags |= AOP_FLAG_NOFS;
Alex Tomas64769242008-07-11 19:27:31 -04002975
Nick Piggin54566b22009-01-04 12:00:53 -08002976 page = grab_cache_page_write_begin(mapping, index, flags);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002977 if (!page) {
2978 ext4_journal_stop(handle);
2979 ret = -ENOMEM;
2980 goto out;
2981 }
Alex Tomas64769242008-07-11 19:27:31 -04002982 *pagep = page;
2983
2984 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
Theodore Ts'ob920c752009-05-14 00:54:29 -04002985 ext4_da_get_block_prep);
Alex Tomas64769242008-07-11 19:27:31 -04002986 if (ret < 0) {
2987 unlock_page(page);
2988 ext4_journal_stop(handle);
2989 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04002990 /*
2991 * block_write_begin may have instantiated a few blocks
2992 * outside i_size. Trim these off again. Don't need
2993 * i_size_read because we hold i_mutex.
2994 */
2995 if (pos + len > inode->i_size)
Jan Karaffacfa72009-07-13 16:22:22 -04002996 ext4_truncate(inode);
Alex Tomas64769242008-07-11 19:27:31 -04002997 }
2998
Mingming Caod2a17632008-07-14 17:52:37 -04002999 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3000 goto retry;
Alex Tomas64769242008-07-11 19:27:31 -04003001out:
3002 return ret;
3003}
3004
Mingming Cao632eaea2008-07-11 19:27:31 -04003005/*
3006 * Check if we should update i_disksize
3007 * when write to the end of file but not require block allocation
3008 */
3009static int ext4_da_should_update_i_disksize(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003010 unsigned long offset)
Mingming Cao632eaea2008-07-11 19:27:31 -04003011{
3012 struct buffer_head *bh;
3013 struct inode *inode = page->mapping->host;
3014 unsigned int idx;
3015 int i;
3016
3017 bh = page_buffers(page);
3018 idx = offset >> inode->i_blkbits;
3019
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003020 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04003021 bh = bh->b_this_page;
3022
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04003023 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
Mingming Cao632eaea2008-07-11 19:27:31 -04003024 return 0;
3025 return 1;
3026}
3027
Alex Tomas64769242008-07-11 19:27:31 -04003028static int ext4_da_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003029 struct address_space *mapping,
3030 loff_t pos, unsigned len, unsigned copied,
3031 struct page *page, void *fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04003032{
3033 struct inode *inode = mapping->host;
3034 int ret = 0, ret2;
3035 handle_t *handle = ext4_journal_current_handle();
3036 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04003037 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003038 int write_mode = (int)(unsigned long)fsdata;
3039
3040 if (write_mode == FALL_BACK_TO_NONDELALLOC) {
3041 if (ext4_should_order_data(inode)) {
3042 return ext4_ordered_write_end(file, mapping, pos,
3043 len, copied, page, fsdata);
3044 } else if (ext4_should_writeback_data(inode)) {
3045 return ext4_writeback_write_end(file, mapping, pos,
3046 len, copied, page, fsdata);
3047 } else {
3048 BUG();
3049 }
3050 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003051
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003052 trace_ext4_da_write_end(inode, pos, len, copied);
Mingming Cao632eaea2008-07-11 19:27:31 -04003053 start = pos & (PAGE_CACHE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003054 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04003055
3056 /*
3057 * generic_write_end() will run mark_inode_dirty() if i_size
3058 * changes. So let's piggyback the i_disksize mark_inode_dirty
3059 * into that.
3060 */
3061
3062 new_i_size = pos + copied;
Mingming Cao632eaea2008-07-11 19:27:31 -04003063 if (new_i_size > EXT4_I(inode)->i_disksize) {
3064 if (ext4_da_should_update_i_disksize(page, end)) {
3065 down_write(&EXT4_I(inode)->i_data_sem);
3066 if (new_i_size > EXT4_I(inode)->i_disksize) {
3067 /*
3068 * Updating i_disksize when extending file
3069 * without needing block allocation
3070 */
3071 if (ext4_should_order_data(inode))
3072 ret = ext4_jbd2_file_inode(handle,
3073 inode);
Alex Tomas64769242008-07-11 19:27:31 -04003074
Mingming Cao632eaea2008-07-11 19:27:31 -04003075 EXT4_I(inode)->i_disksize = new_i_size;
3076 }
3077 up_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003078 /* We need to mark inode dirty even if
3079 * new_i_size is less that inode->i_size
3080 * bu greater than i_disksize.(hint delalloc)
3081 */
3082 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04003083 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003084 }
Alex Tomas64769242008-07-11 19:27:31 -04003085 ret2 = generic_write_end(file, mapping, pos, len, copied,
3086 page, fsdata);
3087 copied = ret2;
3088 if (ret2 < 0)
3089 ret = ret2;
3090 ret2 = ext4_journal_stop(handle);
3091 if (!ret)
3092 ret = ret2;
3093
3094 return ret ? ret : copied;
3095}
3096
3097static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
3098{
Alex Tomas64769242008-07-11 19:27:31 -04003099 /*
3100 * Drop reserved blocks
3101 */
3102 BUG_ON(!PageLocked(page));
3103 if (!page_has_buffers(page))
3104 goto out;
3105
Mingming Caod2a17632008-07-14 17:52:37 -04003106 ext4_da_page_release_reservation(page, offset);
Alex Tomas64769242008-07-11 19:27:31 -04003107
3108out:
3109 ext4_invalidatepage(page, offset);
3110
3111 return;
3112}
3113
Theodore Ts'occd25062009-02-26 01:04:07 -05003114/*
3115 * Force all delayed allocation blocks to be allocated for a given inode.
3116 */
3117int ext4_alloc_da_blocks(struct inode *inode)
3118{
3119 if (!EXT4_I(inode)->i_reserved_data_blocks &&
3120 !EXT4_I(inode)->i_reserved_meta_blocks)
3121 return 0;
3122
3123 /*
3124 * We do something simple for now. The filemap_flush() will
3125 * also start triggering a write of the data blocks, which is
3126 * not strictly speaking necessary (and for users of
3127 * laptop_mode, not even desirable). However, to do otherwise
3128 * would require replicating code paths in:
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003129 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003130 * ext4_da_writepages() ->
3131 * write_cache_pages() ---> (via passed in callback function)
3132 * __mpage_da_writepage() -->
3133 * mpage_add_bh_to_extent()
3134 * mpage_da_map_blocks()
3135 *
3136 * The problem is that write_cache_pages(), located in
3137 * mm/page-writeback.c, marks pages clean in preparation for
3138 * doing I/O, which is not desirable if we're not planning on
3139 * doing I/O at all.
3140 *
3141 * We could call write_cache_pages(), and then redirty all of
3142 * the pages by calling redirty_page_for_writeback() but that
3143 * would be ugly in the extreme. So instead we would need to
3144 * replicate parts of the code in the above functions,
3145 * simplifying them becuase we wouldn't actually intend to
3146 * write out the pages, but rather only collect contiguous
3147 * logical block extents, call the multi-block allocator, and
3148 * then update the buffer heads with the block allocations.
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003149 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003150 * For now, though, we'll cheat by calling filemap_flush(),
3151 * which will map the blocks, and start the I/O, but not
3152 * actually wait for the I/O to complete.
3153 */
3154 return filemap_flush(inode->i_mapping);
3155}
Alex Tomas64769242008-07-11 19:27:31 -04003156
3157/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003158 * bmap() is special. It gets used by applications such as lilo and by
3159 * the swapper to find the on-disk block of a specific piece of data.
3160 *
3161 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07003162 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003163 * filesystem and enables swap, then they may get a nasty shock when the
3164 * data getting swapped to that swapfile suddenly gets overwritten by
3165 * the original zero's written out previously to the journal and
3166 * awaiting writeback in the kernel's buffer cache.
3167 *
3168 * So, if we see any bmap calls here on a modified, data-journaled file,
3169 * take extra steps to flush any blocks which might be in the cache.
3170 */
Mingming Cao617ba132006-10-11 01:20:53 -07003171static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003172{
3173 struct inode *inode = mapping->host;
3174 journal_t *journal;
3175 int err;
3176
Alex Tomas64769242008-07-11 19:27:31 -04003177 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
3178 test_opt(inode->i_sb, DELALLOC)) {
3179 /*
3180 * With delalloc we want to sync the file
3181 * so that we can make sure we allocate
3182 * blocks for file
3183 */
3184 filemap_write_and_wait(mapping);
3185 }
3186
Frank Mayhar03901312009-01-07 00:06:22 -05003187 if (EXT4_JOURNAL(inode) && EXT4_I(inode)->i_state & EXT4_STATE_JDATA) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003188 /*
3189 * This is a REALLY heavyweight approach, but the use of
3190 * bmap on dirty files is expected to be extremely rare:
3191 * only if we run lilo or swapon on a freshly made file
3192 * do we expect this to happen.
3193 *
3194 * (bmap requires CAP_SYS_RAWIO so this does not
3195 * represent an unprivileged user DOS attack --- we'd be
3196 * in trouble if mortal users could trigger this path at
3197 * will.)
3198 *
Mingming Cao617ba132006-10-11 01:20:53 -07003199 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003200 * regular files. If somebody wants to bmap a directory
3201 * or symlink and gets confused because the buffer
3202 * hasn't yet been flushed to disk, they deserve
3203 * everything they get.
3204 */
3205
Mingming Cao617ba132006-10-11 01:20:53 -07003206 EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA;
3207 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07003208 jbd2_journal_lock_updates(journal);
3209 err = jbd2_journal_flush(journal);
3210 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003211
3212 if (err)
3213 return 0;
3214 }
3215
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003216 return generic_block_bmap(mapping, block, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003217}
3218
Mingming Cao617ba132006-10-11 01:20:53 -07003219static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003220{
Mingming Cao617ba132006-10-11 01:20:53 -07003221 return mpage_readpage(page, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003222}
3223
3224static int
Mingming Cao617ba132006-10-11 01:20:53 -07003225ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003226 struct list_head *pages, unsigned nr_pages)
3227{
Mingming Cao617ba132006-10-11 01:20:53 -07003228 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003229}
3230
Mingming Cao617ba132006-10-11 01:20:53 -07003231static void ext4_invalidatepage(struct page *page, unsigned long offset)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003232{
Mingming Cao617ba132006-10-11 01:20:53 -07003233 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003234
3235 /*
3236 * If it's a full truncate we just forget about the pending dirtying
3237 */
3238 if (offset == 0)
3239 ClearPageChecked(page);
3240
Frank Mayhar03901312009-01-07 00:06:22 -05003241 if (journal)
3242 jbd2_journal_invalidatepage(journal, page, offset);
3243 else
3244 block_invalidatepage(page, offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003245}
3246
Mingming Cao617ba132006-10-11 01:20:53 -07003247static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003248{
Mingming Cao617ba132006-10-11 01:20:53 -07003249 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003250
3251 WARN_ON(PageChecked(page));
3252 if (!page_has_buffers(page))
3253 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003254 if (journal)
3255 return jbd2_journal_try_to_free_buffers(journal, page, wait);
3256 else
3257 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003258}
3259
3260/*
3261 * If the O_DIRECT write will extend the file then add this inode to the
3262 * orphan list. So recovery will truncate it back to the original size
3263 * if the machine crashes during the write.
3264 *
3265 * If the O_DIRECT write is intantiating holes inside i_size and the machine
Jan Kara7fb54092008-02-10 01:08:38 -05003266 * crashes then stale disk data _may_ be exposed inside the file. But current
3267 * VFS code falls back into buffered path in that case so we are safe.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003268 */
Mingming Cao617ba132006-10-11 01:20:53 -07003269static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003270 const struct iovec *iov, loff_t offset,
3271 unsigned long nr_segs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003272{
3273 struct file *file = iocb->ki_filp;
3274 struct inode *inode = file->f_mapping->host;
Mingming Cao617ba132006-10-11 01:20:53 -07003275 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003276 handle_t *handle;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003277 ssize_t ret;
3278 int orphan = 0;
3279 size_t count = iov_length(iov, nr_segs);
3280
3281 if (rw == WRITE) {
3282 loff_t final_size = offset + count;
3283
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003284 if (final_size > inode->i_size) {
Jan Kara7fb54092008-02-10 01:08:38 -05003285 /* Credits for sb + inode write */
3286 handle = ext4_journal_start(inode, 2);
3287 if (IS_ERR(handle)) {
3288 ret = PTR_ERR(handle);
3289 goto out;
3290 }
Mingming Cao617ba132006-10-11 01:20:53 -07003291 ret = ext4_orphan_add(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003292 if (ret) {
3293 ext4_journal_stop(handle);
3294 goto out;
3295 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003296 orphan = 1;
3297 ei->i_disksize = inode->i_size;
Jan Kara7fb54092008-02-10 01:08:38 -05003298 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003299 }
3300 }
3301
3302 ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
3303 offset, nr_segs,
Mingming Cao617ba132006-10-11 01:20:53 -07003304 ext4_get_block, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003305
Jan Kara7fb54092008-02-10 01:08:38 -05003306 if (orphan) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003307 int err;
3308
Jan Kara7fb54092008-02-10 01:08:38 -05003309 /* Credits for sb + inode write */
3310 handle = ext4_journal_start(inode, 2);
3311 if (IS_ERR(handle)) {
3312 /* This is really bad luck. We've written the data
3313 * but cannot extend i_size. Bail out and pretend
3314 * the write failed... */
3315 ret = PTR_ERR(handle);
3316 goto out;
3317 }
3318 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003319 ext4_orphan_del(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003320 if (ret > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003321 loff_t end = offset + ret;
3322 if (end > inode->i_size) {
3323 ei->i_disksize = end;
3324 i_size_write(inode, end);
3325 /*
3326 * We're going to return a positive `ret'
3327 * here due to non-zero-length I/O, so there's
3328 * no way of reporting error returns from
Mingming Cao617ba132006-10-11 01:20:53 -07003329 * ext4_mark_inode_dirty() to userspace. So
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003330 * ignore it.
3331 */
Mingming Cao617ba132006-10-11 01:20:53 -07003332 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003333 }
3334 }
Mingming Cao617ba132006-10-11 01:20:53 -07003335 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003336 if (ret == 0)
3337 ret = err;
3338 }
3339out:
3340 return ret;
3341}
3342
3343/*
Mingming Cao617ba132006-10-11 01:20:53 -07003344 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003345 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3346 * much here because ->set_page_dirty is called under VFS locks. The page is
3347 * not necessarily locked.
3348 *
3349 * We cannot just dirty the page and leave attached buffers clean, because the
3350 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3351 * or jbddirty because all the journalling code will explode.
3352 *
3353 * So what we do is to mark the page "pending dirty" and next time writepage
3354 * is called, propagate that into the buffers appropriately.
3355 */
Mingming Cao617ba132006-10-11 01:20:53 -07003356static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003357{
3358 SetPageChecked(page);
3359 return __set_page_dirty_nobuffers(page);
3360}
3361
Mingming Cao617ba132006-10-11 01:20:53 -07003362static const struct address_space_operations ext4_ordered_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003363 .readpage = ext4_readpage,
3364 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003365 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003366 .sync_page = block_sync_page,
3367 .write_begin = ext4_write_begin,
3368 .write_end = ext4_ordered_write_end,
3369 .bmap = ext4_bmap,
3370 .invalidatepage = ext4_invalidatepage,
3371 .releasepage = ext4_releasepage,
3372 .direct_IO = ext4_direct_IO,
3373 .migratepage = buffer_migrate_page,
3374 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003375};
3376
Mingming Cao617ba132006-10-11 01:20:53 -07003377static const struct address_space_operations ext4_writeback_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003378 .readpage = ext4_readpage,
3379 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003380 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003381 .sync_page = block_sync_page,
3382 .write_begin = ext4_write_begin,
3383 .write_end = ext4_writeback_write_end,
3384 .bmap = ext4_bmap,
3385 .invalidatepage = ext4_invalidatepage,
3386 .releasepage = ext4_releasepage,
3387 .direct_IO = ext4_direct_IO,
3388 .migratepage = buffer_migrate_page,
3389 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003390};
3391
Mingming Cao617ba132006-10-11 01:20:53 -07003392static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003393 .readpage = ext4_readpage,
3394 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003395 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003396 .sync_page = block_sync_page,
3397 .write_begin = ext4_write_begin,
3398 .write_end = ext4_journalled_write_end,
3399 .set_page_dirty = ext4_journalled_set_page_dirty,
3400 .bmap = ext4_bmap,
3401 .invalidatepage = ext4_invalidatepage,
3402 .releasepage = ext4_releasepage,
3403 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003404};
3405
Alex Tomas64769242008-07-11 19:27:31 -04003406static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003407 .readpage = ext4_readpage,
3408 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003409 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003410 .writepages = ext4_da_writepages,
3411 .sync_page = block_sync_page,
3412 .write_begin = ext4_da_write_begin,
3413 .write_end = ext4_da_write_end,
3414 .bmap = ext4_bmap,
3415 .invalidatepage = ext4_da_invalidatepage,
3416 .releasepage = ext4_releasepage,
3417 .direct_IO = ext4_direct_IO,
3418 .migratepage = buffer_migrate_page,
3419 .is_partially_uptodate = block_is_partially_uptodate,
Alex Tomas64769242008-07-11 19:27:31 -04003420};
3421
Mingming Cao617ba132006-10-11 01:20:53 -07003422void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003423{
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04003424 if (ext4_should_order_data(inode) &&
3425 test_opt(inode->i_sb, DELALLOC))
3426 inode->i_mapping->a_ops = &ext4_da_aops;
3427 else if (ext4_should_order_data(inode))
Mingming Cao617ba132006-10-11 01:20:53 -07003428 inode->i_mapping->a_ops = &ext4_ordered_aops;
Alex Tomas64769242008-07-11 19:27:31 -04003429 else if (ext4_should_writeback_data(inode) &&
3430 test_opt(inode->i_sb, DELALLOC))
3431 inode->i_mapping->a_ops = &ext4_da_aops;
Mingming Cao617ba132006-10-11 01:20:53 -07003432 else if (ext4_should_writeback_data(inode))
3433 inode->i_mapping->a_ops = &ext4_writeback_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003434 else
Mingming Cao617ba132006-10-11 01:20:53 -07003435 inode->i_mapping->a_ops = &ext4_journalled_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003436}
3437
3438/*
Mingming Cao617ba132006-10-11 01:20:53 -07003439 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003440 * up to the end of the block which corresponds to `from'.
3441 * This required during truncate. We need to physically zero the tail end
3442 * of that block so it doesn't yield old data if the file is later grown.
3443 */
Jan Karacf108bc2008-07-11 19:27:31 -04003444int ext4_block_truncate_page(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003445 struct address_space *mapping, loff_t from)
3446{
Mingming Cao617ba132006-10-11 01:20:53 -07003447 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003448 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003449 unsigned blocksize, length, pos;
3450 ext4_lblk_t iblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003451 struct inode *inode = mapping->host;
3452 struct buffer_head *bh;
Jan Karacf108bc2008-07-11 19:27:31 -04003453 struct page *page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003454 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003455
Theodore Ts'of4a01012009-07-05 22:08:16 -04003456 page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
3457 mapping_gfp_mask(mapping) & ~__GFP_FS);
Jan Karacf108bc2008-07-11 19:27:31 -04003458 if (!page)
3459 return -EINVAL;
3460
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003461 blocksize = inode->i_sb->s_blocksize;
3462 length = blocksize - (offset & (blocksize - 1));
3463 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
3464
3465 /*
3466 * For "nobh" option, we can only work if we don't need to
3467 * read-in the page - otherwise we create buffers to do the IO.
3468 */
3469 if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) &&
Mingming Cao617ba132006-10-11 01:20:53 -07003470 ext4_should_writeback_data(inode) && PageUptodate(page)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003471 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003472 set_page_dirty(page);
3473 goto unlock;
3474 }
3475
3476 if (!page_has_buffers(page))
3477 create_empty_buffers(page, blocksize, 0);
3478
3479 /* Find the buffer that contains "offset" */
3480 bh = page_buffers(page);
3481 pos = blocksize;
3482 while (offset >= pos) {
3483 bh = bh->b_this_page;
3484 iblock++;
3485 pos += blocksize;
3486 }
3487
3488 err = 0;
3489 if (buffer_freed(bh)) {
3490 BUFFER_TRACE(bh, "freed: skip");
3491 goto unlock;
3492 }
3493
3494 if (!buffer_mapped(bh)) {
3495 BUFFER_TRACE(bh, "unmapped");
Mingming Cao617ba132006-10-11 01:20:53 -07003496 ext4_get_block(inode, iblock, bh, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003497 /* unmapped? It's a hole - nothing to do */
3498 if (!buffer_mapped(bh)) {
3499 BUFFER_TRACE(bh, "still unmapped");
3500 goto unlock;
3501 }
3502 }
3503
3504 /* Ok, it's mapped. Make sure it's up-to-date */
3505 if (PageUptodate(page))
3506 set_buffer_uptodate(bh);
3507
3508 if (!buffer_uptodate(bh)) {
3509 err = -EIO;
3510 ll_rw_block(READ, 1, &bh);
3511 wait_on_buffer(bh);
3512 /* Uhhuh. Read error. Complain and punt. */
3513 if (!buffer_uptodate(bh))
3514 goto unlock;
3515 }
3516
Mingming Cao617ba132006-10-11 01:20:53 -07003517 if (ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003518 BUFFER_TRACE(bh, "get write access");
Mingming Cao617ba132006-10-11 01:20:53 -07003519 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003520 if (err)
3521 goto unlock;
3522 }
3523
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003524 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003525
3526 BUFFER_TRACE(bh, "zeroed end of block");
3527
3528 err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003529 if (ext4_should_journal_data(inode)) {
Frank Mayhar03901312009-01-07 00:06:22 -05003530 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003531 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003532 if (ext4_should_order_data(inode))
Jan Kara678aaf42008-07-11 19:27:31 -04003533 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003534 mark_buffer_dirty(bh);
3535 }
3536
3537unlock:
3538 unlock_page(page);
3539 page_cache_release(page);
3540 return err;
3541}
3542
3543/*
3544 * Probably it should be a library function... search for first non-zero word
3545 * or memcmp with zero_page, whatever is better for particular architecture.
3546 * Linus?
3547 */
3548static inline int all_zeroes(__le32 *p, __le32 *q)
3549{
3550 while (p < q)
3551 if (*p++)
3552 return 0;
3553 return 1;
3554}
3555
3556/**
Mingming Cao617ba132006-10-11 01:20:53 -07003557 * ext4_find_shared - find the indirect blocks for partial truncation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003558 * @inode: inode in question
3559 * @depth: depth of the affected branch
Mingming Cao617ba132006-10-11 01:20:53 -07003560 * @offsets: offsets of pointers in that branch (see ext4_block_to_path)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003561 * @chain: place to store the pointers to partial indirect blocks
3562 * @top: place to the (detached) top of branch
3563 *
Mingming Cao617ba132006-10-11 01:20:53 -07003564 * This is a helper function used by ext4_truncate().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003565 *
3566 * When we do truncate() we may have to clean the ends of several
3567 * indirect blocks but leave the blocks themselves alive. Block is
3568 * partially truncated if some data below the new i_size is refered
3569 * from it (and it is on the path to the first completely truncated
3570 * data block, indeed). We have to free the top of that path along
3571 * with everything to the right of the path. Since no allocation
Mingming Cao617ba132006-10-11 01:20:53 -07003572 * past the truncation point is possible until ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003573 * finishes, we may safely do the latter, but top of branch may
3574 * require special attention - pageout below the truncation point
3575 * might try to populate it.
3576 *
3577 * We atomically detach the top of branch from the tree, store the
3578 * block number of its root in *@top, pointers to buffer_heads of
3579 * partially truncated blocks - in @chain[].bh and pointers to
3580 * their last elements that should not be removed - in
3581 * @chain[].p. Return value is the pointer to last filled element
3582 * of @chain.
3583 *
3584 * The work left to caller to do the actual freeing of subtrees:
3585 * a) free the subtree starting from *@top
3586 * b) free the subtrees whose roots are stored in
3587 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
3588 * c) free the subtrees growing from the inode past the @chain[0].
3589 * (no partially truncated stuff there). */
3590
Mingming Cao617ba132006-10-11 01:20:53 -07003591static Indirect *ext4_find_shared(struct inode *inode, int depth,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003592 ext4_lblk_t offsets[4], Indirect chain[4],
3593 __le32 *top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003594{
3595 Indirect *partial, *p;
3596 int k, err;
3597
3598 *top = 0;
3599 /* Make k index the deepest non-null offest + 1 */
3600 for (k = depth; k > 1 && !offsets[k-1]; k--)
3601 ;
Mingming Cao617ba132006-10-11 01:20:53 -07003602 partial = ext4_get_branch(inode, k, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003603 /* Writer: pointers */
3604 if (!partial)
3605 partial = chain + k-1;
3606 /*
3607 * If the branch acquired continuation since we've looked at it -
3608 * fine, it should all survive and (new) top doesn't belong to us.
3609 */
3610 if (!partial->key && *partial->p)
3611 /* Writer: end */
3612 goto no_top;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003613 for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003614 ;
3615 /*
3616 * OK, we've found the last block that must survive. The rest of our
3617 * branch should be detached before unlocking. However, if that rest
3618 * of branch is all ours and does not grow immediately from the inode
3619 * it's easier to cheat and just decrement partial->p.
3620 */
3621 if (p == chain + k - 1 && p > chain) {
3622 p->p--;
3623 } else {
3624 *top = *p->p;
Mingming Cao617ba132006-10-11 01:20:53 -07003625 /* Nope, don't do this in ext4. Must leave the tree intact */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003626#if 0
3627 *p->p = 0;
3628#endif
3629 }
3630 /* Writer: end */
3631
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003632 while (partial > p) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003633 brelse(partial->bh);
3634 partial--;
3635 }
3636no_top:
3637 return partial;
3638}
3639
3640/*
3641 * Zero a number of block pointers in either an inode or an indirect block.
3642 * If we restart the transaction we must again get write access to the
3643 * indirect block for further modification.
3644 *
3645 * We release `count' blocks on disk, but (last - first) may be greater
3646 * than `count' because there can be holes in there.
3647 */
Mingming Cao617ba132006-10-11 01:20:53 -07003648static void ext4_clear_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003649 struct buffer_head *bh,
3650 ext4_fsblk_t block_to_free,
3651 unsigned long count, __le32 *first,
3652 __le32 *last)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003653{
3654 __le32 *p;
3655 if (try_to_extend_transaction(handle, inode)) {
3656 if (bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05003657 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
3658 ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003659 }
Mingming Cao617ba132006-10-11 01:20:53 -07003660 ext4_mark_inode_dirty(handle, inode);
3661 ext4_journal_test_restart(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003662 if (bh) {
3663 BUFFER_TRACE(bh, "retaking write access");
Mingming Cao617ba132006-10-11 01:20:53 -07003664 ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003665 }
3666 }
3667
3668 /*
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003669 * Any buffers which are on the journal will be in memory. We
3670 * find them on the hash table so jbd2_journal_revoke() will
3671 * run jbd2_journal_forget() on them. We've already detached
3672 * each block from the file, so bforget() in
3673 * jbd2_journal_forget() should be safe.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003674 *
Mingming Caodab291a2006-10-11 01:21:01 -07003675 * AKPM: turn on bforget in jbd2_journal_forget()!!!
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003676 */
3677 for (p = first; p < last; p++) {
3678 u32 nr = le32_to_cpu(*p);
3679 if (nr) {
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003680 struct buffer_head *tbh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003681
3682 *p = 0;
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003683 tbh = sb_find_get_block(inode->i_sb, nr);
3684 ext4_forget(handle, 0, inode, tbh, nr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003685 }
3686 }
3687
Alex Tomasc9de5602008-01-29 00:19:52 -05003688 ext4_free_blocks(handle, inode, block_to_free, count, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003689}
3690
3691/**
Mingming Cao617ba132006-10-11 01:20:53 -07003692 * ext4_free_data - free a list of data blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003693 * @handle: handle for this transaction
3694 * @inode: inode we are dealing with
3695 * @this_bh: indirect buffer_head which contains *@first and *@last
3696 * @first: array of block numbers
3697 * @last: points immediately past the end of array
3698 *
3699 * We are freeing all blocks refered from that array (numbers are stored as
3700 * little-endian 32-bit) and updating @inode->i_blocks appropriately.
3701 *
3702 * We accumulate contiguous runs of blocks to free. Conveniently, if these
3703 * blocks are contiguous then releasing them at one time will only affect one
3704 * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
3705 * actually use a lot of journal space.
3706 *
3707 * @this_bh will be %NULL if @first and @last point into the inode's direct
3708 * block pointers.
3709 */
Mingming Cao617ba132006-10-11 01:20:53 -07003710static void ext4_free_data(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003711 struct buffer_head *this_bh,
3712 __le32 *first, __le32 *last)
3713{
Mingming Cao617ba132006-10-11 01:20:53 -07003714 ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003715 unsigned long count = 0; /* Number of blocks in the run */
3716 __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
3717 corresponding to
3718 block_to_free */
Mingming Cao617ba132006-10-11 01:20:53 -07003719 ext4_fsblk_t nr; /* Current block # */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003720 __le32 *p; /* Pointer into inode/ind
3721 for current block */
3722 int err;
3723
3724 if (this_bh) { /* For indirect block */
3725 BUFFER_TRACE(this_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003726 err = ext4_journal_get_write_access(handle, this_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003727 /* Important: if we can't update the indirect pointers
3728 * to the blocks, we can't free them. */
3729 if (err)
3730 return;
3731 }
3732
3733 for (p = first; p < last; p++) {
3734 nr = le32_to_cpu(*p);
3735 if (nr) {
3736 /* accumulate blocks to free if they're contiguous */
3737 if (count == 0) {
3738 block_to_free = nr;
3739 block_to_free_p = p;
3740 count = 1;
3741 } else if (nr == block_to_free + count) {
3742 count++;
3743 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003744 ext4_clear_blocks(handle, inode, this_bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003745 block_to_free,
3746 count, block_to_free_p, p);
3747 block_to_free = nr;
3748 block_to_free_p = p;
3749 count = 1;
3750 }
3751 }
3752 }
3753
3754 if (count > 0)
Mingming Cao617ba132006-10-11 01:20:53 -07003755 ext4_clear_blocks(handle, inode, this_bh, block_to_free,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003756 count, block_to_free_p, p);
3757
3758 if (this_bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05003759 BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata");
Duane Griffin71dc8fb2008-07-11 19:27:31 -04003760
3761 /*
3762 * The buffer head should have an attached journal head at this
3763 * point. However, if the data is corrupted and an indirect
3764 * block pointed to itself, it would have been detached when
3765 * the block was cleared. Check for this instead of OOPSing.
3766 */
Theodore Ts'oe7f07962009-01-20 09:50:19 -05003767 if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
Frank Mayhar03901312009-01-07 00:06:22 -05003768 ext4_handle_dirty_metadata(handle, inode, this_bh);
Duane Griffin71dc8fb2008-07-11 19:27:31 -04003769 else
3770 ext4_error(inode->i_sb, __func__,
3771 "circular indirect block detected, "
3772 "inode=%lu, block=%llu",
3773 inode->i_ino,
3774 (unsigned long long) this_bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003775 }
3776}
3777
3778/**
Mingming Cao617ba132006-10-11 01:20:53 -07003779 * ext4_free_branches - free an array of branches
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003780 * @handle: JBD handle for this transaction
3781 * @inode: inode we are dealing with
3782 * @parent_bh: the buffer_head which contains *@first and *@last
3783 * @first: array of block numbers
3784 * @last: pointer immediately past the end of array
3785 * @depth: depth of the branches to free
3786 *
3787 * We are freeing all blocks refered from these branches (numbers are
3788 * stored as little-endian 32-bit) and updating @inode->i_blocks
3789 * appropriately.
3790 */
Mingming Cao617ba132006-10-11 01:20:53 -07003791static void ext4_free_branches(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003792 struct buffer_head *parent_bh,
3793 __le32 *first, __le32 *last, int depth)
3794{
Mingming Cao617ba132006-10-11 01:20:53 -07003795 ext4_fsblk_t nr;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003796 __le32 *p;
3797
Frank Mayhar03901312009-01-07 00:06:22 -05003798 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003799 return;
3800
3801 if (depth--) {
3802 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07003803 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003804 p = last;
3805 while (--p >= first) {
3806 nr = le32_to_cpu(*p);
3807 if (!nr)
3808 continue; /* A hole */
3809
3810 /* Go read the buffer for the next level down */
3811 bh = sb_bread(inode->i_sb, nr);
3812
3813 /*
3814 * A read failure? Report error and clear slot
3815 * (should be rare).
3816 */
3817 if (!bh) {
Mingming Cao617ba132006-10-11 01:20:53 -07003818 ext4_error(inode->i_sb, "ext4_free_branches",
Mingming Cao2ae02102006-10-11 01:21:11 -07003819 "Read failure, inode=%lu, block=%llu",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003820 inode->i_ino, nr);
3821 continue;
3822 }
3823
3824 /* This zaps the entire block. Bottom up. */
3825 BUFFER_TRACE(bh, "free child branches");
Mingming Cao617ba132006-10-11 01:20:53 -07003826 ext4_free_branches(handle, inode, bh,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003827 (__le32 *) bh->b_data,
3828 (__le32 *) bh->b_data + addr_per_block,
3829 depth);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003830
3831 /*
3832 * We've probably journalled the indirect block several
3833 * times during the truncate. But it's no longer
3834 * needed and we now drop it from the transaction via
Mingming Caodab291a2006-10-11 01:21:01 -07003835 * jbd2_journal_revoke().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003836 *
3837 * That's easy if it's exclusively part of this
3838 * transaction. But if it's part of the committing
Mingming Caodab291a2006-10-11 01:21:01 -07003839 * transaction then jbd2_journal_forget() will simply
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003840 * brelse() it. That means that if the underlying
Mingming Cao617ba132006-10-11 01:20:53 -07003841 * block is reallocated in ext4_get_block(),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003842 * unmap_underlying_metadata() will find this block
3843 * and will try to get rid of it. damn, damn.
3844 *
3845 * If this block has already been committed to the
3846 * journal, a revoke record will be written. And
3847 * revoke records must be emitted *before* clearing
3848 * this block's bit in the bitmaps.
3849 */
Mingming Cao617ba132006-10-11 01:20:53 -07003850 ext4_forget(handle, 1, inode, bh, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003851
3852 /*
3853 * Everything below this this pointer has been
3854 * released. Now let this top-of-subtree go.
3855 *
3856 * We want the freeing of this indirect block to be
3857 * atomic in the journal with the updating of the
3858 * bitmap block which owns it. So make some room in
3859 * the journal.
3860 *
3861 * We zero the parent pointer *after* freeing its
3862 * pointee in the bitmaps, so if extend_transaction()
3863 * for some reason fails to put the bitmap changes and
3864 * the release into the same transaction, recovery
3865 * will merely complain about releasing a free block,
3866 * rather than leaking blocks.
3867 */
Frank Mayhar03901312009-01-07 00:06:22 -05003868 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003869 return;
3870 if (try_to_extend_transaction(handle, inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003871 ext4_mark_inode_dirty(handle, inode);
3872 ext4_journal_test_restart(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003873 }
3874
Alex Tomasc9de5602008-01-29 00:19:52 -05003875 ext4_free_blocks(handle, inode, nr, 1, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003876
3877 if (parent_bh) {
3878 /*
3879 * The block which we have just freed is
3880 * pointed to by an indirect block: journal it
3881 */
3882 BUFFER_TRACE(parent_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003883 if (!ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003884 parent_bh)){
3885 *p = 0;
3886 BUFFER_TRACE(parent_bh,
Frank Mayhar03901312009-01-07 00:06:22 -05003887 "call ext4_handle_dirty_metadata");
3888 ext4_handle_dirty_metadata(handle,
3889 inode,
3890 parent_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003891 }
3892 }
3893 }
3894 } else {
3895 /* We have reached the bottom of the tree. */
3896 BUFFER_TRACE(parent_bh, "free data blocks");
Mingming Cao617ba132006-10-11 01:20:53 -07003897 ext4_free_data(handle, inode, parent_bh, first, last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003898 }
3899}
3900
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003901int ext4_can_truncate(struct inode *inode)
3902{
3903 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3904 return 0;
3905 if (S_ISREG(inode->i_mode))
3906 return 1;
3907 if (S_ISDIR(inode->i_mode))
3908 return 1;
3909 if (S_ISLNK(inode->i_mode))
3910 return !ext4_inode_is_fast_symlink(inode);
3911 return 0;
3912}
3913
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003914/*
Mingming Cao617ba132006-10-11 01:20:53 -07003915 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003916 *
Mingming Cao617ba132006-10-11 01:20:53 -07003917 * We block out ext4_get_block() block instantiations across the entire
3918 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003919 * simultaneously on behalf of the same inode.
3920 *
3921 * As we work through the truncate and commmit bits of it to the journal there
3922 * is one core, guiding principle: the file's tree must always be consistent on
3923 * disk. We must be able to restart the truncate after a crash.
3924 *
3925 * The file's tree may be transiently inconsistent in memory (although it
3926 * probably isn't), but whenever we close off and commit a journal transaction,
3927 * the contents of (the filesystem + the journal) must be consistent and
3928 * restartable. It's pretty simple, really: bottom up, right to left (although
3929 * left-to-right works OK too).
3930 *
3931 * Note that at recovery time, journal replay occurs *before* the restart of
3932 * truncate against the orphan inode list.
3933 *
3934 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07003935 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003936 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07003937 * ext4_truncate() to have another go. So there will be instantiated blocks
3938 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003939 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07003940 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003941 */
Mingming Cao617ba132006-10-11 01:20:53 -07003942void ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003943{
3944 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -07003945 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003946 __le32 *i_data = ei->i_data;
Mingming Cao617ba132006-10-11 01:20:53 -07003947 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003948 struct address_space *mapping = inode->i_mapping;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003949 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003950 Indirect chain[4];
3951 Indirect *partial;
3952 __le32 nr = 0;
3953 int n;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003954 ext4_lblk_t last_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003955 unsigned blocksize = inode->i_sb->s_blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003956
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003957 if (!ext4_can_truncate(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003958 return;
3959
Theodore Ts'o0eab9282009-06-09 09:54:40 -04003960 if (ei->i_disksize && inode->i_size == 0 &&
3961 !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o7d8f9f72009-02-24 08:21:14 -05003962 ei->i_state |= EXT4_STATE_DA_ALLOC_CLOSE;
3963
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003964 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
Jan Karacf108bc2008-07-11 19:27:31 -04003965 ext4_ext_truncate(inode);
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003966 return;
3967 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003968
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003969 handle = start_transaction(inode);
Jan Karacf108bc2008-07-11 19:27:31 -04003970 if (IS_ERR(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003971 return; /* AKPM: return what? */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003972
3973 last_block = (inode->i_size + blocksize-1)
Mingming Cao617ba132006-10-11 01:20:53 -07003974 >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003975
Jan Karacf108bc2008-07-11 19:27:31 -04003976 if (inode->i_size & (blocksize - 1))
3977 if (ext4_block_truncate_page(handle, mapping, inode->i_size))
3978 goto out_stop;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003979
Mingming Cao617ba132006-10-11 01:20:53 -07003980 n = ext4_block_to_path(inode, last_block, offsets, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003981 if (n == 0)
3982 goto out_stop; /* error */
3983
3984 /*
3985 * OK. This truncate is going to happen. We add the inode to the
3986 * orphan list, so that if this truncate spans multiple transactions,
3987 * and we crash, we will resume the truncate when the filesystem
3988 * recovers. It also marks the inode dirty, to catch the new size.
3989 *
3990 * Implication: the file must always be in a sane, consistent
3991 * truncatable state while each transaction commits.
3992 */
Mingming Cao617ba132006-10-11 01:20:53 -07003993 if (ext4_orphan_add(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003994 goto out_stop;
3995
3996 /*
Mingming Cao632eaea2008-07-11 19:27:31 -04003997 * From here we block out all ext4_get_block() callers who want to
3998 * modify the block allocation tree.
3999 */
4000 down_write(&ei->i_data_sem);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04004001
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004002 ext4_discard_preallocations(inode);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04004003
Mingming Cao632eaea2008-07-11 19:27:31 -04004004 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004005 * The orphan list entry will now protect us from any crash which
4006 * occurs before the truncate completes, so it is now safe to propagate
4007 * the new, shorter inode size (held for now in i_size) into the
4008 * on-disk inode. We do this via i_disksize, which is the value which
Mingming Cao617ba132006-10-11 01:20:53 -07004009 * ext4 *really* writes onto the disk inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004010 */
4011 ei->i_disksize = inode->i_size;
4012
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004013 if (n == 1) { /* direct blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07004014 ext4_free_data(handle, inode, NULL, i_data+offsets[0],
4015 i_data + EXT4_NDIR_BLOCKS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004016 goto do_indirects;
4017 }
4018
Mingming Cao617ba132006-10-11 01:20:53 -07004019 partial = ext4_find_shared(inode, n, offsets, chain, &nr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004020 /* Kill the top of shared branch (not detached) */
4021 if (nr) {
4022 if (partial == chain) {
4023 /* Shared branch grows from the inode */
Mingming Cao617ba132006-10-11 01:20:53 -07004024 ext4_free_branches(handle, inode, NULL,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004025 &nr, &nr+1, (chain+n-1) - partial);
4026 *partial->p = 0;
4027 /*
4028 * We mark the inode dirty prior to restart,
4029 * and prior to stop. No need for it here.
4030 */
4031 } else {
4032 /* Shared branch grows from an indirect block */
4033 BUFFER_TRACE(partial->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07004034 ext4_free_branches(handle, inode, partial->bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004035 partial->p,
4036 partial->p+1, (chain+n-1) - partial);
4037 }
4038 }
4039 /* Clear the ends of indirect blocks on the shared branch */
4040 while (partial > chain) {
Mingming Cao617ba132006-10-11 01:20:53 -07004041 ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004042 (__le32*)partial->bh->b_data+addr_per_block,
4043 (chain+n-1) - partial);
4044 BUFFER_TRACE(partial->bh, "call brelse");
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004045 brelse(partial->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004046 partial--;
4047 }
4048do_indirects:
4049 /* Kill the remaining (whole) subtrees */
4050 switch (offsets[0]) {
4051 default:
Mingming Cao617ba132006-10-11 01:20:53 -07004052 nr = i_data[EXT4_IND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004053 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004054 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
4055 i_data[EXT4_IND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004056 }
Mingming Cao617ba132006-10-11 01:20:53 -07004057 case EXT4_IND_BLOCK:
4058 nr = i_data[EXT4_DIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004059 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004060 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
4061 i_data[EXT4_DIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004062 }
Mingming Cao617ba132006-10-11 01:20:53 -07004063 case EXT4_DIND_BLOCK:
4064 nr = i_data[EXT4_TIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004065 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004066 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
4067 i_data[EXT4_TIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004068 }
Mingming Cao617ba132006-10-11 01:20:53 -07004069 case EXT4_TIND_BLOCK:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004070 ;
4071 }
4072
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004073 up_write(&ei->i_data_sem);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004074 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07004075 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004076
4077 /*
4078 * In a multi-transaction truncate, we only make the final transaction
4079 * synchronous
4080 */
4081 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05004082 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004083out_stop:
4084 /*
4085 * If this was a simple ftruncate(), and the file will remain alive
4086 * then we need to clear up the orphan record which we created above.
4087 * However, if this was a real unlink then we were called by
Mingming Cao617ba132006-10-11 01:20:53 -07004088 * ext4_delete_inode(), and we allow that function to clean up the
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004089 * orphan info for us.
4090 */
4091 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07004092 ext4_orphan_del(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004093
Mingming Cao617ba132006-10-11 01:20:53 -07004094 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004095}
4096
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004097/*
Mingming Cao617ba132006-10-11 01:20:53 -07004098 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004099 * underlying buffer_head on success. If 'in_mem' is true, we have all
4100 * data in memory that is needed to recreate the on-disk version of this
4101 * inode.
4102 */
Mingming Cao617ba132006-10-11 01:20:53 -07004103static int __ext4_get_inode_loc(struct inode *inode,
4104 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004105{
Theodore Ts'o240799c2008-10-09 23:53:47 -04004106 struct ext4_group_desc *gdp;
4107 struct buffer_head *bh;
4108 struct super_block *sb = inode->i_sb;
4109 ext4_fsblk_t block;
4110 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004111
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004112 iloc->bh = NULL;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004113 if (!ext4_valid_inum(sb, inode->i_ino))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004114 return -EIO;
4115
Theodore Ts'o240799c2008-10-09 23:53:47 -04004116 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
4117 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
4118 if (!gdp)
4119 return -EIO;
4120
4121 /*
4122 * Figure out the offset within the block group inode table
4123 */
4124 inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb));
4125 inode_offset = ((inode->i_ino - 1) %
4126 EXT4_INODES_PER_GROUP(sb));
4127 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4128 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4129
4130 bh = sb_getblk(sb, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004131 if (!bh) {
Theodore Ts'o240799c2008-10-09 23:53:47 -04004132 ext4_error(sb, "ext4_get_inode_loc", "unable to read "
4133 "inode block - inode=%lu, block=%llu",
4134 inode->i_ino, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004135 return -EIO;
4136 }
4137 if (!buffer_uptodate(bh)) {
4138 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04004139
4140 /*
4141 * If the buffer has the write error flag, we have failed
4142 * to write out another inode in the same block. In this
4143 * case, we don't have to read the block because we may
4144 * read the old inode data successfully.
4145 */
4146 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
4147 set_buffer_uptodate(bh);
4148
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004149 if (buffer_uptodate(bh)) {
4150 /* someone brought it uptodate while we waited */
4151 unlock_buffer(bh);
4152 goto has_buffer;
4153 }
4154
4155 /*
4156 * If we have all information of the inode in memory and this
4157 * is the only valid inode in the block, we need not read the
4158 * block.
4159 */
4160 if (in_mem) {
4161 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004162 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004163
Theodore Ts'o240799c2008-10-09 23:53:47 -04004164 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004165
4166 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004167 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004168 if (!bitmap_bh)
4169 goto make_io;
4170
4171 /*
4172 * If the inode bitmap isn't in cache then the
4173 * optimisation may end up performing two reads instead
4174 * of one, so skip it.
4175 */
4176 if (!buffer_uptodate(bitmap_bh)) {
4177 brelse(bitmap_bh);
4178 goto make_io;
4179 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04004180 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004181 if (i == inode_offset)
4182 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07004183 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004184 break;
4185 }
4186 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004187 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004188 /* all other inodes are free, so skip I/O */
4189 memset(bh->b_data, 0, bh->b_size);
4190 set_buffer_uptodate(bh);
4191 unlock_buffer(bh);
4192 goto has_buffer;
4193 }
4194 }
4195
4196make_io:
4197 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04004198 * If we need to do any I/O, try to pre-readahead extra
4199 * blocks from the inode table.
4200 */
4201 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4202 ext4_fsblk_t b, end, table;
4203 unsigned num;
4204
4205 table = ext4_inode_table(sb, gdp);
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004206 /* s_inode_readahead_blks is always a power of 2 */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004207 b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
4208 if (table > b)
4209 b = table;
4210 end = b + EXT4_SB(sb)->s_inode_readahead_blks;
4211 num = EXT4_INODES_PER_GROUP(sb);
4212 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4213 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004214 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004215 table += num / inodes_per_block;
4216 if (end > table)
4217 end = table;
4218 while (b <= end)
4219 sb_breadahead(sb, b++);
4220 }
4221
4222 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004223 * There are other valid inodes in the buffer, this inode
4224 * has in-inode xattrs, or we don't have this inode in memory.
4225 * Read the block from disk.
4226 */
4227 get_bh(bh);
4228 bh->b_end_io = end_buffer_read_sync;
4229 submit_bh(READ_META, bh);
4230 wait_on_buffer(bh);
4231 if (!buffer_uptodate(bh)) {
Theodore Ts'o240799c2008-10-09 23:53:47 -04004232 ext4_error(sb, __func__,
4233 "unable to read inode block - inode=%lu, "
4234 "block=%llu", inode->i_ino, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004235 brelse(bh);
4236 return -EIO;
4237 }
4238 }
4239has_buffer:
4240 iloc->bh = bh;
4241 return 0;
4242}
4243
Mingming Cao617ba132006-10-11 01:20:53 -07004244int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004245{
4246 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07004247 return __ext4_get_inode_loc(inode, iloc,
4248 !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004249}
4250
Mingming Cao617ba132006-10-11 01:20:53 -07004251void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004252{
Mingming Cao617ba132006-10-11 01:20:53 -07004253 unsigned int flags = EXT4_I(inode)->i_flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004254
4255 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
Mingming Cao617ba132006-10-11 01:20:53 -07004256 if (flags & EXT4_SYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004257 inode->i_flags |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07004258 if (flags & EXT4_APPEND_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004259 inode->i_flags |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07004260 if (flags & EXT4_IMMUTABLE_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004261 inode->i_flags |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07004262 if (flags & EXT4_NOATIME_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004263 inode->i_flags |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004264 if (flags & EXT4_DIRSYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004265 inode->i_flags |= S_DIRSYNC;
4266}
4267
Jan Karaff9ddf72007-07-18 09:24:20 -04004268/* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4269void ext4_get_inode_flags(struct ext4_inode_info *ei)
4270{
4271 unsigned int flags = ei->vfs_inode.i_flags;
4272
4273 ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
4274 EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL);
4275 if (flags & S_SYNC)
4276 ei->i_flags |= EXT4_SYNC_FL;
4277 if (flags & S_APPEND)
4278 ei->i_flags |= EXT4_APPEND_FL;
4279 if (flags & S_IMMUTABLE)
4280 ei->i_flags |= EXT4_IMMUTABLE_FL;
4281 if (flags & S_NOATIME)
4282 ei->i_flags |= EXT4_NOATIME_FL;
4283 if (flags & S_DIRSYNC)
4284 ei->i_flags |= EXT4_DIRSYNC_FL;
4285}
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004286
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004287static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004288 struct ext4_inode_info *ei)
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004289{
4290 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004291 struct inode *inode = &(ei->vfs_inode);
4292 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004293
4294 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4295 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
4296 /* we are using combined 48 bit field */
4297 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4298 le32_to_cpu(raw_inode->i_blocks_lo);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004299 if (ei->i_flags & EXT4_HUGE_FILE_FL) {
4300 /* i_blocks represent file system block size */
4301 return i_blocks << (inode->i_blkbits - 9);
4302 } else {
4303 return i_blocks;
4304 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004305 } else {
4306 return le32_to_cpu(raw_inode->i_blocks_lo);
4307 }
4308}
Jan Karaff9ddf72007-07-18 09:24:20 -04004309
David Howells1d1fe1e2008-02-07 00:15:37 -08004310struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004311{
Mingming Cao617ba132006-10-11 01:20:53 -07004312 struct ext4_iloc iloc;
4313 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004314 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004315 struct buffer_head *bh;
David Howells1d1fe1e2008-02-07 00:15:37 -08004316 struct inode *inode;
4317 long ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004318 int block;
4319
David Howells1d1fe1e2008-02-07 00:15:37 -08004320 inode = iget_locked(sb, ino);
4321 if (!inode)
4322 return ERR_PTR(-ENOMEM);
4323 if (!(inode->i_state & I_NEW))
4324 return inode;
4325
4326 ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004327
David Howells1d1fe1e2008-02-07 00:15:37 -08004328 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4329 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004330 goto bad_inode;
4331 bh = iloc.bh;
Mingming Cao617ba132006-10-11 01:20:53 -07004332 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004333 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
4334 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4335 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004336 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004337 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4338 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
4339 }
4340 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004341
4342 ei->i_state = 0;
4343 ei->i_dir_start_lookup = 0;
4344 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4345 /* We now have enough fields to check if the inode was active or not.
4346 * This is needed because nfsd might try to access dead inodes
4347 * the test is that same one that e2fsck uses
4348 * NeilBrown 1999oct15
4349 */
4350 if (inode->i_nlink == 0) {
4351 if (inode->i_mode == 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07004352 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004353 /* this inode is deleted */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004354 brelse(bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004355 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004356 goto bad_inode;
4357 }
4358 /* The only unlinked inodes we let through here have
4359 * valid i_mode and are being read by the orphan
4360 * recovery code: that's fine, we're about to complete
4361 * the process of deleting those. */
4362 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004363 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004364 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004365 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Theodore Ts'oa9e81742009-04-24 16:11:18 -04004366 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004367 ei->i_file_acl |=
4368 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004369 inode->i_size = ext4_isize(raw_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004370 ei->i_disksize = inode->i_size;
4371 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4372 ei->i_block_group = iloc.block_group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04004373 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004374 /*
4375 * NOTE! The in-memory inode i_data array is in little-endian order
4376 * even on big-endian machines: we do NOT byteswap the block numbers!
4377 */
Mingming Cao617ba132006-10-11 01:20:53 -07004378 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004379 ei->i_data[block] = raw_inode->i_block[block];
4380 INIT_LIST_HEAD(&ei->i_orphan);
4381
Eric Sandeen0040d982008-02-05 22:36:43 -05004382 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004383 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
Mingming Cao617ba132006-10-11 01:20:53 -07004384 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004385 EXT4_INODE_SIZE(inode->i_sb)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004386 brelse(bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004387 ret = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004388 goto bad_inode;
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004389 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004390 if (ei->i_extra_isize == 0) {
4391 /* The extra space is currently unused. Use it. */
Mingming Cao617ba132006-10-11 01:20:53 -07004392 ei->i_extra_isize = sizeof(struct ext4_inode) -
4393 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004394 } else {
4395 __le32 *magic = (void *)raw_inode +
Mingming Cao617ba132006-10-11 01:20:53 -07004396 EXT4_GOOD_OLD_INODE_SIZE +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004397 ei->i_extra_isize;
Mingming Cao617ba132006-10-11 01:20:53 -07004398 if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC))
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004399 ei->i_state |= EXT4_STATE_XATTR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004400 }
4401 } else
4402 ei->i_extra_isize = 0;
4403
Kalpak Shahef7f3832007-07-18 09:15:20 -04004404 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4405 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4406 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4407 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4408
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004409 inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
4410 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4411 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4412 inode->i_version |=
4413 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4414 }
4415
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004416 ret = 0;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004417 if (ei->i_file_acl &&
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004418 ((ei->i_file_acl <
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004419 (le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) +
4420 EXT4_SB(sb)->s_gdb_count)) ||
4421 (ei->i_file_acl >= ext4_blocks_count(EXT4_SB(sb)->s_es)))) {
4422 ext4_error(sb, __func__,
4423 "bad extended attribute block %llu in inode #%lu",
4424 ei->i_file_acl, inode->i_ino);
4425 ret = -EIO;
4426 goto bad_inode;
4427 } else if (ei->i_flags & EXT4_EXTENTS_FL) {
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004428 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4429 (S_ISLNK(inode->i_mode) &&
4430 !ext4_inode_is_fast_symlink(inode)))
4431 /* Validate extent which is part of inode */
4432 ret = ext4_ext_check_inode(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004433 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004434 (S_ISLNK(inode->i_mode) &&
4435 !ext4_inode_is_fast_symlink(inode))) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004436 /* Validate block references which are part of inode */
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004437 ret = ext4_check_inode_blockref(inode);
4438 }
4439 if (ret) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004440 brelse(bh);
4441 goto bad_inode;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -04004442 }
4443
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004444 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004445 inode->i_op = &ext4_file_inode_operations;
4446 inode->i_fop = &ext4_file_operations;
4447 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004448 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004449 inode->i_op = &ext4_dir_inode_operations;
4450 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004451 } else if (S_ISLNK(inode->i_mode)) {
Duane Griffine83c1392008-12-19 20:47:15 +00004452 if (ext4_inode_is_fast_symlink(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004453 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00004454 nd_terminate_link(ei->i_data, inode->i_size,
4455 sizeof(ei->i_data) - 1);
4456 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004457 inode->i_op = &ext4_symlink_inode_operations;
4458 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004459 }
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004460 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4461 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004462 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004463 if (raw_inode->i_block[0])
4464 init_special_inode(inode, inode->i_mode,
4465 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4466 else
4467 init_special_inode(inode, inode->i_mode,
4468 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004469 } else {
4470 brelse(bh);
4471 ret = -EIO;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004472 ext4_error(inode->i_sb, __func__,
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004473 "bogus i_mode (%o) for inode=%lu",
4474 inode->i_mode, inode->i_ino);
4475 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004476 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004477 brelse(iloc.bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004478 ext4_set_inode_flags(inode);
David Howells1d1fe1e2008-02-07 00:15:37 -08004479 unlock_new_inode(inode);
4480 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004481
4482bad_inode:
David Howells1d1fe1e2008-02-07 00:15:37 -08004483 iget_failed(inode);
4484 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004485}
4486
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004487static int ext4_inode_blocks_set(handle_t *handle,
4488 struct ext4_inode *raw_inode,
4489 struct ext4_inode_info *ei)
4490{
4491 struct inode *inode = &(ei->vfs_inode);
4492 u64 i_blocks = inode->i_blocks;
4493 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004494
4495 if (i_blocks <= ~0U) {
4496 /*
4497 * i_blocks can be represnted in a 32 bit variable
4498 * as multiple of 512 bytes
4499 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004500 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004501 raw_inode->i_blocks_high = 0;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004502 ei->i_flags &= ~EXT4_HUGE_FILE_FL;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004503 return 0;
4504 }
4505 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
4506 return -EFBIG;
4507
4508 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004509 /*
4510 * i_blocks can be represented in a 48 bit variable
4511 * as multiple of 512 bytes
4512 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004513 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004514 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004515 ei->i_flags &= ~EXT4_HUGE_FILE_FL;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004516 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004517 ei->i_flags |= EXT4_HUGE_FILE_FL;
4518 /* i_block is stored in file system block size */
4519 i_blocks = i_blocks >> (inode->i_blkbits - 9);
4520 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4521 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004522 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004523 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004524}
4525
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004526/*
4527 * Post the struct inode info into an on-disk inode location in the
4528 * buffer-cache. This gobbles the caller's reference to the
4529 * buffer_head in the inode location struct.
4530 *
4531 * The caller must have write access to iloc->bh.
4532 */
Mingming Cao617ba132006-10-11 01:20:53 -07004533static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004534 struct inode *inode,
Mingming Cao617ba132006-10-11 01:20:53 -07004535 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004536{
Mingming Cao617ba132006-10-11 01:20:53 -07004537 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4538 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004539 struct buffer_head *bh = iloc->bh;
4540 int err = 0, rc, block;
4541
4542 /* For fields not not tracking in the in-memory inode,
4543 * initialise them to zero for new inodes. */
Mingming Cao617ba132006-10-11 01:20:53 -07004544 if (ei->i_state & EXT4_STATE_NEW)
4545 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004546
Jan Karaff9ddf72007-07-18 09:24:20 -04004547 ext4_get_inode_flags(ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004548 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004549 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004550 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
4551 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
4552/*
4553 * Fix up interoperability with old kernels. Otherwise, old inodes get
4554 * re-used with the upper 16 bits of the uid/gid intact
4555 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004556 if (!ei->i_dtime) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004557 raw_inode->i_uid_high =
4558 cpu_to_le16(high_16_bits(inode->i_uid));
4559 raw_inode->i_gid_high =
4560 cpu_to_le16(high_16_bits(inode->i_gid));
4561 } else {
4562 raw_inode->i_uid_high = 0;
4563 raw_inode->i_gid_high = 0;
4564 }
4565 } else {
4566 raw_inode->i_uid_low =
4567 cpu_to_le16(fs_high2lowuid(inode->i_uid));
4568 raw_inode->i_gid_low =
4569 cpu_to_le16(fs_high2lowgid(inode->i_gid));
4570 raw_inode->i_uid_high = 0;
4571 raw_inode->i_gid_high = 0;
4572 }
4573 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004574
4575 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4576 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4577 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4578 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4579
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004580 if (ext4_inode_blocks_set(handle, raw_inode, ei))
4581 goto out_brelse;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004582 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04004583 /* clear the migrate flag in the raw_inode */
4584 raw_inode->i_flags = cpu_to_le32(ei->i_flags & ~EXT4_EXT_MIGRATE);
Mingming Cao9b8f1f02006-10-11 01:21:13 -07004585 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
4586 cpu_to_le32(EXT4_OS_HURD))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004587 raw_inode->i_file_acl_high =
4588 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004589 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004590 ext4_isize_set(raw_inode, ei->i_disksize);
4591 if (ei->i_disksize > 0x7fffffffULL) {
4592 struct super_block *sb = inode->i_sb;
4593 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
4594 EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
4595 EXT4_SB(sb)->s_es->s_rev_level ==
4596 cpu_to_le32(EXT4_GOOD_OLD_REV)) {
4597 /* If this is the first large file
4598 * created, add a flag to the superblock.
4599 */
4600 err = ext4_journal_get_write_access(handle,
4601 EXT4_SB(sb)->s_sbh);
4602 if (err)
4603 goto out_brelse;
4604 ext4_update_dynamic_rev(sb);
4605 EXT4_SET_RO_COMPAT_FEATURE(sb,
Mingming Cao617ba132006-10-11 01:20:53 -07004606 EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004607 sb->s_dirt = 1;
Frank Mayhar03901312009-01-07 00:06:22 -05004608 ext4_handle_sync(handle);
4609 err = ext4_handle_dirty_metadata(handle, inode,
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004610 EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004611 }
4612 }
4613 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
4614 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4615 if (old_valid_dev(inode->i_rdev)) {
4616 raw_inode->i_block[0] =
4617 cpu_to_le32(old_encode_dev(inode->i_rdev));
4618 raw_inode->i_block[1] = 0;
4619 } else {
4620 raw_inode->i_block[0] = 0;
4621 raw_inode->i_block[1] =
4622 cpu_to_le32(new_encode_dev(inode->i_rdev));
4623 raw_inode->i_block[2] = 0;
4624 }
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004625 } else
4626 for (block = 0; block < EXT4_N_BLOCKS; block++)
4627 raw_inode->i_block[block] = ei->i_data[block];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004628
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004629 raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
4630 if (ei->i_extra_isize) {
4631 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4632 raw_inode->i_version_hi =
4633 cpu_to_le32(inode->i_version >> 32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004634 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004635 }
4636
Frank Mayhar03901312009-01-07 00:06:22 -05004637 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
4638 rc = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004639 if (!err)
4640 err = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07004641 ei->i_state &= ~EXT4_STATE_NEW;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004642
4643out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004644 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004645 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004646 return err;
4647}
4648
4649/*
Mingming Cao617ba132006-10-11 01:20:53 -07004650 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004651 *
4652 * We are called from a few places:
4653 *
4654 * - Within generic_file_write() for O_SYNC files.
4655 * Here, there will be no transaction running. We wait for any running
4656 * trasnaction to commit.
4657 *
4658 * - Within sys_sync(), kupdate and such.
4659 * We wait on commit, if tol to.
4660 *
4661 * - Within prune_icache() (PF_MEMALLOC == true)
4662 * Here we simply return. We can't afford to block kswapd on the
4663 * journal commit.
4664 *
4665 * In all cases it is actually safe for us to return without doing anything,
4666 * because the inode has been copied into a raw inode buffer in
Mingming Cao617ba132006-10-11 01:20:53 -07004667 * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004668 * knfsd.
4669 *
4670 * Note that we are absolutely dependent upon all inode dirtiers doing the
4671 * right thing: they *must* call mark_inode_dirty() after dirtying info in
4672 * which we are interested.
4673 *
4674 * It would be a bug for them to not do this. The code:
4675 *
4676 * mark_inode_dirty(inode)
4677 * stuff();
4678 * inode->i_size = expr;
4679 *
4680 * is in error because a kswapd-driven write_inode() could occur while
4681 * `stuff()' is running, and the new i_size will be lost. Plus the inode
4682 * will no longer be on the superblock's dirty inode list.
4683 */
Mingming Cao617ba132006-10-11 01:20:53 -07004684int ext4_write_inode(struct inode *inode, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004685{
4686 if (current->flags & PF_MEMALLOC)
4687 return 0;
4688
Mingming Cao617ba132006-10-11 01:20:53 -07004689 if (ext4_journal_current_handle()) {
Mingming Caob38bd332007-07-19 01:48:35 -07004690 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004691 dump_stack();
4692 return -EIO;
4693 }
4694
4695 if (!wait)
4696 return 0;
4697
Mingming Cao617ba132006-10-11 01:20:53 -07004698 return ext4_force_commit(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004699}
4700
4701/*
Mingming Cao617ba132006-10-11 01:20:53 -07004702 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004703 *
4704 * Called from notify_change.
4705 *
4706 * We want to trap VFS attempts to truncate the file as soon as
4707 * possible. In particular, we want to make sure that when the VFS
4708 * shrinks i_size, we put the inode on the orphan list and modify
4709 * i_disksize immediately, so that during the subsequent flushing of
4710 * dirty pages and freeing of disk blocks, we can guarantee that any
4711 * commit will leave the blocks being flushed in an unused state on
4712 * disk. (On recovery, the inode will get truncated and the blocks will
4713 * be freed, so we have a strong guarantee that no future commit will
4714 * leave these blocks visible to the user.)
4715 *
Jan Kara678aaf42008-07-11 19:27:31 -04004716 * Another thing we have to assure is that if we are in ordered mode
4717 * and inode is still attached to the committing transaction, we must
4718 * we start writeout of all the dirty pages which are being truncated.
4719 * This way we are sure that all the data written in the previous
4720 * transaction are already on disk (truncate waits for pages under
4721 * writeback).
4722 *
4723 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004724 */
Mingming Cao617ba132006-10-11 01:20:53 -07004725int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004726{
4727 struct inode *inode = dentry->d_inode;
4728 int error, rc = 0;
4729 const unsigned int ia_valid = attr->ia_valid;
4730
4731 error = inode_change_ok(inode, attr);
4732 if (error)
4733 return error;
4734
4735 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
4736 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
4737 handle_t *handle;
4738
4739 /* (user+group)*(old+new) structure, inode write (sb,
4740 * inode block, ? - but truncate inode update has it) */
Mingming Cao617ba132006-10-11 01:20:53 -07004741 handle = ext4_journal_start(inode, 2*(EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)+
4742 EXT4_QUOTA_DEL_BLOCKS(inode->i_sb))+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004743 if (IS_ERR(handle)) {
4744 error = PTR_ERR(handle);
4745 goto err_out;
4746 }
Jan Karaa269eb12009-01-26 17:04:39 +01004747 error = vfs_dq_transfer(inode, attr) ? -EDQUOT : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004748 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07004749 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004750 return error;
4751 }
4752 /* Update corresponding info in inode so that everything is in
4753 * one transaction */
4754 if (attr->ia_valid & ATTR_UID)
4755 inode->i_uid = attr->ia_uid;
4756 if (attr->ia_valid & ATTR_GID)
4757 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07004758 error = ext4_mark_inode_dirty(handle, inode);
4759 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004760 }
4761
Eric Sandeene2b46572008-01-28 23:58:27 -05004762 if (attr->ia_valid & ATTR_SIZE) {
4763 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) {
4764 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4765
4766 if (attr->ia_size > sbi->s_bitmap_maxbytes) {
4767 error = -EFBIG;
4768 goto err_out;
4769 }
4770 }
4771 }
4772
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004773 if (S_ISREG(inode->i_mode) &&
4774 attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
4775 handle_t *handle;
4776
Mingming Cao617ba132006-10-11 01:20:53 -07004777 handle = ext4_journal_start(inode, 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004778 if (IS_ERR(handle)) {
4779 error = PTR_ERR(handle);
4780 goto err_out;
4781 }
4782
Mingming Cao617ba132006-10-11 01:20:53 -07004783 error = ext4_orphan_add(handle, inode);
4784 EXT4_I(inode)->i_disksize = attr->ia_size;
4785 rc = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004786 if (!error)
4787 error = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07004788 ext4_journal_stop(handle);
Jan Kara678aaf42008-07-11 19:27:31 -04004789
4790 if (ext4_should_order_data(inode)) {
4791 error = ext4_begin_ordered_truncate(inode,
4792 attr->ia_size);
4793 if (error) {
4794 /* Do as much error cleanup as possible */
4795 handle = ext4_journal_start(inode, 3);
4796 if (IS_ERR(handle)) {
4797 ext4_orphan_del(NULL, inode);
4798 goto err_out;
4799 }
4800 ext4_orphan_del(handle, inode);
4801 ext4_journal_stop(handle);
4802 goto err_out;
4803 }
4804 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004805 }
4806
4807 rc = inode_setattr(inode, attr);
4808
Mingming Cao617ba132006-10-11 01:20:53 -07004809 /* If inode_setattr's call to ext4_truncate failed to get a
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004810 * transaction handle at all, we need to clean up the in-core
4811 * orphan list manually. */
4812 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07004813 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004814
4815 if (!rc && (ia_valid & ATTR_MODE))
Mingming Cao617ba132006-10-11 01:20:53 -07004816 rc = ext4_acl_chmod(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004817
4818err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07004819 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004820 if (!error)
4821 error = rc;
4822 return error;
4823}
4824
Mingming Cao3e3398a2008-07-11 19:27:31 -04004825int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
4826 struct kstat *stat)
4827{
4828 struct inode *inode;
4829 unsigned long delalloc_blocks;
4830
4831 inode = dentry->d_inode;
4832 generic_fillattr(inode, stat);
4833
4834 /*
4835 * We can't update i_blocks if the block allocation is delayed
4836 * otherwise in the case of system crash before the real block
4837 * allocation is done, we will have i_blocks inconsistent with
4838 * on-disk file blocks.
4839 * We always keep i_blocks updated together with real
4840 * allocation. But to not confuse with user, stat
4841 * will return the blocks that include the delayed allocation
4842 * blocks for this file.
4843 */
4844 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
4845 delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks;
4846 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
4847
4848 stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
4849 return 0;
4850}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004851
Mingming Caoa02908f2008-08-19 22:16:07 -04004852static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks,
4853 int chunk)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004854{
Mingming Caoa02908f2008-08-19 22:16:07 -04004855 int indirects;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004856
Mingming Caoa02908f2008-08-19 22:16:07 -04004857 /* if nrblocks are contiguous */
4858 if (chunk) {
4859 /*
4860 * With N contiguous data blocks, it need at most
4861 * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks
4862 * 2 dindirect blocks
4863 * 1 tindirect block
4864 */
4865 indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb);
4866 return indirects + 3;
4867 }
4868 /*
4869 * if nrblocks are not contiguous, worse case, each block touch
4870 * a indirect block, and each indirect block touch a double indirect
4871 * block, plus a triple indirect block
4872 */
4873 indirects = nrblocks * 2 + 1;
4874 return indirects;
4875}
Alex Tomasa86c6182006-10-11 01:21:03 -07004876
Mingming Caoa02908f2008-08-19 22:16:07 -04004877static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4878{
4879 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
Theodore Ts'oac51d832008-11-06 16:49:36 -05004880 return ext4_indirect_trans_blocks(inode, nrblocks, chunk);
4881 return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
Mingming Caoa02908f2008-08-19 22:16:07 -04004882}
Theodore Ts'oac51d832008-11-06 16:49:36 -05004883
Mingming Caoa02908f2008-08-19 22:16:07 -04004884/*
4885 * Account for index blocks, block groups bitmaps and block group
4886 * descriptor blocks if modify datablocks and index blocks
4887 * worse case, the indexs blocks spread over different block groups
4888 *
4889 * If datablocks are discontiguous, they are possible to spread over
4890 * different block groups too. If they are contiugous, with flexbg,
4891 * they could still across block group boundary.
4892 *
4893 * Also account for superblock, inode, quota and xattr blocks
4894 */
4895int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4896{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004897 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
4898 int gdpblocks;
Mingming Caoa02908f2008-08-19 22:16:07 -04004899 int idxblocks;
4900 int ret = 0;
4901
4902 /*
4903 * How many index blocks need to touch to modify nrblocks?
4904 * The "Chunk" flag indicating whether the nrblocks is
4905 * physically contiguous on disk
4906 *
4907 * For Direct IO and fallocate, they calls get_block to allocate
4908 * one single extent at a time, so they could set the "Chunk" flag
4909 */
4910 idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
4911
4912 ret = idxblocks;
4913
4914 /*
4915 * Now let's see how many group bitmaps and group descriptors need
4916 * to account
4917 */
4918 groups = idxblocks;
4919 if (chunk)
4920 groups += 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004921 else
Mingming Caoa02908f2008-08-19 22:16:07 -04004922 groups += nrblocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004923
Mingming Caoa02908f2008-08-19 22:16:07 -04004924 gdpblocks = groups;
Theodore Ts'o8df96752009-05-01 08:50:38 -04004925 if (groups > ngroups)
4926 groups = ngroups;
Mingming Caoa02908f2008-08-19 22:16:07 -04004927 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
4928 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
4929
4930 /* bitmaps and block group descriptor blocks */
4931 ret += groups + gdpblocks;
4932
4933 /* Blocks for super block, inode, quota and xattr blocks */
4934 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004935
4936 return ret;
4937}
4938
4939/*
Mingming Caoa02908f2008-08-19 22:16:07 -04004940 * Calulate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04004941 * the modification of a single pages into a single transaction,
4942 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04004943 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004944 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04004945 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004946 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04004947 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04004948 */
4949int ext4_writepage_trans_blocks(struct inode *inode)
4950{
4951 int bpp = ext4_journal_blocks_per_page(inode);
4952 int ret;
4953
4954 ret = ext4_meta_trans_blocks(inode, bpp, 0);
4955
4956 /* Account for data blocks for journalled mode */
4957 if (ext4_should_journal_data(inode))
4958 ret += bpp;
4959 return ret;
4960}
Mingming Caof3bd1f32008-08-19 22:16:03 -04004961
4962/*
4963 * Calculate the journal credits for a chunk of data modification.
4964 *
4965 * This is called from DIO, fallocate or whoever calling
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04004966 * ext4_get_blocks() to map/allocate a chunk of contigous disk blocks.
Mingming Caof3bd1f32008-08-19 22:16:03 -04004967 *
4968 * journal buffers for data blocks are not included here, as DIO
4969 * and fallocate do no need to journal data buffers.
4970 */
4971int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
4972{
4973 return ext4_meta_trans_blocks(inode, nrblocks, 1);
4974}
4975
Mingming Caoa02908f2008-08-19 22:16:07 -04004976/*
Mingming Cao617ba132006-10-11 01:20:53 -07004977 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004978 * Give this, we know that the caller already has write access to iloc->bh.
4979 */
Mingming Cao617ba132006-10-11 01:20:53 -07004980int ext4_mark_iloc_dirty(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004981 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004982{
4983 int err = 0;
4984
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004985 if (test_opt(inode->i_sb, I_VERSION))
4986 inode_inc_iversion(inode);
4987
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004988 /* the do_update_inode consumes one bh->b_count */
4989 get_bh(iloc->bh);
4990
Mingming Caodab291a2006-10-11 01:21:01 -07004991 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Mingming Cao617ba132006-10-11 01:20:53 -07004992 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004993 put_bh(iloc->bh);
4994 return err;
4995}
4996
4997/*
4998 * On success, We end up with an outstanding reference count against
4999 * iloc->bh. This _must_ be cleaned up later.
5000 */
5001
5002int
Mingming Cao617ba132006-10-11 01:20:53 -07005003ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
5004 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005005{
Frank Mayhar03901312009-01-07 00:06:22 -05005006 int err;
5007
5008 err = ext4_get_inode_loc(inode, iloc);
5009 if (!err) {
5010 BUFFER_TRACE(iloc->bh, "get_write_access");
5011 err = ext4_journal_get_write_access(handle, iloc->bh);
5012 if (err) {
5013 brelse(iloc->bh);
5014 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005015 }
5016 }
Mingming Cao617ba132006-10-11 01:20:53 -07005017 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005018 return err;
5019}
5020
5021/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005022 * Expand an inode by new_extra_isize bytes.
5023 * Returns 0 on success or negative error number on failure.
5024 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05005025static int ext4_expand_extra_isize(struct inode *inode,
5026 unsigned int new_extra_isize,
5027 struct ext4_iloc iloc,
5028 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005029{
5030 struct ext4_inode *raw_inode;
5031 struct ext4_xattr_ibody_header *header;
5032 struct ext4_xattr_entry *entry;
5033
5034 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
5035 return 0;
5036
5037 raw_inode = ext4_raw_inode(&iloc);
5038
5039 header = IHDR(inode, raw_inode);
5040 entry = IFIRST(header);
5041
5042 /* No extended attributes present */
5043 if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) ||
5044 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
5045 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
5046 new_extra_isize);
5047 EXT4_I(inode)->i_extra_isize = new_extra_isize;
5048 return 0;
5049 }
5050
5051 /* try to expand with EAs present */
5052 return ext4_expand_extra_isize_ea(inode, new_extra_isize,
5053 raw_inode, handle);
5054}
5055
5056/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005057 * What we do here is to mark the in-core inode as clean with respect to inode
5058 * dirtiness (it may still be data-dirty).
5059 * This means that the in-core inode may be reaped by prune_icache
5060 * without having to perform any I/O. This is a very good thing,
5061 * because *any* task may call prune_icache - even ones which
5062 * have a transaction open against a different journal.
5063 *
5064 * Is this cheating? Not really. Sure, we haven't written the
5065 * inode out, but prune_icache isn't a user-visible syncing function.
5066 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5067 * we start and wait on commits.
5068 *
5069 * Is this efficient/effective? Well, we're being nice to the system
5070 * by cleaning up our inodes proactively so they can be reaped
5071 * without I/O. But we are potentially leaving up to five seconds'
5072 * worth of inodes floating about which prune_icache wants us to
5073 * write out. One way to fix that would be to get prune_icache()
5074 * to do a write_super() to free up some memory. It has the desired
5075 * effect.
5076 */
Mingming Cao617ba132006-10-11 01:20:53 -07005077int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005078{
Mingming Cao617ba132006-10-11 01:20:53 -07005079 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005080 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5081 static unsigned int mnt_count;
5082 int err, ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005083
5084 might_sleep();
Mingming Cao617ba132006-10-11 01:20:53 -07005085 err = ext4_reserve_inode_write(handle, inode, &iloc);
Frank Mayhar03901312009-01-07 00:06:22 -05005086 if (ext4_handle_valid(handle) &&
5087 EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005088 !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) {
5089 /*
5090 * We need extra buffer credits since we may write into EA block
5091 * with this same handle. If journal_extend fails, then it will
5092 * only result in a minor loss of functionality for that inode.
5093 * If this is felt to be critical, then e2fsck should be run to
5094 * force a large enough s_min_extra_isize.
5095 */
5096 if ((jbd2_journal_extend(handle,
5097 EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
5098 ret = ext4_expand_extra_isize(inode,
5099 sbi->s_want_extra_isize,
5100 iloc, handle);
5101 if (ret) {
5102 EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND;
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005103 if (mnt_count !=
5104 le16_to_cpu(sbi->s_es->s_mnt_count)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04005105 ext4_warning(inode->i_sb, __func__,
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005106 "Unable to expand inode %lu. Delete"
5107 " some EAs or run e2fsck.",
5108 inode->i_ino);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005109 mnt_count =
5110 le16_to_cpu(sbi->s_es->s_mnt_count);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005111 }
5112 }
5113 }
5114 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005115 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07005116 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005117 return err;
5118}
5119
5120/*
Mingming Cao617ba132006-10-11 01:20:53 -07005121 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005122 *
5123 * We're really interested in the case where a file is being extended.
5124 * i_size has been changed by generic_commit_write() and we thus need
5125 * to include the updated inode in the current transaction.
5126 *
Jan Karaa269eb12009-01-26 17:04:39 +01005127 * Also, vfs_dq_alloc_block() will always dirty the inode when blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005128 * are allocated to the file.
5129 *
5130 * If the inode is marked synchronous, we don't honour that here - doing
5131 * so would cause a commit on atime updates, which we don't bother doing.
5132 * We handle synchronous inodes at the highest possible level.
5133 */
Mingming Cao617ba132006-10-11 01:20:53 -07005134void ext4_dirty_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005135{
Mingming Cao617ba132006-10-11 01:20:53 -07005136 handle_t *current_handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005137 handle_t *handle;
5138
Frank Mayhar03901312009-01-07 00:06:22 -05005139 if (!ext4_handle_valid(current_handle)) {
5140 ext4_mark_inode_dirty(current_handle, inode);
5141 return;
5142 }
5143
Mingming Cao617ba132006-10-11 01:20:53 -07005144 handle = ext4_journal_start(inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005145 if (IS_ERR(handle))
5146 goto out;
5147 if (current_handle &&
5148 current_handle->h_transaction != handle->h_transaction) {
5149 /* This task has a transaction open against a different fs */
5150 printk(KERN_EMERG "%s: transactions do not match!\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04005151 __func__);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005152 } else {
5153 jbd_debug(5, "marking dirty. outer handle=%p\n",
5154 current_handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005155 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005156 }
Mingming Cao617ba132006-10-11 01:20:53 -07005157 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005158out:
5159 return;
5160}
5161
5162#if 0
5163/*
5164 * Bind an inode's backing buffer_head into this transaction, to prevent
5165 * it from being flushed to disk early. Unlike
Mingming Cao617ba132006-10-11 01:20:53 -07005166 * ext4_reserve_inode_write, this leaves behind no bh reference and
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005167 * returns no iloc structure, so the caller needs to repeat the iloc
5168 * lookup to mark the inode dirty later.
5169 */
Mingming Cao617ba132006-10-11 01:20:53 -07005170static int ext4_pin_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005171{
Mingming Cao617ba132006-10-11 01:20:53 -07005172 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005173
5174 int err = 0;
5175 if (handle) {
Mingming Cao617ba132006-10-11 01:20:53 -07005176 err = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005177 if (!err) {
5178 BUFFER_TRACE(iloc.bh, "get_write_access");
Mingming Caodab291a2006-10-11 01:21:01 -07005179 err = jbd2_journal_get_write_access(handle, iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005180 if (!err)
Frank Mayhar03901312009-01-07 00:06:22 -05005181 err = ext4_handle_dirty_metadata(handle,
5182 inode,
5183 iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005184 brelse(iloc.bh);
5185 }
5186 }
Mingming Cao617ba132006-10-11 01:20:53 -07005187 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005188 return err;
5189}
5190#endif
5191
Mingming Cao617ba132006-10-11 01:20:53 -07005192int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005193{
5194 journal_t *journal;
5195 handle_t *handle;
5196 int err;
5197
5198 /*
5199 * We have to be very careful here: changing a data block's
5200 * journaling status dynamically is dangerous. If we write a
5201 * data block to the journal, change the status and then delete
5202 * that block, we risk forgetting to revoke the old log record
5203 * from the journal and so a subsequent replay can corrupt data.
5204 * So, first we make sure that the journal is empty and that
5205 * nobody is changing anything.
5206 */
5207
Mingming Cao617ba132006-10-11 01:20:53 -07005208 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005209 if (!journal)
5210 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04005211 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005212 return -EROFS;
5213
Mingming Caodab291a2006-10-11 01:21:01 -07005214 jbd2_journal_lock_updates(journal);
5215 jbd2_journal_flush(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005216
5217 /*
5218 * OK, there are no updates running now, and all cached data is
5219 * synced to disk. We are now in a completely consistent state
5220 * which doesn't have anything in the journal, and we know that
5221 * no filesystem updates are running, so it is safe to modify
5222 * the inode's in-core data-journaling state flag now.
5223 */
5224
5225 if (val)
Mingming Cao617ba132006-10-11 01:20:53 -07005226 EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005227 else
Mingming Cao617ba132006-10-11 01:20:53 -07005228 EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL;
5229 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005230
Mingming Caodab291a2006-10-11 01:21:01 -07005231 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005232
5233 /* Finally we can mark the inode as dirty. */
5234
Mingming Cao617ba132006-10-11 01:20:53 -07005235 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005236 if (IS_ERR(handle))
5237 return PTR_ERR(handle);
5238
Mingming Cao617ba132006-10-11 01:20:53 -07005239 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005240 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005241 ext4_journal_stop(handle);
5242 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005243
5244 return err;
5245}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005246
5247static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5248{
5249 return !buffer_mapped(bh);
5250}
5251
Nick Pigginc2ec1752009-03-31 15:23:21 -07005252int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005253{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005254 struct page *page = vmf->page;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005255 loff_t size;
5256 unsigned long len;
5257 int ret = -EINVAL;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005258 void *fsdata;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005259 struct file *file = vma->vm_file;
5260 struct inode *inode = file->f_path.dentry->d_inode;
5261 struct address_space *mapping = inode->i_mapping;
5262
5263 /*
5264 * Get i_alloc_sem to stop truncates messing with the inode. We cannot
5265 * get i_mutex because we are already holding mmap_sem.
5266 */
5267 down_read(&inode->i_alloc_sem);
5268 size = i_size_read(inode);
5269 if (page->mapping != mapping || size <= page_offset(page)
5270 || !PageUptodate(page)) {
5271 /* page got truncated from under us? */
5272 goto out_unlock;
5273 }
5274 ret = 0;
5275 if (PageMappedToDisk(page))
5276 goto out_unlock;
5277
5278 if (page->index == size >> PAGE_CACHE_SHIFT)
5279 len = size & ~PAGE_CACHE_MASK;
5280 else
5281 len = PAGE_CACHE_SIZE;
5282
5283 if (page_has_buffers(page)) {
5284 /* return if we have all the buffers mapped */
5285 if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
5286 ext4_bh_unmapped))
5287 goto out_unlock;
5288 }
5289 /*
5290 * OK, we need to fill the hole... Do write_begin write_end
5291 * to do block allocation/reservation.We are not holding
5292 * inode.i__mutex here. That allow * parallel write_begin,
5293 * write_end call. lock_page prevent this from happening
5294 * on the same page though
5295 */
5296 ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005297 len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005298 if (ret < 0)
5299 goto out_unlock;
5300 ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005301 len, len, page, fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005302 if (ret < 0)
5303 goto out_unlock;
5304 ret = 0;
5305out_unlock:
Nick Pigginc2ec1752009-03-31 15:23:21 -07005306 if (ret)
5307 ret = VM_FAULT_SIGBUS;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005308 up_read(&inode->i_alloc_sem);
5309 return ret;
5310}