blob: deb14a728791aac05cdf9e60010268ae36da05a3 [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
344 if (i_block < 0) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400345 ext4_warning(inode->i_sb, "ext4_block_to_path", "block < 0");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700346 } else if (i_block < direct_blocks) {
347 offsets[n++] = i_block;
348 final = direct_blocks;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400349 } else if ((i_block -= direct_blocks) < indirect_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700350 offsets[n++] = EXT4_IND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700351 offsets[n++] = i_block;
352 final = ptrs;
353 } else if ((i_block -= indirect_blocks) < double_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700354 offsets[n++] = EXT4_DIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700355 offsets[n++] = i_block >> ptrs_bits;
356 offsets[n++] = i_block & (ptrs - 1);
357 final = ptrs;
358 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
Mingming Cao617ba132006-10-11 01:20:53 -0700359 offsets[n++] = EXT4_TIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700360 offsets[n++] = i_block >> (ptrs_bits * 2);
361 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
362 offsets[n++] = i_block & (ptrs - 1);
363 final = ptrs;
364 } else {
Eric Sandeene2b46572008-01-28 23:58:27 -0500365 ext4_warning(inode->i_sb, "ext4_block_to_path",
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400366 "block %lu > max in inode %lu",
367 i_block + direct_blocks +
368 indirect_blocks + double_blocks, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700369 }
370 if (boundary)
371 *boundary = final - 1 - (i_block & (ptrs - 1));
372 return n;
373}
374
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400375static int __ext4_check_blockref(const char *function, struct inode *inode,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400376 __le32 *p, unsigned int max)
377{
Thiemo Nagelf73953c2009-04-07 18:46:47 -0400378 __le32 *bref = p;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400379 unsigned int blk;
380
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400381 while (bref < p+max) {
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400382 blk = le32_to_cpu(*bref++);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400383 if (blk &&
384 unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb),
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400385 blk, 1))) {
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400386 ext4_error(inode->i_sb, function,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400387 "invalid block reference %u "
388 "in inode #%lu", blk, inode->i_ino);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400389 return -EIO;
390 }
391 }
392 return 0;
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400393}
394
395
396#define ext4_check_indirect_blockref(inode, bh) \
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400397 __ext4_check_blockref(__func__, inode, (__le32 *)(bh)->b_data, \
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400398 EXT4_ADDR_PER_BLOCK((inode)->i_sb))
399
400#define ext4_check_inode_blockref(inode) \
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400401 __ext4_check_blockref(__func__, inode, EXT4_I(inode)->i_data, \
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400402 EXT4_NDIR_BLOCKS)
403
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700404/**
Mingming Cao617ba132006-10-11 01:20:53 -0700405 * ext4_get_branch - read the chain of indirect blocks leading to data
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700406 * @inode: inode in question
407 * @depth: depth of the chain (1 - direct pointer, etc.)
408 * @offsets: offsets of pointers in inode/indirect blocks
409 * @chain: place to store the result
410 * @err: here we store the error value
411 *
412 * Function fills the array of triples <key, p, bh> and returns %NULL
413 * if everything went OK or the pointer to the last filled triple
414 * (incomplete one) otherwise. Upon the return chain[i].key contains
415 * the number of (i+1)-th block in the chain (as it is stored in memory,
416 * i.e. little-endian 32-bit), chain[i].p contains the address of that
417 * number (it points into struct inode for i==0 and into the bh->b_data
418 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
419 * block for i>0 and NULL for i==0. In other words, it holds the block
420 * numbers of the chain, addresses they were taken from (and where we can
421 * verify that chain did not change) and buffer_heads hosting these
422 * numbers.
423 *
424 * Function stops when it stumbles upon zero pointer (absent block)
425 * (pointer to last triple returned, *@err == 0)
426 * or when it gets an IO error reading an indirect block
427 * (ditto, *@err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700428 * or when it reads all @depth-1 indirect blocks successfully and finds
429 * the whole chain, all way to the data (returns %NULL, *err == 0).
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500430 *
431 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500432 * down_read(&EXT4_I(inode)->i_data_sem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700433 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500434static Indirect *ext4_get_branch(struct inode *inode, int depth,
435 ext4_lblk_t *offsets,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700436 Indirect chain[4], int *err)
437{
438 struct super_block *sb = inode->i_sb;
439 Indirect *p = chain;
440 struct buffer_head *bh;
441
442 *err = 0;
443 /* i_data is not going away, no lock needed */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400444 add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700445 if (!p->key)
446 goto no_block;
447 while (--depth) {
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400448 bh = sb_getblk(sb, le32_to_cpu(p->key));
449 if (unlikely(!bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700450 goto failure;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400451
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400452 if (!bh_uptodate_or_lock(bh)) {
453 if (bh_submit_read(bh) < 0) {
454 put_bh(bh);
455 goto failure;
456 }
457 /* validate block references */
458 if (ext4_check_indirect_blockref(inode, bh)) {
459 put_bh(bh);
460 goto failure;
461 }
462 }
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400463
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400464 add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700465 /* Reader: end */
466 if (!p->key)
467 goto no_block;
468 }
469 return NULL;
470
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700471failure:
472 *err = -EIO;
473no_block:
474 return p;
475}
476
477/**
Mingming Cao617ba132006-10-11 01:20:53 -0700478 * ext4_find_near - find a place for allocation with sufficient locality
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700479 * @inode: owner
480 * @ind: descriptor of indirect block.
481 *
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000482 * This function returns the preferred place for block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700483 * It is used when heuristic for sequential allocation fails.
484 * Rules are:
485 * + if there is a block to the left of our position - allocate near it.
486 * + if pointer will live in indirect block - allocate near that block.
487 * + if pointer will live in inode - allocate in the same
488 * cylinder group.
489 *
490 * In the latter case we colour the starting block by the callers PID to
491 * prevent it from clashing with concurrent allocations for a different inode
492 * in the same block group. The PID is used here so that functionally related
493 * files will be close-by on-disk.
494 *
495 * Caller must make sure that @ind is valid and will stay that way.
496 */
Mingming Cao617ba132006-10-11 01:20:53 -0700497static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700498{
Mingming Cao617ba132006-10-11 01:20:53 -0700499 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400500 __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700501 __le32 *p;
Mingming Cao617ba132006-10-11 01:20:53 -0700502 ext4_fsblk_t bg_start;
Valerie Clement74d34872008-02-15 13:43:07 -0500503 ext4_fsblk_t last_block;
Mingming Cao617ba132006-10-11 01:20:53 -0700504 ext4_grpblk_t colour;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400505 ext4_group_t block_group;
506 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700507
508 /* Try to find previous block */
509 for (p = ind->p - 1; p >= start; p--) {
510 if (*p)
511 return le32_to_cpu(*p);
512 }
513
514 /* No such thing, so let's try location of indirect block */
515 if (ind->bh)
516 return ind->bh->b_blocknr;
517
518 /*
519 * It is going to be referred to from the inode itself? OK, just put it
520 * into the same cylinder group then.
521 */
Theodore Ts'oa4912122009-03-12 12:18:34 -0400522 block_group = ei->i_block_group;
523 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
524 block_group &= ~(flex_size-1);
525 if (S_ISREG(inode->i_mode))
526 block_group++;
527 }
528 bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
Valerie Clement74d34872008-02-15 13:43:07 -0500529 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
530
Theodore Ts'oa4912122009-03-12 12:18:34 -0400531 /*
532 * If we are doing delayed allocation, we don't need take
533 * colour into account.
534 */
535 if (test_opt(inode->i_sb, DELALLOC))
536 return bg_start;
537
Valerie Clement74d34872008-02-15 13:43:07 -0500538 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
539 colour = (current->pid % 16) *
Mingming Cao617ba132006-10-11 01:20:53 -0700540 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
Valerie Clement74d34872008-02-15 13:43:07 -0500541 else
542 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700543 return bg_start + colour;
544}
545
546/**
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000547 * ext4_find_goal - find a preferred place for allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700548 * @inode: owner
549 * @block: block we want
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700550 * @partial: pointer to the last triple within a chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700551 *
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000552 * Normally this function find the preferred place for block allocation,
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800553 * returns it.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700554 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500555static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400556 Indirect *partial)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700557{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700558 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400559 * XXX need to get goal block from mballoc's data structures
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700560 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700561
Mingming Cao617ba132006-10-11 01:20:53 -0700562 return ext4_find_near(inode, partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563}
564
565/**
Mingming Cao617ba132006-10-11 01:20:53 -0700566 * ext4_blks_to_allocate: Look up the block map and count the number
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700567 * of direct blocks need to be allocated for the given branch.
568 *
569 * @branch: chain of indirect blocks
570 * @k: number of blocks need for indirect blocks
571 * @blks: number of data blocks to be mapped.
572 * @blocks_to_boundary: the offset in the indirect block
573 *
574 * return the total number of blocks to be allocate, including the
575 * direct and indirect blocks.
576 */
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500577static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400578 int blocks_to_boundary)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700579{
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500580 unsigned int count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700581
582 /*
583 * Simple case, [t,d]Indirect block(s) has not allocated yet
584 * then it's clear blocks on that path have not allocated
585 */
586 if (k > 0) {
587 /* right now we don't handle cross boundary allocation */
588 if (blks < blocks_to_boundary + 1)
589 count += blks;
590 else
591 count += blocks_to_boundary + 1;
592 return count;
593 }
594
595 count++;
596 while (count < blks && count <= blocks_to_boundary &&
597 le32_to_cpu(*(branch[0].p + count)) == 0) {
598 count++;
599 }
600 return count;
601}
602
603/**
Mingming Cao617ba132006-10-11 01:20:53 -0700604 * ext4_alloc_blocks: multiple allocate blocks needed for a branch
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700605 * @indirect_blks: the number of blocks need to allocate for indirect
606 * blocks
607 *
608 * @new_blocks: on return it will store the new block numbers for
609 * the indirect blocks(if needed) and the first direct block,
610 * @blks: on return it will store the total number of allocated
611 * direct blocks
612 */
Mingming Cao617ba132006-10-11 01:20:53 -0700613static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400614 ext4_lblk_t iblock, ext4_fsblk_t goal,
615 int indirect_blks, int blks,
616 ext4_fsblk_t new_blocks[4], int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700617{
Theodore Ts'o815a1132009-01-01 23:59:43 -0500618 struct ext4_allocation_request ar;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700619 int target, i;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400620 unsigned long count = 0, blk_allocated = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700621 int index = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700622 ext4_fsblk_t current_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700623 int ret = 0;
624
625 /*
626 * Here we try to allocate the requested multiple blocks at once,
627 * on a best-effort basis.
628 * To build a branch, we should allocate blocks for
629 * the indirect blocks(if not allocated yet), and at least
630 * the first direct block of this branch. That's the
631 * minimum number of blocks need to allocate(required)
632 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400633 /* first we try to allocate the indirect blocks */
634 target = indirect_blks;
635 while (target > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700636 count = target;
637 /* allocating blocks for indirect blocks and direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400638 current_block = ext4_new_meta_blocks(handle, inode,
639 goal, &count, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700640 if (*err)
641 goto failed_out;
642
643 target -= count;
644 /* allocate blocks for indirect blocks */
645 while (index < indirect_blks && count) {
646 new_blocks[index++] = current_block++;
647 count--;
648 }
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400649 if (count > 0) {
650 /*
651 * save the new block number
652 * for the first direct block
653 */
654 new_blocks[index] = current_block;
655 printk(KERN_INFO "%s returned more blocks than "
656 "requested\n", __func__);
657 WARN_ON(1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700658 break;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400659 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700660 }
661
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400662 target = blks - count ;
663 blk_allocated = count;
664 if (!target)
665 goto allocated;
666 /* Now allocate data blocks */
Theodore Ts'o815a1132009-01-01 23:59:43 -0500667 memset(&ar, 0, sizeof(ar));
668 ar.inode = inode;
669 ar.goal = goal;
670 ar.len = target;
671 ar.logical = iblock;
672 if (S_ISREG(inode->i_mode))
673 /* enable in-core preallocation only for regular files */
674 ar.flags = EXT4_MB_HINT_DATA;
675
676 current_block = ext4_mb_new_blocks(handle, &ar, err);
677
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400678 if (*err && (target == blks)) {
679 /*
680 * if the allocation failed and we didn't allocate
681 * any blocks before
682 */
683 goto failed_out;
684 }
685 if (!*err) {
686 if (target == blks) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400687 /*
688 * save the new block number
689 * for the first direct block
690 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400691 new_blocks[index] = current_block;
692 }
Theodore Ts'o815a1132009-01-01 23:59:43 -0500693 blk_allocated += ar.len;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400694 }
695allocated:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700696 /* total number of blocks allocated for direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400697 ret = blk_allocated;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700698 *err = 0;
699 return ret;
700failed_out:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400701 for (i = 0; i < index; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500702 ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700703 return ret;
704}
705
706/**
Mingming Cao617ba132006-10-11 01:20:53 -0700707 * ext4_alloc_branch - allocate and set up a chain of blocks.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700708 * @inode: owner
709 * @indirect_blks: number of allocated indirect blocks
710 * @blks: number of allocated direct blocks
711 * @offsets: offsets (in the blocks) to store the pointers to next.
712 * @branch: place to store the chain in.
713 *
714 * This function allocates blocks, zeroes out all but the last one,
715 * links them into chain and (if we are synchronous) writes them to disk.
716 * In other words, it prepares a branch that can be spliced onto the
717 * inode. It stores the information about that chain in the branch[], in
Mingming Cao617ba132006-10-11 01:20:53 -0700718 * the same format as ext4_get_branch() would do. We are calling it after
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700719 * we had read the existing part of chain and partial points to the last
720 * triple of that (one with zero ->key). Upon the exit we have the same
Mingming Cao617ba132006-10-11 01:20:53 -0700721 * picture as after the successful ext4_get_block(), except that in one
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700722 * place chain is disconnected - *branch->p is still zero (we did not
723 * set the last link), but branch->key contains the number that should
724 * be placed into *branch->p to fill that gap.
725 *
726 * If allocation fails we free all blocks we've allocated (and forget
727 * their buffer_heads) and return the error value the from failed
Mingming Cao617ba132006-10-11 01:20:53 -0700728 * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700729 * as described above and return 0.
730 */
Mingming Cao617ba132006-10-11 01:20:53 -0700731static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400732 ext4_lblk_t iblock, int indirect_blks,
733 int *blks, ext4_fsblk_t goal,
734 ext4_lblk_t *offsets, Indirect *branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700735{
736 int blocksize = inode->i_sb->s_blocksize;
737 int i, n = 0;
738 int err = 0;
739 struct buffer_head *bh;
740 int num;
Mingming Cao617ba132006-10-11 01:20:53 -0700741 ext4_fsblk_t new_blocks[4];
742 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700743
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400744 num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700745 *blks, new_blocks, &err);
746 if (err)
747 return err;
748
749 branch[0].key = cpu_to_le32(new_blocks[0]);
750 /*
751 * metadata blocks and data blocks are allocated.
752 */
753 for (n = 1; n <= indirect_blks; n++) {
754 /*
755 * Get buffer_head for parent block, zero it out
756 * and set the pointer to new one, then send
757 * parent to disk.
758 */
759 bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
760 branch[n].bh = bh;
761 lock_buffer(bh);
762 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700763 err = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700764 if (err) {
Curt Wohlgemuth6487a9d2009-07-17 10:54:08 -0400765 /* Don't brelse(bh) here; it's done in
766 * ext4_journal_forget() below */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700767 unlock_buffer(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700768 goto failed;
769 }
770
771 memset(bh->b_data, 0, blocksize);
772 branch[n].p = (__le32 *) bh->b_data + offsets[n];
773 branch[n].key = cpu_to_le32(new_blocks[n]);
774 *branch[n].p = branch[n].key;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400775 if (n == indirect_blks) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700776 current_block = new_blocks[n];
777 /*
778 * End of chain, update the last new metablock of
779 * the chain to point to the new allocated
780 * data blocks numbers
781 */
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400782 for (i = 1; i < num; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700783 *(branch[n].p + i) = cpu_to_le32(++current_block);
784 }
785 BUFFER_TRACE(bh, "marking uptodate");
786 set_buffer_uptodate(bh);
787 unlock_buffer(bh);
788
Frank Mayhar03901312009-01-07 00:06:22 -0500789 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
790 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700791 if (err)
792 goto failed;
793 }
794 *blks = num;
795 return err;
796failed:
797 /* Allocation failed, free what we already allocated */
798 for (i = 1; i <= n ; i++) {
Mingming Caodab291a2006-10-11 01:21:01 -0700799 BUFFER_TRACE(branch[i].bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700800 ext4_journal_forget(handle, branch[i].bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700801 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400802 for (i = 0; i < indirect_blks; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500803 ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700804
Alex Tomasc9de5602008-01-29 00:19:52 -0500805 ext4_free_blocks(handle, inode, new_blocks[i], num, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700806
807 return err;
808}
809
810/**
Mingming Cao617ba132006-10-11 01:20:53 -0700811 * ext4_splice_branch - splice the allocated branch onto inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700812 * @inode: owner
813 * @block: (logical) number of block we are adding
814 * @chain: chain of indirect blocks (with a missing link - see
Mingming Cao617ba132006-10-11 01:20:53 -0700815 * ext4_alloc_branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700816 * @where: location of missing link
817 * @num: number of indirect blocks we are adding
818 * @blks: number of direct blocks we are adding
819 *
820 * This function fills the missing link and does all housekeeping needed in
821 * inode (->i_blocks, etc.). In case of success we end up with the full
822 * chain to new block and return 0.
823 */
Mingming Cao617ba132006-10-11 01:20:53 -0700824static int ext4_splice_branch(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400825 ext4_lblk_t block, Indirect *where, int num,
826 int blks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700827{
828 int i;
829 int err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700830 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700831
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700832 /*
833 * If we're splicing into a [td]indirect block (as opposed to the
834 * inode) then we need to get write access to the [td]indirect block
835 * before the splice.
836 */
837 if (where->bh) {
838 BUFFER_TRACE(where->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700839 err = ext4_journal_get_write_access(handle, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700840 if (err)
841 goto err_out;
842 }
843 /* That's it */
844
845 *where->p = where->key;
846
847 /*
848 * Update the host buffer_head or inode to point to more just allocated
849 * direct blocks blocks
850 */
851 if (num == 0 && blks > 1) {
852 current_block = le32_to_cpu(where->key) + 1;
853 for (i = 1; i < blks; i++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400854 *(where->p + i) = cpu_to_le32(current_block++);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700855 }
856
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700857 /* We are done with atomic stuff, now do the rest of housekeeping */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700858 /* had we spliced it onto indirect block? */
859 if (where->bh) {
860 /*
861 * If we spliced it onto an indirect block, we haven't
862 * altered the inode. Note however that if it is being spliced
863 * onto an indirect block at the very end of the file (the
864 * file is growing) then we *will* alter the inode to reflect
865 * the new i_size. But that is not done here - it is done in
Mingming Cao617ba132006-10-11 01:20:53 -0700866 * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700867 */
868 jbd_debug(5, "splicing indirect only\n");
Frank Mayhar03901312009-01-07 00:06:22 -0500869 BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata");
870 err = ext4_handle_dirty_metadata(handle, inode, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700871 if (err)
872 goto err_out;
873 } else {
874 /*
875 * OK, we spliced it into the inode itself on a direct block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700876 */
Theodore Ts'o41591752009-06-15 03:41:23 -0400877 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700878 jbd_debug(5, "splicing direct\n");
879 }
880 return err;
881
882err_out:
883 for (i = 1; i <= num; i++) {
Mingming Caodab291a2006-10-11 01:21:01 -0700884 BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700885 ext4_journal_forget(handle, where[i].bh);
Alex Tomasc9de5602008-01-29 00:19:52 -0500886 ext4_free_blocks(handle, inode,
887 le32_to_cpu(where[i-1].key), 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700888 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500889 ext4_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700890
891 return err;
892}
893
894/*
Theodore Ts'ob920c752009-05-14 00:54:29 -0400895 * The ext4_ind_get_blocks() function handles non-extents inodes
896 * (i.e., using the traditional indirect/double-indirect i_blocks
897 * scheme) for ext4_get_blocks().
898 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700899 * Allocation strategy is simple: if we have to allocate something, we will
900 * have to go the whole way to leaf. So let's do it before attaching anything
901 * to tree, set linkage between the newborn blocks, write them if sync is
902 * required, recheck the path, free and repeat if check fails, otherwise
903 * set the last missing link (that will protect us from any truncate-generated
904 * removals - all blocks on the path are immune now) and possibly force the
905 * write on the parent block.
906 * That has a nice additional property: no special recovery from the failed
907 * allocations is needed - we simply release blocks and do not touch anything
908 * reachable from inode.
909 *
910 * `handle' can be NULL if create == 0.
911 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700912 * return > 0, # of blocks mapped or allocated.
913 * return = 0, if plain lookup failed.
914 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500915 *
Theodore Ts'ob920c752009-05-14 00:54:29 -0400916 * The ext4_ind_get_blocks() function should be called with
917 * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem
918 * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or
919 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system
920 * blocks.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700921 */
Theodore Ts'oe4d996c2009-05-12 00:25:28 -0400922static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400923 ext4_lblk_t iblock, unsigned int maxblocks,
924 struct buffer_head *bh_result,
925 int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700926{
927 int err = -EIO;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500928 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700929 Indirect chain[4];
930 Indirect *partial;
Mingming Cao617ba132006-10-11 01:20:53 -0700931 ext4_fsblk_t goal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700932 int indirect_blks;
933 int blocks_to_boundary = 0;
934 int depth;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700935 int count = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700936 ext4_fsblk_t first_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700937
Alex Tomasa86c6182006-10-11 01:21:03 -0700938 J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL));
Theodore Ts'oc2177052009-05-14 00:58:52 -0400939 J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500940 depth = ext4_block_to_path(inode, iblock, offsets,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400941 &blocks_to_boundary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700942
943 if (depth == 0)
944 goto out;
945
Mingming Cao617ba132006-10-11 01:20:53 -0700946 partial = ext4_get_branch(inode, depth, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700947
948 /* Simplest case - block found, no allocation needed */
949 if (!partial) {
950 first_block = le32_to_cpu(chain[depth - 1].key);
951 clear_buffer_new(bh_result);
952 count++;
953 /*map more blocks*/
954 while (count < maxblocks && count <= blocks_to_boundary) {
Mingming Cao617ba132006-10-11 01:20:53 -0700955 ext4_fsblk_t blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700956
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700957 blk = le32_to_cpu(*(chain[depth-1].p + count));
958
959 if (blk == first_block + count)
960 count++;
961 else
962 break;
963 }
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500964 goto got_it;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700965 }
966
967 /* Next simple case - plain lookup or failed read of indirect block */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400968 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700969 goto cleanup;
970
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700971 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400972 * Okay, we need to do block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700973 */
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800974 goal = ext4_find_goal(inode, iblock, partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700975
976 /* the number of blocks need to allocate for [d,t]indirect blocks */
977 indirect_blks = (chain + depth) - partial - 1;
978
979 /*
980 * Next look up the indirect map to count the totoal number of
981 * direct blocks to allocate for this branch.
982 */
Mingming Cao617ba132006-10-11 01:20:53 -0700983 count = ext4_blks_to_allocate(partial, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700984 maxblocks, blocks_to_boundary);
985 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700986 * Block out ext4_truncate while we alter the tree
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700987 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400988 err = ext4_alloc_branch(handle, inode, iblock, indirect_blks,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400989 &count, goal,
990 offsets + (partial - chain), partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700991
992 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700993 * The ext4_splice_branch call will free and forget any buffers
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700994 * on the new chain if there is a failure, but that risks using
995 * up transaction credits, especially for bitmaps where the
996 * credits cannot be returned. Can we handle this somehow? We
997 * may need to return -EAGAIN upwards in the worst case. --sct
998 */
999 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07001000 err = ext4_splice_branch(handle, inode, iblock,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001001 partial, indirect_blks, count);
1002 else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001003 goto cleanup;
1004
1005 set_buffer_new(bh_result);
1006got_it:
1007 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
1008 if (count > blocks_to_boundary)
1009 set_buffer_boundary(bh_result);
1010 err = count;
1011 /* Clean up and exit */
1012 partial = chain + depth - 1; /* the whole chain */
1013cleanup:
1014 while (partial > chain) {
1015 BUFFER_TRACE(partial->bh, "call brelse");
1016 brelse(partial->bh);
1017 partial--;
1018 }
1019 BUFFER_TRACE(bh_result, "returned");
1020out:
1021 return err;
1022}
1023
Mingming Cao60e58e02009-01-22 18:13:05 +01001024qsize_t ext4_get_reserved_space(struct inode *inode)
1025{
1026 unsigned long long total;
1027
1028 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1029 total = EXT4_I(inode)->i_reserved_data_blocks +
1030 EXT4_I(inode)->i_reserved_meta_blocks;
1031 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1032
1033 return total;
1034}
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001035/*
1036 * Calculate the number of metadata blocks need to reserve
1037 * to allocate @blocks for non extent file based file
1038 */
1039static int ext4_indirect_calc_metadata_amount(struct inode *inode, int blocks)
1040{
1041 int icap = EXT4_ADDR_PER_BLOCK(inode->i_sb);
1042 int ind_blks, dind_blks, tind_blks;
1043
1044 /* number of new indirect blocks needed */
1045 ind_blks = (blocks + icap - 1) / icap;
1046
1047 dind_blks = (ind_blks + icap - 1) / icap;
1048
1049 tind_blks = 1;
1050
1051 return ind_blks + dind_blks + tind_blks;
1052}
1053
1054/*
1055 * Calculate the number of metadata blocks need to reserve
1056 * to allocate given number of blocks
1057 */
1058static int ext4_calc_metadata_amount(struct inode *inode, int blocks)
1059{
Mingming Caocd213222008-08-19 22:16:59 -04001060 if (!blocks)
1061 return 0;
1062
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001063 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)
1064 return ext4_ext_calc_metadata_amount(inode, blocks);
1065
1066 return ext4_indirect_calc_metadata_amount(inode, blocks);
1067}
1068
1069static void ext4_da_update_reserve_space(struct inode *inode, int used)
1070{
1071 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1072 int total, mdb, mdb_free;
1073
1074 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1075 /* recalculate the number of metablocks still need to be reserved */
1076 total = EXT4_I(inode)->i_reserved_data_blocks - used;
1077 mdb = ext4_calc_metadata_amount(inode, total);
1078
1079 /* figure out how many metablocks to release */
1080 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1081 mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
1082
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001083 if (mdb_free) {
1084 /* Account for allocated meta_blocks */
1085 mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001086
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001087 /* update fs dirty blocks counter */
1088 percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free);
1089 EXT4_I(inode)->i_allocated_meta_blocks = 0;
1090 EXT4_I(inode)->i_reserved_meta_blocks = mdb;
1091 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001092
1093 /* update per-inode reservations */
1094 BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks);
1095 EXT4_I(inode)->i_reserved_data_blocks -= used;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001096 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001097
1098 /*
1099 * free those over-booking quota for metadata blocks
1100 */
Mingming Cao60e58e02009-01-22 18:13:05 +01001101 if (mdb_free)
1102 vfs_dq_release_reservation_block(inode, mdb_free);
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -04001103
1104 /*
1105 * If we have done all the pending block allocations and if
1106 * there aren't any writers on the inode, we can discard the
1107 * inode's preallocations.
1108 */
1109 if (!total && (atomic_read(&inode->i_writecount) == 0))
1110 ext4_discard_preallocations(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001111}
1112
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001113static int check_block_validity(struct inode *inode, sector_t logical,
1114 sector_t phys, int len)
1115{
1116 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), phys, len)) {
1117 ext4_error(inode->i_sb, "check_block_validity",
1118 "inode #%lu logical block %llu mapped to %llu "
1119 "(size %d)", inode->i_ino,
1120 (unsigned long long) logical,
1121 (unsigned long long) phys, len);
1122 WARN_ON(1);
1123 return -EIO;
1124 }
1125 return 0;
1126}
1127
Mingming Caof5ab0d12008-02-25 15:29:55 -05001128/*
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001129 * The ext4_get_blocks() function tries to look up the requested blocks,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001130 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -05001131 *
Mingming Caof5ab0d12008-02-25 15:29:55 -05001132 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
1133 * and store the allocated blocks in the result buffer head and mark it
1134 * mapped.
1135 *
1136 * If file type is extents based, it will call ext4_ext_get_blocks(),
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001137 * Otherwise, call with ext4_ind_get_blocks() to handle indirect mapping
Mingming Caof5ab0d12008-02-25 15:29:55 -05001138 * based files
1139 *
1140 * On success, it returns the number of blocks being mapped or allocate.
1141 * if create==0 and the blocks are pre-allocated and uninitialized block,
1142 * the result buffer head is unmapped. If the create ==1, it will make sure
1143 * the buffer head is mapped.
1144 *
1145 * It returns 0 if plain look up failed (blocks have not been allocated), in
1146 * that casem, buffer head is unmapped
1147 *
1148 * It returns the error in case of allocation failure.
1149 */
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001150int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block,
1151 unsigned int max_blocks, struct buffer_head *bh,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001152 int flags)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001153{
1154 int retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -05001155
1156 clear_buffer_mapped(bh);
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -04001157 clear_buffer_unwritten(bh);
Mingming Caof5ab0d12008-02-25 15:29:55 -05001158
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001159 /*
Theodore Ts'ob920c752009-05-14 00:54:29 -04001160 * Try to see if we can get the block without requesting a new
1161 * file system block.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001162 */
1163 down_read((&EXT4_I(inode)->i_data_sem));
1164 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1165 retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001166 bh, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001167 } else {
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001168 retval = ext4_ind_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001169 bh, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001170 }
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001171 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -05001172
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001173 if (retval > 0 && buffer_mapped(bh)) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001174 int ret = check_block_validity(inode, block,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001175 bh->b_blocknr, retval);
1176 if (ret != 0)
1177 return ret;
1178 }
1179
Mingming Caof5ab0d12008-02-25 15:29:55 -05001180 /* If it is only a block(s) look up */
Theodore Ts'oc2177052009-05-14 00:58:52 -04001181 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001182 return retval;
1183
1184 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001185 * Returns if the blocks have already allocated
1186 *
1187 * Note that if blocks have been preallocated
1188 * ext4_ext_get_block() returns th create = 0
1189 * with buffer head unmapped.
1190 */
1191 if (retval > 0 && buffer_mapped(bh))
1192 return retval;
1193
1194 /*
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -04001195 * When we call get_blocks without the create flag, the
1196 * BH_Unwritten flag could have gotten set if the blocks
1197 * requested were part of a uninitialized extent. We need to
1198 * clear this flag now that we are committed to convert all or
1199 * part of the uninitialized extent to be an initialized
1200 * extent. This is because we need to avoid the combination
1201 * of BH_Unwritten and BH_Mapped flags being simultaneously
1202 * set on the buffer_head.
1203 */
1204 clear_buffer_unwritten(bh);
1205
1206 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001207 * New blocks allocate and/or writing to uninitialized extent
1208 * will possibly result in updating i_data, so we take
1209 * the write lock of i_data_sem, and call get_blocks()
1210 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001211 */
1212 down_write((&EXT4_I(inode)->i_data_sem));
Mingming Caod2a17632008-07-14 17:52:37 -04001213
1214 /*
1215 * if the caller is from delayed allocation writeout path
1216 * we have already reserved fs blocks for allocation
1217 * let the underlying get_block() function know to
1218 * avoid double accounting
1219 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04001220 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Mingming Caod2a17632008-07-14 17:52:37 -04001221 EXT4_I(inode)->i_delalloc_reserved_flag = 1;
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001222 /*
1223 * We need to check for EXT4 here because migrate
1224 * could have changed the inode type in between
1225 */
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001226 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1227 retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001228 bh, flags);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001229 } else {
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001230 retval = ext4_ind_get_blocks(handle, inode, block,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001231 max_blocks, bh, flags);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04001232
1233 if (retval > 0 && buffer_new(bh)) {
1234 /*
1235 * We allocated new blocks which will result in
1236 * i_data's format changing. Force the migrate
1237 * to fail by clearing migrate flags
1238 */
1239 EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags &
1240 ~EXT4_EXT_MIGRATE;
1241 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001242 }
Mingming Caod2a17632008-07-14 17:52:37 -04001243
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001244 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Mingming Caod2a17632008-07-14 17:52:37 -04001245 EXT4_I(inode)->i_delalloc_reserved_flag = 0;
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001246
1247 /*
1248 * Update reserved blocks/metadata blocks after successful
1249 * block allocation which had been deferred till now.
1250 */
1251 if ((retval > 0) && (flags & EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE))
1252 ext4_da_update_reserve_space(inode, retval);
Mingming Caod2a17632008-07-14 17:52:37 -04001253
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001254 up_write((&EXT4_I(inode)->i_data_sem));
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001255 if (retval > 0 && buffer_mapped(bh)) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001256 int ret = check_block_validity(inode, block,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001257 bh->b_blocknr, retval);
1258 if (ret != 0)
1259 return ret;
1260 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001261 return retval;
1262}
1263
Mingming Caof3bd1f32008-08-19 22:16:03 -04001264/* Maximum number of blocks we map for direct IO at once. */
1265#define DIO_MAX_BLOCKS 4096
1266
Eric Sandeen6873fa02008-10-07 00:46:36 -04001267int ext4_get_block(struct inode *inode, sector_t iblock,
1268 struct buffer_head *bh_result, int create)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001269{
Dmitriy Monakhov3e4fdaf2007-02-10 01:46:35 -08001270 handle_t *handle = ext4_journal_current_handle();
Jan Kara7fb54092008-02-10 01:08:38 -05001271 int ret = 0, started = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001272 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001273 int dio_credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001274
Jan Kara7fb54092008-02-10 01:08:38 -05001275 if (create && !handle) {
1276 /* Direct IO write... */
1277 if (max_blocks > DIO_MAX_BLOCKS)
1278 max_blocks = DIO_MAX_BLOCKS;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001279 dio_credits = ext4_chunk_trans_blocks(inode, max_blocks);
1280 handle = ext4_journal_start(inode, dio_credits);
Jan Kara7fb54092008-02-10 01:08:38 -05001281 if (IS_ERR(handle)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001282 ret = PTR_ERR(handle);
Jan Kara7fb54092008-02-10 01:08:38 -05001283 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001284 }
Jan Kara7fb54092008-02-10 01:08:38 -05001285 started = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001286 }
1287
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001288 ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001289 create ? EXT4_GET_BLOCKS_CREATE : 0);
Jan Kara7fb54092008-02-10 01:08:38 -05001290 if (ret > 0) {
1291 bh_result->b_size = (ret << inode->i_blkbits);
1292 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001293 }
Jan Kara7fb54092008-02-10 01:08:38 -05001294 if (started)
1295 ext4_journal_stop(handle);
1296out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001297 return ret;
1298}
1299
1300/*
1301 * `handle' can be NULL if create is zero
1302 */
Mingming Cao617ba132006-10-11 01:20:53 -07001303struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001304 ext4_lblk_t block, int create, int *errp)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001305{
1306 struct buffer_head dummy;
1307 int fatal = 0, err;
Jan Kara03f5d8b2009-06-09 00:17:05 -04001308 int flags = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001309
1310 J_ASSERT(handle != NULL || create == 0);
1311
1312 dummy.b_state = 0;
1313 dummy.b_blocknr = -1000;
1314 buffer_trace_init(&dummy.b_history);
Theodore Ts'oc2177052009-05-14 00:58:52 -04001315 if (create)
1316 flags |= EXT4_GET_BLOCKS_CREATE;
1317 err = ext4_get_blocks(handle, inode, block, 1, &dummy, flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001318 /*
Theodore Ts'oc2177052009-05-14 00:58:52 -04001319 * ext4_get_blocks() returns number of blocks mapped. 0 in
1320 * case of a HOLE.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001321 */
1322 if (err > 0) {
1323 if (err > 1)
1324 WARN_ON(1);
1325 err = 0;
1326 }
1327 *errp = err;
1328 if (!err && buffer_mapped(&dummy)) {
1329 struct buffer_head *bh;
1330 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
1331 if (!bh) {
1332 *errp = -EIO;
1333 goto err;
1334 }
1335 if (buffer_new(&dummy)) {
1336 J_ASSERT(create != 0);
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001337 J_ASSERT(handle != NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001338
1339 /*
1340 * Now that we do not always journal data, we should
1341 * keep in mind whether this should always journal the
1342 * new buffer as metadata. For now, regular file
Mingming Cao617ba132006-10-11 01:20:53 -07001343 * writes use ext4_get_block instead, so it's not a
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001344 * problem.
1345 */
1346 lock_buffer(bh);
1347 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001348 fatal = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001349 if (!fatal && !buffer_uptodate(bh)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001350 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001351 set_buffer_uptodate(bh);
1352 }
1353 unlock_buffer(bh);
Frank Mayhar03901312009-01-07 00:06:22 -05001354 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
1355 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001356 if (!fatal)
1357 fatal = err;
1358 } else {
1359 BUFFER_TRACE(bh, "not a new buffer");
1360 }
1361 if (fatal) {
1362 *errp = fatal;
1363 brelse(bh);
1364 bh = NULL;
1365 }
1366 return bh;
1367 }
1368err:
1369 return NULL;
1370}
1371
Mingming Cao617ba132006-10-11 01:20:53 -07001372struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001373 ext4_lblk_t block, int create, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001374{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001375 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001376
Mingming Cao617ba132006-10-11 01:20:53 -07001377 bh = ext4_getblk(handle, inode, block, create, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001378 if (!bh)
1379 return bh;
1380 if (buffer_uptodate(bh))
1381 return bh;
1382 ll_rw_block(READ_META, 1, &bh);
1383 wait_on_buffer(bh);
1384 if (buffer_uptodate(bh))
1385 return bh;
1386 put_bh(bh);
1387 *err = -EIO;
1388 return NULL;
1389}
1390
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001391static int walk_page_buffers(handle_t *handle,
1392 struct buffer_head *head,
1393 unsigned from,
1394 unsigned to,
1395 int *partial,
1396 int (*fn)(handle_t *handle,
1397 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001398{
1399 struct buffer_head *bh;
1400 unsigned block_start, block_end;
1401 unsigned blocksize = head->b_size;
1402 int err, ret = 0;
1403 struct buffer_head *next;
1404
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001405 for (bh = head, block_start = 0;
1406 ret == 0 && (bh != head || !block_start);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001407 block_start = block_end, bh = next) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001408 next = bh->b_this_page;
1409 block_end = block_start + blocksize;
1410 if (block_end <= from || block_start >= to) {
1411 if (partial && !buffer_uptodate(bh))
1412 *partial = 1;
1413 continue;
1414 }
1415 err = (*fn)(handle, bh);
1416 if (!ret)
1417 ret = err;
1418 }
1419 return ret;
1420}
1421
1422/*
1423 * To preserve ordering, it is essential that the hole instantiation and
1424 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -07001425 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -07001426 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001427 * prepare_write() is the right place.
1428 *
Mingming Cao617ba132006-10-11 01:20:53 -07001429 * Also, this function can nest inside ext4_writepage() ->
1430 * block_write_full_page(). In that case, we *know* that ext4_writepage()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001431 * has generated enough buffer credits to do the whole page. So we won't
1432 * block on the journal in that case, which is good, because the caller may
1433 * be PF_MEMALLOC.
1434 *
Mingming Cao617ba132006-10-11 01:20:53 -07001435 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001436 * quota file writes. If we were to commit the transaction while thus
1437 * reentered, there can be a deadlock - we would be holding a quota
1438 * lock, and the commit would never complete if another thread had a
1439 * transaction open and was blocking on the quota lock - a ranking
1440 * violation.
1441 *
Mingming Caodab291a2006-10-11 01:21:01 -07001442 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001443 * will _not_ run commit under these circumstances because handle->h_ref
1444 * is elevated. We'll still have enough credits for the tiny quotafile
1445 * write.
1446 */
1447static int do_journal_get_write_access(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001448 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001449{
1450 if (!buffer_mapped(bh) || buffer_freed(bh))
1451 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001452 return ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001453}
1454
Nick Pigginbfc1af62007-10-16 01:25:05 -07001455static int ext4_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001456 loff_t pos, unsigned len, unsigned flags,
1457 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001458{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001459 struct inode *inode = mapping->host;
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001460 int ret, needed_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001461 handle_t *handle;
1462 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001463 struct page *page;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001464 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001465 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001466
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001467 trace_ext4_write_begin(inode, pos, len, flags);
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001468 /*
1469 * Reserve one block more for addition to orphan list in case
1470 * we allocate blocks but write fails for some reason
1471 */
1472 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001473 index = pos >> PAGE_CACHE_SHIFT;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001474 from = pos & (PAGE_CACHE_SIZE - 1);
1475 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001476
1477retry:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001478 handle = ext4_journal_start(inode, needed_blocks);
1479 if (IS_ERR(handle)) {
1480 ret = PTR_ERR(handle);
1481 goto out;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001482 }
1483
Jan Karaebd36102009-02-22 21:09:59 -05001484 /* We cannot recurse into the filesystem as the transaction is already
1485 * started */
1486 flags |= AOP_FLAG_NOFS;
1487
Nick Piggin54566b22009-01-04 12:00:53 -08001488 page = grab_cache_page_write_begin(mapping, index, flags);
Jan Karacf108bc2008-07-11 19:27:31 -04001489 if (!page) {
1490 ext4_journal_stop(handle);
1491 ret = -ENOMEM;
1492 goto out;
1493 }
1494 *pagep = page;
1495
Nick Pigginbfc1af62007-10-16 01:25:05 -07001496 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
Jan Karaebd36102009-02-22 21:09:59 -05001497 ext4_get_block);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001498
1499 if (!ret && ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001500 ret = walk_page_buffers(handle, page_buffers(page),
1501 from, to, NULL, do_journal_get_write_access);
1502 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001503
1504 if (ret) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001505 unlock_page(page);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001506 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001507 /*
1508 * block_write_begin may have instantiated a few blocks
1509 * outside i_size. Trim these off again. Don't need
1510 * i_size_read because we hold i_mutex.
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001511 *
1512 * Add inode to orphan list in case we crash before
1513 * truncate finishes
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001514 */
Jan Karaffacfa72009-07-13 16:22:22 -04001515 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001516 ext4_orphan_add(handle, inode);
1517
1518 ext4_journal_stop(handle);
1519 if (pos + len > inode->i_size) {
Jan Karaffacfa72009-07-13 16:22:22 -04001520 ext4_truncate(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001521 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001522 * If truncate failed early the inode might
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001523 * still be on the orphan list; we need to
1524 * make sure the inode is removed from the
1525 * orphan list in that case.
1526 */
1527 if (inode->i_nlink)
1528 ext4_orphan_del(NULL, inode);
1529 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001530 }
1531
Mingming Cao617ba132006-10-11 01:20:53 -07001532 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001533 goto retry;
Andrew Morton7479d2b2007-04-01 23:49:44 -07001534out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001535 return ret;
1536}
1537
Nick Pigginbfc1af62007-10-16 01:25:05 -07001538/* For write_end() in data=journal mode */
1539static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001540{
1541 if (!buffer_mapped(bh) || buffer_freed(bh))
1542 return 0;
1543 set_buffer_uptodate(bh);
Frank Mayhar03901312009-01-07 00:06:22 -05001544 return ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001545}
1546
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001547static int ext4_generic_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001548 struct address_space *mapping,
1549 loff_t pos, unsigned len, unsigned copied,
1550 struct page *page, void *fsdata)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001551{
1552 int i_size_changed = 0;
1553 struct inode *inode = mapping->host;
1554 handle_t *handle = ext4_journal_current_handle();
1555
1556 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
1557
1558 /*
1559 * No need to use i_size_read() here, the i_size
1560 * cannot change under us because we hold i_mutex.
1561 *
1562 * But it's important to update i_size while still holding page lock:
1563 * page writeout could otherwise come in and zero beyond i_size.
1564 */
1565 if (pos + copied > inode->i_size) {
1566 i_size_write(inode, pos + copied);
1567 i_size_changed = 1;
1568 }
1569
1570 if (pos + copied > EXT4_I(inode)->i_disksize) {
1571 /* We need to mark inode dirty even if
1572 * new_i_size is less that inode->i_size
1573 * bu greater than i_disksize.(hint delalloc)
1574 */
1575 ext4_update_i_disksize(inode, (pos + copied));
1576 i_size_changed = 1;
1577 }
1578 unlock_page(page);
1579 page_cache_release(page);
1580
1581 /*
1582 * Don't mark the inode dirty under page lock. First, it unnecessarily
1583 * makes the holding time of page lock longer. Second, it forces lock
1584 * ordering of page lock and transaction start for journaling
1585 * filesystems.
1586 */
1587 if (i_size_changed)
1588 ext4_mark_inode_dirty(handle, inode);
1589
1590 return copied;
1591}
1592
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001593/*
1594 * We need to pick up the new inode size which generic_commit_write gave us
1595 * `file' can be NULL - eg, when called from page_symlink().
1596 *
Mingming Cao617ba132006-10-11 01:20:53 -07001597 * ext4 never places buffers on inode->i_mapping->private_list. metadata
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001598 * buffers are managed internally.
1599 */
Nick Pigginbfc1af62007-10-16 01:25:05 -07001600static int ext4_ordered_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001601 struct address_space *mapping,
1602 loff_t pos, unsigned len, unsigned copied,
1603 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001604{
Mingming Cao617ba132006-10-11 01:20:53 -07001605 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001606 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001607 int ret = 0, ret2;
1608
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001609 trace_ext4_ordered_write_end(inode, pos, len, copied);
Jan Kara678aaf42008-07-11 19:27:31 -04001610 ret = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001611
1612 if (ret == 0) {
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001613 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001614 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001615 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -04001616 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001617 /* if we have allocated more blocks and copied
1618 * less. We will have blocks allocated outside
1619 * inode->i_size. So truncate them
1620 */
1621 ext4_orphan_add(handle, inode);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001622 if (ret2 < 0)
1623 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001624 }
Mingming Cao617ba132006-10-11 01:20:53 -07001625 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001626 if (!ret)
1627 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001628
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001629 if (pos + len > inode->i_size) {
Jan Karaffacfa72009-07-13 16:22:22 -04001630 ext4_truncate(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001631 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001632 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001633 * on the orphan list; we need to make sure the inode
1634 * is removed from the orphan list in that case.
1635 */
1636 if (inode->i_nlink)
1637 ext4_orphan_del(NULL, inode);
1638 }
1639
1640
Nick Pigginbfc1af62007-10-16 01:25:05 -07001641 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001642}
1643
Nick Pigginbfc1af62007-10-16 01:25:05 -07001644static int ext4_writeback_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001645 struct address_space *mapping,
1646 loff_t pos, unsigned len, unsigned copied,
1647 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001648{
Mingming Cao617ba132006-10-11 01:20:53 -07001649 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001650 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001651 int ret = 0, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001652
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001653 trace_ext4_writeback_write_end(inode, pos, len, copied);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001654 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001655 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001656 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -04001657 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001658 /* if we have allocated more blocks and copied
1659 * less. We will have blocks allocated outside
1660 * inode->i_size. So truncate them
1661 */
1662 ext4_orphan_add(handle, inode);
1663
Roel Kluinf8a87d82008-04-29 22:01:18 -04001664 if (ret2 < 0)
1665 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001666
Mingming Cao617ba132006-10-11 01:20:53 -07001667 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001668 if (!ret)
1669 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001670
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001671 if (pos + len > inode->i_size) {
Jan Karaffacfa72009-07-13 16:22:22 -04001672 ext4_truncate(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001673 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001674 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001675 * on the orphan list; we need to make sure the inode
1676 * is removed from the orphan list in that case.
1677 */
1678 if (inode->i_nlink)
1679 ext4_orphan_del(NULL, inode);
1680 }
1681
Nick Pigginbfc1af62007-10-16 01:25:05 -07001682 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001683}
1684
Nick Pigginbfc1af62007-10-16 01:25:05 -07001685static int ext4_journalled_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001686 struct address_space *mapping,
1687 loff_t pos, unsigned len, unsigned copied,
1688 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001689{
Mingming Cao617ba132006-10-11 01:20:53 -07001690 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001691 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001692 int ret = 0, ret2;
1693 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001694 unsigned from, to;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001695 loff_t new_i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001696
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001697 trace_ext4_journalled_write_end(inode, pos, len, copied);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001698 from = pos & (PAGE_CACHE_SIZE - 1);
1699 to = from + len;
1700
1701 if (copied < len) {
1702 if (!PageUptodate(page))
1703 copied = 0;
1704 page_zero_new_buffers(page, from+copied, to);
1705 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001706
1707 ret = walk_page_buffers(handle, page_buffers(page), from,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001708 to, &partial, write_end_fn);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001709 if (!partial)
1710 SetPageUptodate(page);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001711 new_i_size = pos + copied;
1712 if (new_i_size > inode->i_size)
Nick Pigginbfc1af62007-10-16 01:25:05 -07001713 i_size_write(inode, pos+copied);
Mingming Cao617ba132006-10-11 01:20:53 -07001714 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001715 if (new_i_size > EXT4_I(inode)->i_disksize) {
1716 ext4_update_i_disksize(inode, new_i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001717 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001718 if (!ret)
1719 ret = ret2;
1720 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001721
Jan Karacf108bc2008-07-11 19:27:31 -04001722 unlock_page(page);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001723 page_cache_release(page);
Jan Karaffacfa72009-07-13 16:22:22 -04001724 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001725 /* if we have allocated more blocks and copied
1726 * less. We will have blocks allocated outside
1727 * inode->i_size. So truncate them
1728 */
1729 ext4_orphan_add(handle, inode);
1730
Mingming Cao617ba132006-10-11 01:20:53 -07001731 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001732 if (!ret)
1733 ret = ret2;
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001734 if (pos + len > inode->i_size) {
Jan Karaffacfa72009-07-13 16:22:22 -04001735 ext4_truncate(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001736 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001737 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001738 * on the orphan list; we need to make sure the inode
1739 * is removed from the orphan list in that case.
1740 */
1741 if (inode->i_nlink)
1742 ext4_orphan_del(NULL, inode);
1743 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001744
1745 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001746}
Mingming Caod2a17632008-07-14 17:52:37 -04001747
1748static int ext4_da_reserve_space(struct inode *inode, int nrblocks)
1749{
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001750 int retries = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01001751 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1752 unsigned long md_needed, mdblocks, total = 0;
Mingming Caod2a17632008-07-14 17:52:37 -04001753
1754 /*
1755 * recalculate the amount of metadata blocks to reserve
1756 * in order to allocate nrblocks
1757 * worse case is one extent per block
1758 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001759repeat:
Mingming Caod2a17632008-07-14 17:52:37 -04001760 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1761 total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks;
1762 mdblocks = ext4_calc_metadata_amount(inode, total);
1763 BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks);
1764
1765 md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks;
1766 total = md_needed + nrblocks;
1767
Mingming Cao60e58e02009-01-22 18:13:05 +01001768 /*
1769 * Make quota reservation here to prevent quota overflow
1770 * later. Real quota accounting is done at pages writeout
1771 * time.
1772 */
1773 if (vfs_dq_reserve_block(inode, total)) {
1774 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1775 return -EDQUOT;
1776 }
1777
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04001778 if (ext4_claim_free_blocks(sbi, total)) {
Mingming Caod2a17632008-07-14 17:52:37 -04001779 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001780 if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1781 yield();
1782 goto repeat;
1783 }
Mingming Cao60e58e02009-01-22 18:13:05 +01001784 vfs_dq_release_reservation_block(inode, total);
Mingming Caod2a17632008-07-14 17:52:37 -04001785 return -ENOSPC;
1786 }
Mingming Caod2a17632008-07-14 17:52:37 -04001787 EXT4_I(inode)->i_reserved_data_blocks += nrblocks;
1788 EXT4_I(inode)->i_reserved_meta_blocks = mdblocks;
1789
1790 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1791 return 0; /* success */
1792}
1793
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001794static void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001795{
1796 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1797 int total, mdb, mdb_free, release;
1798
Mingming Caocd213222008-08-19 22:16:59 -04001799 if (!to_free)
1800 return; /* Nothing to release, exit */
1801
Mingming Caod2a17632008-07-14 17:52:37 -04001802 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001803
1804 if (!EXT4_I(inode)->i_reserved_data_blocks) {
1805 /*
1806 * if there is no reserved blocks, but we try to free some
1807 * then the counter is messed up somewhere.
1808 * but since this function is called from invalidate
1809 * page, it's harmless to return without any action
1810 */
1811 printk(KERN_INFO "ext4 delalloc try to release %d reserved "
1812 "blocks for inode %lu, but there is no reserved "
1813 "data blocks\n", to_free, inode->i_ino);
1814 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1815 return;
1816 }
1817
Mingming Caod2a17632008-07-14 17:52:37 -04001818 /* recalculate the number of metablocks still need to be reserved */
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001819 total = EXT4_I(inode)->i_reserved_data_blocks - to_free;
Mingming Caod2a17632008-07-14 17:52:37 -04001820 mdb = ext4_calc_metadata_amount(inode, total);
1821
1822 /* figure out how many metablocks to release */
1823 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1824 mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
1825
Mingming Caod2a17632008-07-14 17:52:37 -04001826 release = to_free + mdb_free;
1827
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001828 /* update fs dirty blocks counter for truncate case */
1829 percpu_counter_sub(&sbi->s_dirtyblocks_counter, release);
Mingming Caod2a17632008-07-14 17:52:37 -04001830
1831 /* update per-inode reservations */
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001832 BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks);
1833 EXT4_I(inode)->i_reserved_data_blocks -= to_free;
Mingming Caod2a17632008-07-14 17:52:37 -04001834
1835 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1836 EXT4_I(inode)->i_reserved_meta_blocks = mdb;
Mingming Caod2a17632008-07-14 17:52:37 -04001837 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001838
1839 vfs_dq_release_reservation_block(inode, release);
Mingming Caod2a17632008-07-14 17:52:37 -04001840}
1841
1842static void ext4_da_page_release_reservation(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001843 unsigned long offset)
Mingming Caod2a17632008-07-14 17:52:37 -04001844{
1845 int to_release = 0;
1846 struct buffer_head *head, *bh;
1847 unsigned int curr_off = 0;
1848
1849 head = page_buffers(page);
1850 bh = head;
1851 do {
1852 unsigned int next_off = curr_off + bh->b_size;
1853
1854 if ((offset <= curr_off) && (buffer_delay(bh))) {
1855 to_release++;
1856 clear_buffer_delay(bh);
1857 }
1858 curr_off = next_off;
1859 } while ((bh = bh->b_this_page) != head);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001860 ext4_da_release_space(page->mapping->host, to_release);
Mingming Caod2a17632008-07-14 17:52:37 -04001861}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001862
1863/*
Alex Tomas64769242008-07-11 19:27:31 -04001864 * Delayed allocation stuff
1865 */
1866
1867struct mpage_da_data {
1868 struct inode *inode;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001869 sector_t b_blocknr; /* start block number of extent */
1870 size_t b_size; /* size of extent */
1871 unsigned long b_state; /* state of the extent */
Alex Tomas64769242008-07-11 19:27:31 -04001872 unsigned long first_page, next_page; /* extent of pages */
Alex Tomas64769242008-07-11 19:27:31 -04001873 struct writeback_control *wbc;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001874 int io_done;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001875 int pages_written;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001876 int retval;
Alex Tomas64769242008-07-11 19:27:31 -04001877};
1878
1879/*
1880 * mpage_da_submit_io - walks through extent of pages and try to write
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001881 * them with writepage() call back
Alex Tomas64769242008-07-11 19:27:31 -04001882 *
1883 * @mpd->inode: inode
1884 * @mpd->first_page: first page of the extent
1885 * @mpd->next_page: page after the last page of the extent
Alex Tomas64769242008-07-11 19:27:31 -04001886 *
1887 * By the time mpage_da_submit_io() is called we expect all blocks
1888 * to be allocated. this may be wrong if allocation failed.
1889 *
1890 * As pages are already locked by write_cache_pages(), we can't use it
1891 */
1892static int mpage_da_submit_io(struct mpage_da_data *mpd)
1893{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001894 long pages_skipped;
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001895 struct pagevec pvec;
1896 unsigned long index, end;
1897 int ret = 0, err, nr_pages, i;
1898 struct inode *inode = mpd->inode;
1899 struct address_space *mapping = inode->i_mapping;
Alex Tomas64769242008-07-11 19:27:31 -04001900
1901 BUG_ON(mpd->next_page <= mpd->first_page);
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001902 /*
1903 * We need to start from the first_page to the next_page - 1
1904 * to make sure we also write the mapped dirty buffer_heads.
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001905 * If we look at mpd->b_blocknr we would only be looking
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001906 * at the currently mapped buffer_heads.
1907 */
Alex Tomas64769242008-07-11 19:27:31 -04001908 index = mpd->first_page;
1909 end = mpd->next_page - 1;
1910
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001911 pagevec_init(&pvec, 0);
Alex Tomas64769242008-07-11 19:27:31 -04001912 while (index <= end) {
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001913 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
Alex Tomas64769242008-07-11 19:27:31 -04001914 if (nr_pages == 0)
1915 break;
1916 for (i = 0; i < nr_pages; i++) {
1917 struct page *page = pvec.pages[i];
1918
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001919 index = page->index;
1920 if (index > end)
1921 break;
1922 index++;
1923
1924 BUG_ON(!PageLocked(page));
1925 BUG_ON(PageWriteback(page));
1926
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001927 pages_skipped = mpd->wbc->pages_skipped;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001928 err = mapping->a_ops->writepage(page, mpd->wbc);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001929 if (!err && (pages_skipped == mpd->wbc->pages_skipped))
1930 /*
1931 * have successfully written the page
1932 * without skipping the same
1933 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001934 mpd->pages_written++;
Alex Tomas64769242008-07-11 19:27:31 -04001935 /*
1936 * In error case, we have to continue because
1937 * remaining pages are still locked
1938 * XXX: unlock and re-dirty them?
1939 */
1940 if (ret == 0)
1941 ret = err;
1942 }
1943 pagevec_release(&pvec);
1944 }
Alex Tomas64769242008-07-11 19:27:31 -04001945 return ret;
1946}
1947
1948/*
1949 * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers
1950 *
1951 * @mpd->inode - inode to walk through
1952 * @exbh->b_blocknr - first block on a disk
1953 * @exbh->b_size - amount of space in bytes
1954 * @logical - first logical block to start assignment with
1955 *
1956 * the function goes through all passed space and put actual disk
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001957 * block numbers into buffer heads, dropping BH_Delay and BH_Unwritten
Alex Tomas64769242008-07-11 19:27:31 -04001958 */
1959static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical,
1960 struct buffer_head *exbh)
1961{
1962 struct inode *inode = mpd->inode;
1963 struct address_space *mapping = inode->i_mapping;
1964 int blocks = exbh->b_size >> inode->i_blkbits;
1965 sector_t pblock = exbh->b_blocknr, cur_logical;
1966 struct buffer_head *head, *bh;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001967 pgoff_t index, end;
Alex Tomas64769242008-07-11 19:27:31 -04001968 struct pagevec pvec;
1969 int nr_pages, i;
1970
1971 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
1972 end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
1973 cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1974
1975 pagevec_init(&pvec, 0);
1976
1977 while (index <= end) {
1978 /* XXX: optimize tail */
1979 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1980 if (nr_pages == 0)
1981 break;
1982 for (i = 0; i < nr_pages; i++) {
1983 struct page *page = pvec.pages[i];
1984
1985 index = page->index;
1986 if (index > end)
1987 break;
1988 index++;
1989
1990 BUG_ON(!PageLocked(page));
1991 BUG_ON(PageWriteback(page));
1992 BUG_ON(!page_has_buffers(page));
1993
1994 bh = page_buffers(page);
1995 head = bh;
1996
1997 /* skip blocks out of the range */
1998 do {
1999 if (cur_logical >= logical)
2000 break;
2001 cur_logical++;
2002 } while ((bh = bh->b_this_page) != head);
2003
2004 do {
2005 if (cur_logical >= logical + blocks)
2006 break;
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002007
2008 if (buffer_delay(bh) ||
2009 buffer_unwritten(bh)) {
2010
2011 BUG_ON(bh->b_bdev != inode->i_sb->s_bdev);
2012
2013 if (buffer_delay(bh)) {
2014 clear_buffer_delay(bh);
2015 bh->b_blocknr = pblock;
2016 } else {
2017 /*
2018 * unwritten already should have
2019 * blocknr assigned. Verify that
2020 */
2021 clear_buffer_unwritten(bh);
2022 BUG_ON(bh->b_blocknr != pblock);
2023 }
2024
Mingming Cao61628a32008-07-11 19:27:31 -04002025 } else if (buffer_mapped(bh))
Alex Tomas64769242008-07-11 19:27:31 -04002026 BUG_ON(bh->b_blocknr != pblock);
Alex Tomas64769242008-07-11 19:27:31 -04002027
2028 cur_logical++;
2029 pblock++;
2030 } while ((bh = bh->b_this_page) != head);
2031 }
2032 pagevec_release(&pvec);
2033 }
2034}
2035
2036
2037/*
2038 * __unmap_underlying_blocks - just a helper function to unmap
2039 * set of blocks described by @bh
2040 */
2041static inline void __unmap_underlying_blocks(struct inode *inode,
2042 struct buffer_head *bh)
2043{
2044 struct block_device *bdev = inode->i_sb->s_bdev;
2045 int blocks, i;
2046
2047 blocks = bh->b_size >> inode->i_blkbits;
2048 for (i = 0; i < blocks; i++)
2049 unmap_underlying_metadata(bdev, bh->b_blocknr + i);
2050}
2051
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002052static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd,
2053 sector_t logical, long blk_cnt)
2054{
2055 int nr_pages, i;
2056 pgoff_t index, end;
2057 struct pagevec pvec;
2058 struct inode *inode = mpd->inode;
2059 struct address_space *mapping = inode->i_mapping;
2060
2061 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
2062 end = (logical + blk_cnt - 1) >>
2063 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2064 while (index <= end) {
2065 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
2066 if (nr_pages == 0)
2067 break;
2068 for (i = 0; i < nr_pages; i++) {
2069 struct page *page = pvec.pages[i];
2070 index = page->index;
2071 if (index > end)
2072 break;
2073 index++;
2074
2075 BUG_ON(!PageLocked(page));
2076 BUG_ON(PageWriteback(page));
2077 block_invalidatepage(page, 0);
2078 ClearPageUptodate(page);
2079 unlock_page(page);
2080 }
2081 }
2082 return;
2083}
2084
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002085static void ext4_print_free_blocks(struct inode *inode)
2086{
2087 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2088 printk(KERN_EMERG "Total free blocks count %lld\n",
2089 ext4_count_free_blocks(inode->i_sb));
2090 printk(KERN_EMERG "Free/Dirty block details\n");
2091 printk(KERN_EMERG "free_blocks=%lld\n",
Alexander Beregalov8f72fbd2008-10-29 17:13:08 -04002092 (long long)percpu_counter_sum(&sbi->s_freeblocks_counter));
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002093 printk(KERN_EMERG "dirty_blocks=%lld\n",
Alexander Beregalov8f72fbd2008-10-29 17:13:08 -04002094 (long long)percpu_counter_sum(&sbi->s_dirtyblocks_counter));
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002095 printk(KERN_EMERG "Block reservation details\n");
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002096 printk(KERN_EMERG "i_reserved_data_blocks=%u\n",
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002097 EXT4_I(inode)->i_reserved_data_blocks);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002098 printk(KERN_EMERG "i_reserved_meta_blocks=%u\n",
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002099 EXT4_I(inode)->i_reserved_meta_blocks);
2100 return;
2101}
2102
Theodore Ts'ob920c752009-05-14 00:54:29 -04002103/*
Alex Tomas64769242008-07-11 19:27:31 -04002104 * mpage_da_map_blocks - go through given space
2105 *
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002106 * @mpd - bh describing space
Alex Tomas64769242008-07-11 19:27:31 -04002107 *
2108 * The function skips space we know is already mapped to disk blocks.
2109 *
Alex Tomas64769242008-07-11 19:27:31 -04002110 */
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002111static int mpage_da_map_blocks(struct mpage_da_data *mpd)
Alex Tomas64769242008-07-11 19:27:31 -04002112{
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002113 int err, blks, get_blocks_flags;
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002114 struct buffer_head new;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002115 sector_t next = mpd->b_blocknr;
2116 unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
2117 loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
2118 handle_t *handle = NULL;
Alex Tomas64769242008-07-11 19:27:31 -04002119
2120 /*
2121 * We consider only non-mapped and non-allocated blocks
2122 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002123 if ((mpd->b_state & (1 << BH_Mapped)) &&
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002124 !(mpd->b_state & (1 << BH_Delay)) &&
2125 !(mpd->b_state & (1 << BH_Unwritten)))
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002126 return 0;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002127
2128 /*
2129 * If we didn't accumulate anything to write simply return
2130 */
2131 if (!mpd->b_size)
2132 return 0;
2133
2134 handle = ext4_journal_current_handle();
2135 BUG_ON(!handle);
2136
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04002137 /*
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002138 * Call ext4_get_blocks() to allocate any delayed allocation
2139 * blocks, or to convert an uninitialized extent to be
2140 * initialized (in the case where we have written into
2141 * one or more preallocated blocks).
2142 *
2143 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
2144 * indicate that we are on the delayed allocation path. This
2145 * affects functions in many different parts of the allocation
2146 * call path. This flag exists primarily because we don't
2147 * want to change *many* call functions, so ext4_get_blocks()
2148 * will set the magic i_delalloc_reserved_flag once the
2149 * inode's allocation semaphore is taken.
2150 *
2151 * If the blocks in questions were delalloc blocks, set
2152 * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
2153 * variables are updated after the blocks have been allocated.
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04002154 */
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002155 new.b_state = 0;
2156 get_blocks_flags = (EXT4_GET_BLOCKS_CREATE |
2157 EXT4_GET_BLOCKS_DELALLOC_RESERVE);
2158 if (mpd->b_state & (1 << BH_Delay))
2159 get_blocks_flags |= EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002160 blks = ext4_get_blocks(handle, mpd->inode, next, max_blocks,
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002161 &new, get_blocks_flags);
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002162 if (blks < 0) {
2163 err = blks;
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002164 /*
2165 * If get block returns with error we simply
2166 * return. Later writepage will redirty the page and
2167 * writepages will find the dirty page again
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002168 */
2169 if (err == -EAGAIN)
2170 return 0;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002171
2172 if (err == -ENOSPC &&
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002173 ext4_count_free_blocks(mpd->inode->i_sb)) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002174 mpd->retval = err;
2175 return 0;
2176 }
2177
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002178 /*
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002179 * get block failure will cause us to loop in
2180 * writepages, because a_ops->writepage won't be able
2181 * to make progress. The page will be redirtied by
2182 * writepage and writepages will again try to write
2183 * the same.
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002184 */
2185 printk(KERN_EMERG "%s block allocation failed for inode %lu "
2186 "at logical offset %llu with max blocks "
2187 "%zd with error %d\n",
2188 __func__, mpd->inode->i_ino,
2189 (unsigned long long)next,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002190 mpd->b_size >> mpd->inode->i_blkbits, err);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002191 printk(KERN_EMERG "This should not happen.!! "
2192 "Data will be lost\n");
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002193 if (err == -ENOSPC) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002194 ext4_print_free_blocks(mpd->inode);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002195 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002196 /* invalidate all the pages */
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002197 ext4_da_block_invalidatepages(mpd, next,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002198 mpd->b_size >> mpd->inode->i_blkbits);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002199 return err;
2200 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002201 BUG_ON(blks == 0);
2202
2203 new.b_size = (blks << mpd->inode->i_blkbits);
Alex Tomas64769242008-07-11 19:27:31 -04002204
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002205 if (buffer_new(&new))
2206 __unmap_underlying_blocks(mpd->inode, &new);
Alex Tomas64769242008-07-11 19:27:31 -04002207
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002208 /*
2209 * If blocks are delayed marked, we need to
2210 * put actual blocknr and drop delayed bit
2211 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002212 if ((mpd->b_state & (1 << BH_Delay)) ||
2213 (mpd->b_state & (1 << BH_Unwritten)))
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002214 mpage_put_bnr_to_bhs(mpd, next, &new);
2215
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002216 if (ext4_should_order_data(mpd->inode)) {
2217 err = ext4_jbd2_file_inode(handle, mpd->inode);
2218 if (err)
2219 return err;
2220 }
2221
2222 /*
Jan Kara03f5d8b2009-06-09 00:17:05 -04002223 * Update on-disk size along with block allocation.
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002224 */
2225 disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
2226 if (disksize > i_size_read(mpd->inode))
2227 disksize = i_size_read(mpd->inode);
2228 if (disksize > EXT4_I(mpd->inode)->i_disksize) {
2229 ext4_update_i_disksize(mpd->inode, disksize);
2230 return ext4_mark_inode_dirty(handle, mpd->inode);
2231 }
2232
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002233 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04002234}
2235
Aneesh Kumar K.Vbf068ee2008-08-19 22:16:43 -04002236#define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
2237 (1 << BH_Delay) | (1 << BH_Unwritten))
Alex Tomas64769242008-07-11 19:27:31 -04002238
2239/*
2240 * mpage_add_bh_to_extent - try to add one more block to extent of blocks
2241 *
2242 * @mpd->lbh - extent of blocks
2243 * @logical - logical number of the block in the file
2244 * @bh - bh of the block (used to access block's state)
2245 *
2246 * the function is used to collect contig. blocks in same state
2247 */
2248static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002249 sector_t logical, size_t b_size,
2250 unsigned long b_state)
Alex Tomas64769242008-07-11 19:27:31 -04002251{
Alex Tomas64769242008-07-11 19:27:31 -04002252 sector_t next;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002253 int nrblocks = mpd->b_size >> mpd->inode->i_blkbits;
Alex Tomas64769242008-07-11 19:27:31 -04002254
Mingming Cao525f4ed2008-08-19 22:15:58 -04002255 /* check if thereserved journal credits might overflow */
2256 if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) {
2257 if (nrblocks >= EXT4_MAX_TRANS_DATA) {
2258 /*
2259 * With non-extent format we are limited by the journal
2260 * credit available. Total credit needed to insert
2261 * nrblocks contiguous blocks is dependent on the
2262 * nrblocks. So limit nrblocks.
2263 */
2264 goto flush_it;
2265 } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
2266 EXT4_MAX_TRANS_DATA) {
2267 /*
2268 * Adding the new buffer_head would make it cross the
2269 * allowed limit for which we have journal credit
2270 * reserved. So limit the new bh->b_size
2271 */
2272 b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
2273 mpd->inode->i_blkbits;
2274 /* we will do mpage_da_submit_io in the next loop */
2275 }
2276 }
Alex Tomas64769242008-07-11 19:27:31 -04002277 /*
2278 * First block in the extent
2279 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002280 if (mpd->b_size == 0) {
2281 mpd->b_blocknr = logical;
2282 mpd->b_size = b_size;
2283 mpd->b_state = b_state & BH_FLAGS;
Alex Tomas64769242008-07-11 19:27:31 -04002284 return;
2285 }
2286
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002287 next = mpd->b_blocknr + nrblocks;
Alex Tomas64769242008-07-11 19:27:31 -04002288 /*
2289 * Can we merge the block to our big extent?
2290 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002291 if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
2292 mpd->b_size += b_size;
Alex Tomas64769242008-07-11 19:27:31 -04002293 return;
2294 }
2295
Mingming Cao525f4ed2008-08-19 22:15:58 -04002296flush_it:
Alex Tomas64769242008-07-11 19:27:31 -04002297 /*
2298 * We couldn't merge the block to our extent, so we
2299 * need to flush current extent and start new one
2300 */
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002301 if (mpage_da_map_blocks(mpd) == 0)
2302 mpage_da_submit_io(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002303 mpd->io_done = 1;
2304 return;
Alex Tomas64769242008-07-11 19:27:31 -04002305}
2306
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002307static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002308{
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002309 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002310}
2311
Alex Tomas64769242008-07-11 19:27:31 -04002312/*
2313 * __mpage_da_writepage - finds extent of pages and blocks
2314 *
2315 * @page: page to consider
2316 * @wbc: not used, we just follow rules
2317 * @data: context
2318 *
2319 * The function finds extents of pages and scan them for all blocks.
2320 */
2321static int __mpage_da_writepage(struct page *page,
2322 struct writeback_control *wbc, void *data)
2323{
2324 struct mpage_da_data *mpd = data;
2325 struct inode *inode = mpd->inode;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002326 struct buffer_head *bh, *head;
Alex Tomas64769242008-07-11 19:27:31 -04002327 sector_t logical;
2328
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002329 if (mpd->io_done) {
2330 /*
2331 * Rest of the page in the page_vec
2332 * redirty then and skip then. We will
2333 * try to to write them again after
2334 * starting a new transaction
2335 */
2336 redirty_page_for_writepage(wbc, page);
2337 unlock_page(page);
2338 return MPAGE_DA_EXTENT_TAIL;
2339 }
Alex Tomas64769242008-07-11 19:27:31 -04002340 /*
2341 * Can we merge this page to current extent?
2342 */
2343 if (mpd->next_page != page->index) {
2344 /*
2345 * Nope, we can't. So, we map non-allocated blocks
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002346 * and start IO on them using writepage()
Alex Tomas64769242008-07-11 19:27:31 -04002347 */
2348 if (mpd->next_page != mpd->first_page) {
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002349 if (mpage_da_map_blocks(mpd) == 0)
2350 mpage_da_submit_io(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002351 /*
2352 * skip rest of the page in the page_vec
2353 */
2354 mpd->io_done = 1;
2355 redirty_page_for_writepage(wbc, page);
2356 unlock_page(page);
2357 return MPAGE_DA_EXTENT_TAIL;
Alex Tomas64769242008-07-11 19:27:31 -04002358 }
2359
2360 /*
2361 * Start next extent of pages ...
2362 */
2363 mpd->first_page = page->index;
2364
2365 /*
2366 * ... and blocks
2367 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002368 mpd->b_size = 0;
2369 mpd->b_state = 0;
2370 mpd->b_blocknr = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002371 }
2372
2373 mpd->next_page = page->index + 1;
2374 logical = (sector_t) page->index <<
2375 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2376
2377 if (!page_has_buffers(page)) {
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002378 mpage_add_bh_to_extent(mpd, logical, PAGE_CACHE_SIZE,
2379 (1 << BH_Dirty) | (1 << BH_Uptodate));
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002380 if (mpd->io_done)
2381 return MPAGE_DA_EXTENT_TAIL;
Alex Tomas64769242008-07-11 19:27:31 -04002382 } else {
2383 /*
2384 * Page with regular buffer heads, just add all dirty ones
2385 */
2386 head = page_buffers(page);
2387 bh = head;
2388 do {
2389 BUG_ON(buffer_locked(bh));
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002390 /*
2391 * We need to try to allocate
2392 * unmapped blocks in the same page.
2393 * Otherwise we won't make progress
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002394 * with the page in ext4_writepage
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002395 */
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002396 if (ext4_bh_delay_or_unwritten(NULL, bh)) {
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002397 mpage_add_bh_to_extent(mpd, logical,
2398 bh->b_size,
2399 bh->b_state);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002400 if (mpd->io_done)
2401 return MPAGE_DA_EXTENT_TAIL;
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002402 } else if (buffer_dirty(bh) && (buffer_mapped(bh))) {
2403 /*
2404 * mapped dirty buffer. We need to update
2405 * the b_state because we look at
2406 * b_state in mpage_da_map_blocks. We don't
2407 * update b_size because if we find an
2408 * unmapped buffer_head later we need to
2409 * use the b_state flag of that buffer_head.
2410 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002411 if (mpd->b_size == 0)
2412 mpd->b_state = bh->b_state & BH_FLAGS;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002413 }
Alex Tomas64769242008-07-11 19:27:31 -04002414 logical++;
2415 } while ((bh = bh->b_this_page) != head);
2416 }
2417
2418 return 0;
2419}
2420
2421/*
Theodore Ts'ob920c752009-05-14 00:54:29 -04002422 * This is a special get_blocks_t callback which is used by
2423 * ext4_da_write_begin(). It will either return mapped block or
2424 * reserve space for a single block.
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002425 *
2426 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
2427 * We also have b_blocknr = -1 and b_bdev initialized properly
2428 *
2429 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
2430 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
2431 * initialized properly.
Alex Tomas64769242008-07-11 19:27:31 -04002432 */
2433static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
2434 struct buffer_head *bh_result, int create)
2435{
2436 int ret = 0;
Aneesh Kumar K.V33b98172009-05-12 14:40:37 -04002437 sector_t invalid_block = ~((sector_t) 0xffff);
2438
2439 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
2440 invalid_block = ~0;
Alex Tomas64769242008-07-11 19:27:31 -04002441
2442 BUG_ON(create == 0);
2443 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
2444
2445 /*
2446 * first, we need to know whether the block is allocated already
2447 * preallocated blocks are unmapped but should treated
2448 * the same as allocated blocks.
2449 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04002450 ret = ext4_get_blocks(NULL, inode, iblock, 1, bh_result, 0);
Mingming Caod2a17632008-07-14 17:52:37 -04002451 if ((ret == 0) && !buffer_delay(bh_result)) {
2452 /* the block isn't (pre)allocated yet, let's reserve space */
Alex Tomas64769242008-07-11 19:27:31 -04002453 /*
2454 * XXX: __block_prepare_write() unmaps passed block,
2455 * is it OK?
2456 */
Mingming Caod2a17632008-07-14 17:52:37 -04002457 ret = ext4_da_reserve_space(inode, 1);
2458 if (ret)
2459 /* not enough space to reserve */
2460 return ret;
2461
Aneesh Kumar K.V33b98172009-05-12 14:40:37 -04002462 map_bh(bh_result, inode->i_sb, invalid_block);
Alex Tomas64769242008-07-11 19:27:31 -04002463 set_buffer_new(bh_result);
2464 set_buffer_delay(bh_result);
2465 } else if (ret > 0) {
2466 bh_result->b_size = (ret << inode->i_blkbits);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002467 if (buffer_unwritten(bh_result)) {
2468 /* A delayed write to unwritten bh should
2469 * be marked new and mapped. Mapped ensures
2470 * that we don't do get_block multiple times
2471 * when we write to the same offset and new
2472 * ensures that we do proper zero out for
2473 * partial write.
2474 */
Aneesh Kumar K.V9c1ee182009-05-13 18:36:58 -04002475 set_buffer_new(bh_result);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002476 set_buffer_mapped(bh_result);
2477 }
Alex Tomas64769242008-07-11 19:27:31 -04002478 ret = 0;
2479 }
2480
2481 return ret;
2482}
Mingming Cao61628a32008-07-11 19:27:31 -04002483
Theodore Ts'ob920c752009-05-14 00:54:29 -04002484/*
2485 * This function is used as a standard get_block_t calback function
2486 * when there is no desire to allocate any blocks. It is used as a
2487 * callback function for block_prepare_write(), nobh_writepage(), and
2488 * block_write_full_page(). These functions should only try to map a
2489 * single block at a time.
2490 *
2491 * Since this function doesn't do block allocations even if the caller
2492 * requests it by passing in create=1, it is critically important that
2493 * any caller checks to make sure that any buffer heads are returned
2494 * by this function are either all already mapped or marked for
2495 * delayed allocation before calling nobh_writepage() or
2496 * block_write_full_page(). Otherwise, b_blocknr could be left
2497 * unitialized, and the page write functions will be taken by
2498 * surprise.
2499 */
2500static int noalloc_get_block_write(struct inode *inode, sector_t iblock,
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002501 struct buffer_head *bh_result, int create)
2502{
2503 int ret = 0;
2504 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
2505
Theodore Ts'oa2dc52b2009-05-12 13:51:29 -04002506 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
2507
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002508 /*
2509 * we don't want to do block allocation in writepage
2510 * so call get_block_wrap with create = 0
2511 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04002512 ret = ext4_get_blocks(NULL, inode, iblock, max_blocks, bh_result, 0);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002513 if (ret > 0) {
2514 bh_result->b_size = (ret << inode->i_blkbits);
2515 ret = 0;
2516 }
2517 return ret;
Mingming Cao61628a32008-07-11 19:27:31 -04002518}
2519
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002520static int bget_one(handle_t *handle, struct buffer_head *bh)
2521{
2522 get_bh(bh);
2523 return 0;
2524}
2525
2526static int bput_one(handle_t *handle, struct buffer_head *bh)
2527{
2528 put_bh(bh);
2529 return 0;
2530}
2531
2532static int __ext4_journalled_writepage(struct page *page,
2533 struct writeback_control *wbc,
2534 unsigned int len)
2535{
2536 struct address_space *mapping = page->mapping;
2537 struct inode *inode = mapping->host;
2538 struct buffer_head *page_bufs;
2539 handle_t *handle = NULL;
2540 int ret = 0;
2541 int err;
2542
2543 page_bufs = page_buffers(page);
2544 BUG_ON(!page_bufs);
2545 walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one);
2546 /* As soon as we unlock the page, it can go away, but we have
2547 * references to buffers so we are safe */
2548 unlock_page(page);
2549
2550 handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
2551 if (IS_ERR(handle)) {
2552 ret = PTR_ERR(handle);
2553 goto out;
2554 }
2555
2556 ret = walk_page_buffers(handle, page_bufs, 0, len, NULL,
2557 do_journal_get_write_access);
2558
2559 err = walk_page_buffers(handle, page_bufs, 0, len, NULL,
2560 write_end_fn);
2561 if (ret == 0)
2562 ret = err;
2563 err = ext4_journal_stop(handle);
2564 if (!ret)
2565 ret = err;
2566
2567 walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one);
2568 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
2569out:
2570 return ret;
2571}
2572
Mingming Cao61628a32008-07-11 19:27:31 -04002573/*
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002574 * Note that we don't need to start a transaction unless we're journaling data
2575 * because we should have holes filled from ext4_page_mkwrite(). We even don't
2576 * need to file the inode to the transaction's list in ordered mode because if
2577 * we are writing back data added by write(), the inode is already there and if
2578 * we are writing back data modified via mmap(), noone guarantees in which
2579 * transaction the data will hit the disk. In case we are journaling data, we
2580 * cannot start transaction directly because transaction start ranks above page
2581 * lock so we have to do some magic.
2582 *
Theodore Ts'ob920c752009-05-14 00:54:29 -04002583 * This function can get called via...
2584 * - ext4_da_writepages after taking page lock (have journal handle)
2585 * - journal_submit_inode_data_buffers (no journal handle)
2586 * - shrink_page_list via pdflush (no journal handle)
2587 * - grab_page_cache when doing write_begin (have journal handle)
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002588 *
2589 * We don't do any block allocation in this function. If we have page with
2590 * multiple blocks we need to write those buffer_heads that are mapped. This
2591 * is important for mmaped based write. So if we do with blocksize 1K
2592 * truncate(f, 1024);
2593 * a = mmap(f, 0, 4096);
2594 * a[0] = 'a';
2595 * truncate(f, 4096);
2596 * we have in the page first buffer_head mapped via page_mkwrite call back
2597 * but other bufer_heads would be unmapped but dirty(dirty done via the
2598 * do_wp_page). So writepage should write the first block. If we modify
2599 * the mmap area beyond 1024 we will again get a page_fault and the
2600 * page_mkwrite callback will do the block allocation and mark the
2601 * buffer_heads mapped.
2602 *
2603 * We redirty the page if we have any buffer_heads that is either delay or
2604 * unwritten in the page.
2605 *
2606 * We can get recursively called as show below.
2607 *
2608 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
2609 * ext4_writepage()
2610 *
2611 * But since we don't do any block allocation we should not deadlock.
2612 * Page also have the dirty flag cleared so we don't get recurive page_lock.
Mingming Cao61628a32008-07-11 19:27:31 -04002613 */
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002614static int ext4_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002615 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002616{
Alex Tomas64769242008-07-11 19:27:31 -04002617 int ret = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002618 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002619 unsigned int len;
Mingming Cao61628a32008-07-11 19:27:31 -04002620 struct buffer_head *page_bufs;
2621 struct inode *inode = page->mapping->host;
Alex Tomas64769242008-07-11 19:27:31 -04002622
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002623 trace_ext4_writepage(inode, page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002624 size = i_size_read(inode);
2625 if (page->index == size >> PAGE_CACHE_SHIFT)
2626 len = size & ~PAGE_CACHE_MASK;
2627 else
2628 len = PAGE_CACHE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04002629
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002630 if (page_has_buffers(page)) {
Mingming Cao61628a32008-07-11 19:27:31 -04002631 page_bufs = page_buffers(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002632 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002633 ext4_bh_delay_or_unwritten)) {
Mingming Cao61628a32008-07-11 19:27:31 -04002634 /*
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002635 * We don't want to do block allocation
2636 * So redirty the page and return
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04002637 * We may reach here when we do a journal commit
2638 * via journal_submit_inode_data_buffers.
2639 * If we don't have mapping block we just ignore
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002640 * them. We can also reach here via shrink_page_list
2641 */
2642 redirty_page_for_writepage(wbc, page);
2643 unlock_page(page);
2644 return 0;
2645 }
2646 } else {
2647 /*
2648 * The test for page_has_buffers() is subtle:
2649 * We know the page is dirty but it lost buffers. That means
2650 * that at some moment in time after write_begin()/write_end()
2651 * has been called all buffers have been clean and thus they
2652 * must have been written at least once. So they are all
2653 * mapped and we can happily proceed with mapping them
2654 * and writing the page.
2655 *
2656 * Try to initialize the buffer_heads and check whether
2657 * all are mapped and non delay. We don't want to
2658 * do block allocation here.
2659 */
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04002660 ret = block_prepare_write(page, 0, len,
Theodore Ts'ob920c752009-05-14 00:54:29 -04002661 noalloc_get_block_write);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002662 if (!ret) {
2663 page_bufs = page_buffers(page);
2664 /* check whether all are mapped and non delay */
2665 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002666 ext4_bh_delay_or_unwritten)) {
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002667 redirty_page_for_writepage(wbc, page);
2668 unlock_page(page);
2669 return 0;
2670 }
2671 } else {
2672 /*
2673 * We can't do block allocation here
2674 * so just redity the page and unlock
2675 * and return
Mingming Cao61628a32008-07-11 19:27:31 -04002676 */
Mingming Cao61628a32008-07-11 19:27:31 -04002677 redirty_page_for_writepage(wbc, page);
2678 unlock_page(page);
2679 return 0;
2680 }
Aneesh Kumar K.Ved9b3e32008-11-07 09:06:45 -05002681 /* now mark the buffer_heads as dirty and uptodate */
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04002682 block_commit_write(page, 0, len);
Alex Tomas64769242008-07-11 19:27:31 -04002683 }
2684
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002685 if (PageChecked(page) && ext4_should_journal_data(inode)) {
2686 /*
2687 * It's mmapped pagecache. Add buffers and journal it. There
2688 * doesn't seem much point in redirtying the page here.
2689 */
2690 ClearPageChecked(page);
2691 return __ext4_journalled_writepage(page, wbc, len);
2692 }
2693
Alex Tomas64769242008-07-11 19:27:31 -04002694 if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode))
Theodore Ts'ob920c752009-05-14 00:54:29 -04002695 ret = nobh_writepage(page, noalloc_get_block_write, wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002696 else
Theodore Ts'ob920c752009-05-14 00:54:29 -04002697 ret = block_write_full_page(page, noalloc_get_block_write,
2698 wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002699
Alex Tomas64769242008-07-11 19:27:31 -04002700 return ret;
2701}
2702
Mingming Cao61628a32008-07-11 19:27:31 -04002703/*
Mingming Cao525f4ed2008-08-19 22:15:58 -04002704 * This is called via ext4_da_writepages() to
2705 * calulate the total number of credits to reserve to fit
2706 * a single extent allocation into a single transaction,
2707 * ext4_da_writpeages() will loop calling this before
2708 * the block allocation.
Mingming Cao61628a32008-07-11 19:27:31 -04002709 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002710
2711static int ext4_da_writepages_trans_blocks(struct inode *inode)
2712{
2713 int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
2714
2715 /*
2716 * With non-extent format the journal credit needed to
2717 * insert nrblocks contiguous block is dependent on
2718 * number of contiguous block. So we will limit
2719 * number of contiguous block to a sane value
2720 */
2721 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2722 (max_blocks > EXT4_MAX_TRANS_DATA))
2723 max_blocks = EXT4_MAX_TRANS_DATA;
2724
2725 return ext4_chunk_trans_blocks(inode, max_blocks);
2726}
Mingming Cao61628a32008-07-11 19:27:31 -04002727
Alex Tomas64769242008-07-11 19:27:31 -04002728static int ext4_da_writepages(struct address_space *mapping,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002729 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002730{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002731 pgoff_t index;
2732 int range_whole = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002733 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002734 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002735 struct inode *inode = mapping->host;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002736 int no_nrwrite_index_update;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002737 int pages_written = 0;
2738 long pages_skipped;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002739 int range_cyclic, cycled = 1, io_done = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002740 int needed_blocks, ret = 0, nr_to_writebump = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002741 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Mingming Cao61628a32008-07-11 19:27:31 -04002742
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002743 trace_ext4_da_writepages(inode, wbc);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002744
Mingming Cao61628a32008-07-11 19:27:31 -04002745 /*
2746 * No pages to write? This is mainly a kludge to avoid starting
2747 * a transaction for special inodes like journal inode on last iput()
2748 * because that could violate lock ordering on umount
2749 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002750 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Mingming Cao61628a32008-07-11 19:27:31 -04002751 return 0;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002752
2753 /*
2754 * If the filesystem has aborted, it is read-only, so return
2755 * right away instead of dumping stack traces later on that
2756 * will obscure the real source of the problem. We test
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002757 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002758 * the latter could be true if the filesystem is mounted
2759 * read-only, and in that case, ext4_da_writepages should
2760 * *never* be called, so if that ever happens, we would want
2761 * the stack trace.
2762 */
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002763 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002764 return -EROFS;
2765
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002766 /*
2767 * Make sure nr_to_write is >= sbi->s_mb_stream_request
2768 * This make sure small files blocks are allocated in
2769 * single attempt. This ensure that small files
2770 * get less fragmented.
2771 */
2772 if (wbc->nr_to_write < sbi->s_mb_stream_request) {
2773 nr_to_writebump = sbi->s_mb_stream_request - wbc->nr_to_write;
2774 wbc->nr_to_write = sbi->s_mb_stream_request;
2775 }
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002776 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2777 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002778
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002779 range_cyclic = wbc->range_cyclic;
2780 if (wbc->range_cyclic) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002781 index = mapping->writeback_index;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002782 if (index)
2783 cycled = 0;
2784 wbc->range_start = index << PAGE_CACHE_SHIFT;
2785 wbc->range_end = LLONG_MAX;
2786 wbc->range_cyclic = 0;
2787 } else
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002788 index = wbc->range_start >> PAGE_CACHE_SHIFT;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002789
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002790 mpd.wbc = wbc;
2791 mpd.inode = mapping->host;
2792
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002793 /*
2794 * we don't want write_cache_pages to update
2795 * nr_to_write and writeback_index
2796 */
2797 no_nrwrite_index_update = wbc->no_nrwrite_index_update;
2798 wbc->no_nrwrite_index_update = 1;
2799 pages_skipped = wbc->pages_skipped;
2800
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002801retry:
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002802 while (!ret && wbc->nr_to_write > 0) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002803
2804 /*
2805 * we insert one extent at a time. So we need
2806 * credit needed for single extent allocation.
2807 * journalled mode is currently not supported
2808 * by delalloc
2809 */
2810 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002811 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002812
Mingming Cao61628a32008-07-11 19:27:31 -04002813 /* start a new transaction*/
2814 handle = ext4_journal_start(inode, needed_blocks);
2815 if (IS_ERR(handle)) {
2816 ret = PTR_ERR(handle);
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002817 printk(KERN_CRIT "%s: jbd2_start: "
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002818 "%ld pages, ino %lu; err %d\n", __func__,
2819 wbc->nr_to_write, inode->i_ino, ret);
2820 dump_stack();
Mingming Cao61628a32008-07-11 19:27:31 -04002821 goto out_writepages;
2822 }
Theodore Ts'of63e6002009-02-23 16:42:39 -05002823
2824 /*
2825 * Now call __mpage_da_writepage to find the next
2826 * contiguous region of logical blocks that need
2827 * blocks to be allocated by ext4. We don't actually
2828 * submit the blocks for I/O here, even though
2829 * write_cache_pages thinks it will, and will set the
2830 * pages as clean for write before calling
2831 * __mpage_da_writepage().
2832 */
2833 mpd.b_size = 0;
2834 mpd.b_state = 0;
2835 mpd.b_blocknr = 0;
2836 mpd.first_page = 0;
2837 mpd.next_page = 0;
2838 mpd.io_done = 0;
2839 mpd.pages_written = 0;
2840 mpd.retval = 0;
2841 ret = write_cache_pages(mapping, wbc, __mpage_da_writepage,
2842 &mpd);
2843 /*
2844 * If we have a contigous extent of pages and we
2845 * haven't done the I/O yet, map the blocks and submit
2846 * them for I/O.
2847 */
2848 if (!mpd.io_done && mpd.next_page != mpd.first_page) {
2849 if (mpage_da_map_blocks(&mpd) == 0)
2850 mpage_da_submit_io(&mpd);
2851 mpd.io_done = 1;
2852 ret = MPAGE_DA_EXTENT_TAIL;
2853 }
2854 wbc->nr_to_write -= mpd.pages_written;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002855
Mingming Cao61628a32008-07-11 19:27:31 -04002856 ext4_journal_stop(handle);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002857
Eric Sandeen8f64b322009-02-26 00:57:35 -05002858 if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002859 /* commit the transaction which would
2860 * free blocks released in the transaction
2861 * and try again
2862 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002863 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002864 wbc->pages_skipped = pages_skipped;
2865 ret = 0;
2866 } else if (ret == MPAGE_DA_EXTENT_TAIL) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002867 /*
2868 * got one extent now try with
2869 * rest of the pages
2870 */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002871 pages_written += mpd.pages_written;
2872 wbc->pages_skipped = pages_skipped;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002873 ret = 0;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002874 io_done = 1;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002875 } else if (wbc->nr_to_write)
Mingming Cao61628a32008-07-11 19:27:31 -04002876 /*
2877 * There is no more writeout needed
2878 * or we requested for a noblocking writeout
2879 * and we found the device congested
2880 */
Mingming Cao61628a32008-07-11 19:27:31 -04002881 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002882 }
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002883 if (!io_done && !cycled) {
2884 cycled = 1;
2885 index = 0;
2886 wbc->range_start = index << PAGE_CACHE_SHIFT;
2887 wbc->range_end = mapping->writeback_index - 1;
2888 goto retry;
2889 }
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002890 if (pages_skipped != wbc->pages_skipped)
2891 printk(KERN_EMERG "This should not happen leaving %s "
2892 "with nr_to_write = %ld ret = %d\n",
2893 __func__, wbc->nr_to_write, ret);
Mingming Cao61628a32008-07-11 19:27:31 -04002894
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002895 /* Update index */
2896 index += pages_written;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002897 wbc->range_cyclic = range_cyclic;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002898 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2899 /*
2900 * set the writeback_index so that range_cyclic
2901 * mode will write it back later
2902 */
2903 mapping->writeback_index = index;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002904
Mingming Cao61628a32008-07-11 19:27:31 -04002905out_writepages:
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002906 if (!no_nrwrite_index_update)
2907 wbc->no_nrwrite_index_update = 0;
2908 wbc->nr_to_write -= nr_to_writebump;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002909 trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
Mingming Cao61628a32008-07-11 19:27:31 -04002910 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002911}
2912
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002913#define FALL_BACK_TO_NONDELALLOC 1
2914static int ext4_nonda_switch(struct super_block *sb)
2915{
2916 s64 free_blocks, dirty_blocks;
2917 struct ext4_sb_info *sbi = EXT4_SB(sb);
2918
2919 /*
2920 * switch to non delalloc mode if we are running low
2921 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08002922 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002923 * accumulated on each CPU without updating global counters
2924 * Delalloc need an accurate free block accounting. So switch
2925 * to non delalloc when we are near to error range.
2926 */
2927 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
2928 dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter);
2929 if (2 * free_blocks < 3 * dirty_blocks ||
2930 free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) {
2931 /*
2932 * free block count is less that 150% of dirty blocks
2933 * or free blocks is less that watermark
2934 */
2935 return 1;
2936 }
2937 return 0;
2938}
2939
Alex Tomas64769242008-07-11 19:27:31 -04002940static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002941 loff_t pos, unsigned len, unsigned flags,
2942 struct page **pagep, void **fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002943{
Mingming Caod2a17632008-07-14 17:52:37 -04002944 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002945 struct page *page;
2946 pgoff_t index;
2947 unsigned from, to;
2948 struct inode *inode = mapping->host;
2949 handle_t *handle;
2950
2951 index = pos >> PAGE_CACHE_SHIFT;
2952 from = pos & (PAGE_CACHE_SIZE - 1);
2953 to = from + len;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002954
2955 if (ext4_nonda_switch(inode->i_sb)) {
2956 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2957 return ext4_write_begin(file, mapping, pos,
2958 len, flags, pagep, fsdata);
2959 }
2960 *fsdata = (void *)0;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002961 trace_ext4_da_write_begin(inode, pos, len, flags);
Mingming Caod2a17632008-07-14 17:52:37 -04002962retry:
Alex Tomas64769242008-07-11 19:27:31 -04002963 /*
2964 * With delayed allocation, we don't log the i_disksize update
2965 * if there is delayed block allocation. But we still need
2966 * to journalling the i_disksize update if writes to the end
2967 * of file which has an already mapped buffer.
2968 */
2969 handle = ext4_journal_start(inode, 1);
2970 if (IS_ERR(handle)) {
2971 ret = PTR_ERR(handle);
2972 goto out;
2973 }
Jan Karaebd36102009-02-22 21:09:59 -05002974 /* We cannot recurse into the filesystem as the transaction is already
2975 * started */
2976 flags |= AOP_FLAG_NOFS;
Alex Tomas64769242008-07-11 19:27:31 -04002977
Nick Piggin54566b22009-01-04 12:00:53 -08002978 page = grab_cache_page_write_begin(mapping, index, flags);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002979 if (!page) {
2980 ext4_journal_stop(handle);
2981 ret = -ENOMEM;
2982 goto out;
2983 }
Alex Tomas64769242008-07-11 19:27:31 -04002984 *pagep = page;
2985
2986 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
Theodore Ts'ob920c752009-05-14 00:54:29 -04002987 ext4_da_get_block_prep);
Alex Tomas64769242008-07-11 19:27:31 -04002988 if (ret < 0) {
2989 unlock_page(page);
2990 ext4_journal_stop(handle);
2991 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04002992 /*
2993 * block_write_begin may have instantiated a few blocks
2994 * outside i_size. Trim these off again. Don't need
2995 * i_size_read because we hold i_mutex.
2996 */
2997 if (pos + len > inode->i_size)
Jan Karaffacfa72009-07-13 16:22:22 -04002998 ext4_truncate(inode);
Alex Tomas64769242008-07-11 19:27:31 -04002999 }
3000
Mingming Caod2a17632008-07-14 17:52:37 -04003001 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3002 goto retry;
Alex Tomas64769242008-07-11 19:27:31 -04003003out:
3004 return ret;
3005}
3006
Mingming Cao632eaea2008-07-11 19:27:31 -04003007/*
3008 * Check if we should update i_disksize
3009 * when write to the end of file but not require block allocation
3010 */
3011static int ext4_da_should_update_i_disksize(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003012 unsigned long offset)
Mingming Cao632eaea2008-07-11 19:27:31 -04003013{
3014 struct buffer_head *bh;
3015 struct inode *inode = page->mapping->host;
3016 unsigned int idx;
3017 int i;
3018
3019 bh = page_buffers(page);
3020 idx = offset >> inode->i_blkbits;
3021
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003022 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04003023 bh = bh->b_this_page;
3024
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04003025 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
Mingming Cao632eaea2008-07-11 19:27:31 -04003026 return 0;
3027 return 1;
3028}
3029
Alex Tomas64769242008-07-11 19:27:31 -04003030static int ext4_da_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003031 struct address_space *mapping,
3032 loff_t pos, unsigned len, unsigned copied,
3033 struct page *page, void *fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04003034{
3035 struct inode *inode = mapping->host;
3036 int ret = 0, ret2;
3037 handle_t *handle = ext4_journal_current_handle();
3038 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04003039 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003040 int write_mode = (int)(unsigned long)fsdata;
3041
3042 if (write_mode == FALL_BACK_TO_NONDELALLOC) {
3043 if (ext4_should_order_data(inode)) {
3044 return ext4_ordered_write_end(file, mapping, pos,
3045 len, copied, page, fsdata);
3046 } else if (ext4_should_writeback_data(inode)) {
3047 return ext4_writeback_write_end(file, mapping, pos,
3048 len, copied, page, fsdata);
3049 } else {
3050 BUG();
3051 }
3052 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003053
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003054 trace_ext4_da_write_end(inode, pos, len, copied);
Mingming Cao632eaea2008-07-11 19:27:31 -04003055 start = pos & (PAGE_CACHE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003056 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04003057
3058 /*
3059 * generic_write_end() will run mark_inode_dirty() if i_size
3060 * changes. So let's piggyback the i_disksize mark_inode_dirty
3061 * into that.
3062 */
3063
3064 new_i_size = pos + copied;
Mingming Cao632eaea2008-07-11 19:27:31 -04003065 if (new_i_size > EXT4_I(inode)->i_disksize) {
3066 if (ext4_da_should_update_i_disksize(page, end)) {
3067 down_write(&EXT4_I(inode)->i_data_sem);
3068 if (new_i_size > EXT4_I(inode)->i_disksize) {
3069 /*
3070 * Updating i_disksize when extending file
3071 * without needing block allocation
3072 */
3073 if (ext4_should_order_data(inode))
3074 ret = ext4_jbd2_file_inode(handle,
3075 inode);
Alex Tomas64769242008-07-11 19:27:31 -04003076
Mingming Cao632eaea2008-07-11 19:27:31 -04003077 EXT4_I(inode)->i_disksize = new_i_size;
3078 }
3079 up_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003080 /* We need to mark inode dirty even if
3081 * new_i_size is less that inode->i_size
3082 * bu greater than i_disksize.(hint delalloc)
3083 */
3084 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04003085 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003086 }
Alex Tomas64769242008-07-11 19:27:31 -04003087 ret2 = generic_write_end(file, mapping, pos, len, copied,
3088 page, fsdata);
3089 copied = ret2;
3090 if (ret2 < 0)
3091 ret = ret2;
3092 ret2 = ext4_journal_stop(handle);
3093 if (!ret)
3094 ret = ret2;
3095
3096 return ret ? ret : copied;
3097}
3098
3099static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
3100{
Alex Tomas64769242008-07-11 19:27:31 -04003101 /*
3102 * Drop reserved blocks
3103 */
3104 BUG_ON(!PageLocked(page));
3105 if (!page_has_buffers(page))
3106 goto out;
3107
Mingming Caod2a17632008-07-14 17:52:37 -04003108 ext4_da_page_release_reservation(page, offset);
Alex Tomas64769242008-07-11 19:27:31 -04003109
3110out:
3111 ext4_invalidatepage(page, offset);
3112
3113 return;
3114}
3115
Theodore Ts'occd25062009-02-26 01:04:07 -05003116/*
3117 * Force all delayed allocation blocks to be allocated for a given inode.
3118 */
3119int ext4_alloc_da_blocks(struct inode *inode)
3120{
3121 if (!EXT4_I(inode)->i_reserved_data_blocks &&
3122 !EXT4_I(inode)->i_reserved_meta_blocks)
3123 return 0;
3124
3125 /*
3126 * We do something simple for now. The filemap_flush() will
3127 * also start triggering a write of the data blocks, which is
3128 * not strictly speaking necessary (and for users of
3129 * laptop_mode, not even desirable). However, to do otherwise
3130 * would require replicating code paths in:
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003131 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003132 * ext4_da_writepages() ->
3133 * write_cache_pages() ---> (via passed in callback function)
3134 * __mpage_da_writepage() -->
3135 * mpage_add_bh_to_extent()
3136 * mpage_da_map_blocks()
3137 *
3138 * The problem is that write_cache_pages(), located in
3139 * mm/page-writeback.c, marks pages clean in preparation for
3140 * doing I/O, which is not desirable if we're not planning on
3141 * doing I/O at all.
3142 *
3143 * We could call write_cache_pages(), and then redirty all of
3144 * the pages by calling redirty_page_for_writeback() but that
3145 * would be ugly in the extreme. So instead we would need to
3146 * replicate parts of the code in the above functions,
3147 * simplifying them becuase we wouldn't actually intend to
3148 * write out the pages, but rather only collect contiguous
3149 * logical block extents, call the multi-block allocator, and
3150 * then update the buffer heads with the block allocations.
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003151 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003152 * For now, though, we'll cheat by calling filemap_flush(),
3153 * which will map the blocks, and start the I/O, but not
3154 * actually wait for the I/O to complete.
3155 */
3156 return filemap_flush(inode->i_mapping);
3157}
Alex Tomas64769242008-07-11 19:27:31 -04003158
3159/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003160 * bmap() is special. It gets used by applications such as lilo and by
3161 * the swapper to find the on-disk block of a specific piece of data.
3162 *
3163 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07003164 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003165 * filesystem and enables swap, then they may get a nasty shock when the
3166 * data getting swapped to that swapfile suddenly gets overwritten by
3167 * the original zero's written out previously to the journal and
3168 * awaiting writeback in the kernel's buffer cache.
3169 *
3170 * So, if we see any bmap calls here on a modified, data-journaled file,
3171 * take extra steps to flush any blocks which might be in the cache.
3172 */
Mingming Cao617ba132006-10-11 01:20:53 -07003173static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003174{
3175 struct inode *inode = mapping->host;
3176 journal_t *journal;
3177 int err;
3178
Alex Tomas64769242008-07-11 19:27:31 -04003179 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
3180 test_opt(inode->i_sb, DELALLOC)) {
3181 /*
3182 * With delalloc we want to sync the file
3183 * so that we can make sure we allocate
3184 * blocks for file
3185 */
3186 filemap_write_and_wait(mapping);
3187 }
3188
Frank Mayhar03901312009-01-07 00:06:22 -05003189 if (EXT4_JOURNAL(inode) && EXT4_I(inode)->i_state & EXT4_STATE_JDATA) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003190 /*
3191 * This is a REALLY heavyweight approach, but the use of
3192 * bmap on dirty files is expected to be extremely rare:
3193 * only if we run lilo or swapon on a freshly made file
3194 * do we expect this to happen.
3195 *
3196 * (bmap requires CAP_SYS_RAWIO so this does not
3197 * represent an unprivileged user DOS attack --- we'd be
3198 * in trouble if mortal users could trigger this path at
3199 * will.)
3200 *
Mingming Cao617ba132006-10-11 01:20:53 -07003201 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003202 * regular files. If somebody wants to bmap a directory
3203 * or symlink and gets confused because the buffer
3204 * hasn't yet been flushed to disk, they deserve
3205 * everything they get.
3206 */
3207
Mingming Cao617ba132006-10-11 01:20:53 -07003208 EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA;
3209 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07003210 jbd2_journal_lock_updates(journal);
3211 err = jbd2_journal_flush(journal);
3212 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003213
3214 if (err)
3215 return 0;
3216 }
3217
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003218 return generic_block_bmap(mapping, block, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003219}
3220
Mingming Cao617ba132006-10-11 01:20:53 -07003221static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003222{
Mingming Cao617ba132006-10-11 01:20:53 -07003223 return mpage_readpage(page, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003224}
3225
3226static int
Mingming Cao617ba132006-10-11 01:20:53 -07003227ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003228 struct list_head *pages, unsigned nr_pages)
3229{
Mingming Cao617ba132006-10-11 01:20:53 -07003230 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003231}
3232
Mingming Cao617ba132006-10-11 01:20:53 -07003233static void ext4_invalidatepage(struct page *page, unsigned long offset)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003234{
Mingming Cao617ba132006-10-11 01:20:53 -07003235 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003236
3237 /*
3238 * If it's a full truncate we just forget about the pending dirtying
3239 */
3240 if (offset == 0)
3241 ClearPageChecked(page);
3242
Frank Mayhar03901312009-01-07 00:06:22 -05003243 if (journal)
3244 jbd2_journal_invalidatepage(journal, page, offset);
3245 else
3246 block_invalidatepage(page, offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003247}
3248
Mingming Cao617ba132006-10-11 01:20:53 -07003249static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003250{
Mingming Cao617ba132006-10-11 01:20:53 -07003251 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003252
3253 WARN_ON(PageChecked(page));
3254 if (!page_has_buffers(page))
3255 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003256 if (journal)
3257 return jbd2_journal_try_to_free_buffers(journal, page, wait);
3258 else
3259 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003260}
3261
3262/*
3263 * If the O_DIRECT write will extend the file then add this inode to the
3264 * orphan list. So recovery will truncate it back to the original size
3265 * if the machine crashes during the write.
3266 *
3267 * If the O_DIRECT write is intantiating holes inside i_size and the machine
Jan Kara7fb54092008-02-10 01:08:38 -05003268 * crashes then stale disk data _may_ be exposed inside the file. But current
3269 * VFS code falls back into buffered path in that case so we are safe.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003270 */
Mingming Cao617ba132006-10-11 01:20:53 -07003271static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003272 const struct iovec *iov, loff_t offset,
3273 unsigned long nr_segs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003274{
3275 struct file *file = iocb->ki_filp;
3276 struct inode *inode = file->f_mapping->host;
Mingming Cao617ba132006-10-11 01:20:53 -07003277 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003278 handle_t *handle;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003279 ssize_t ret;
3280 int orphan = 0;
3281 size_t count = iov_length(iov, nr_segs);
3282
3283 if (rw == WRITE) {
3284 loff_t final_size = offset + count;
3285
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003286 if (final_size > inode->i_size) {
Jan Kara7fb54092008-02-10 01:08:38 -05003287 /* Credits for sb + inode write */
3288 handle = ext4_journal_start(inode, 2);
3289 if (IS_ERR(handle)) {
3290 ret = PTR_ERR(handle);
3291 goto out;
3292 }
Mingming Cao617ba132006-10-11 01:20:53 -07003293 ret = ext4_orphan_add(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003294 if (ret) {
3295 ext4_journal_stop(handle);
3296 goto out;
3297 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003298 orphan = 1;
3299 ei->i_disksize = inode->i_size;
Jan Kara7fb54092008-02-10 01:08:38 -05003300 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003301 }
3302 }
3303
3304 ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
3305 offset, nr_segs,
Mingming Cao617ba132006-10-11 01:20:53 -07003306 ext4_get_block, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003307
Jan Kara7fb54092008-02-10 01:08:38 -05003308 if (orphan) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003309 int err;
3310
Jan Kara7fb54092008-02-10 01:08:38 -05003311 /* Credits for sb + inode write */
3312 handle = ext4_journal_start(inode, 2);
3313 if (IS_ERR(handle)) {
3314 /* This is really bad luck. We've written the data
3315 * but cannot extend i_size. Bail out and pretend
3316 * the write failed... */
3317 ret = PTR_ERR(handle);
3318 goto out;
3319 }
3320 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003321 ext4_orphan_del(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003322 if (ret > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003323 loff_t end = offset + ret;
3324 if (end > inode->i_size) {
3325 ei->i_disksize = end;
3326 i_size_write(inode, end);
3327 /*
3328 * We're going to return a positive `ret'
3329 * here due to non-zero-length I/O, so there's
3330 * no way of reporting error returns from
Mingming Cao617ba132006-10-11 01:20:53 -07003331 * ext4_mark_inode_dirty() to userspace. So
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003332 * ignore it.
3333 */
Mingming Cao617ba132006-10-11 01:20:53 -07003334 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003335 }
3336 }
Mingming Cao617ba132006-10-11 01:20:53 -07003337 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003338 if (ret == 0)
3339 ret = err;
3340 }
3341out:
3342 return ret;
3343}
3344
3345/*
Mingming Cao617ba132006-10-11 01:20:53 -07003346 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003347 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3348 * much here because ->set_page_dirty is called under VFS locks. The page is
3349 * not necessarily locked.
3350 *
3351 * We cannot just dirty the page and leave attached buffers clean, because the
3352 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3353 * or jbddirty because all the journalling code will explode.
3354 *
3355 * So what we do is to mark the page "pending dirty" and next time writepage
3356 * is called, propagate that into the buffers appropriately.
3357 */
Mingming Cao617ba132006-10-11 01:20:53 -07003358static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003359{
3360 SetPageChecked(page);
3361 return __set_page_dirty_nobuffers(page);
3362}
3363
Mingming Cao617ba132006-10-11 01:20:53 -07003364static const struct address_space_operations ext4_ordered_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003365 .readpage = ext4_readpage,
3366 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003367 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003368 .sync_page = block_sync_page,
3369 .write_begin = ext4_write_begin,
3370 .write_end = ext4_ordered_write_end,
3371 .bmap = ext4_bmap,
3372 .invalidatepage = ext4_invalidatepage,
3373 .releasepage = ext4_releasepage,
3374 .direct_IO = ext4_direct_IO,
3375 .migratepage = buffer_migrate_page,
3376 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003377};
3378
Mingming Cao617ba132006-10-11 01:20:53 -07003379static const struct address_space_operations ext4_writeback_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003380 .readpage = ext4_readpage,
3381 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003382 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003383 .sync_page = block_sync_page,
3384 .write_begin = ext4_write_begin,
3385 .write_end = ext4_writeback_write_end,
3386 .bmap = ext4_bmap,
3387 .invalidatepage = ext4_invalidatepage,
3388 .releasepage = ext4_releasepage,
3389 .direct_IO = ext4_direct_IO,
3390 .migratepage = buffer_migrate_page,
3391 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003392};
3393
Mingming Cao617ba132006-10-11 01:20:53 -07003394static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003395 .readpage = ext4_readpage,
3396 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003397 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003398 .sync_page = block_sync_page,
3399 .write_begin = ext4_write_begin,
3400 .write_end = ext4_journalled_write_end,
3401 .set_page_dirty = ext4_journalled_set_page_dirty,
3402 .bmap = ext4_bmap,
3403 .invalidatepage = ext4_invalidatepage,
3404 .releasepage = ext4_releasepage,
3405 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003406};
3407
Alex Tomas64769242008-07-11 19:27:31 -04003408static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003409 .readpage = ext4_readpage,
3410 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003411 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003412 .writepages = ext4_da_writepages,
3413 .sync_page = block_sync_page,
3414 .write_begin = ext4_da_write_begin,
3415 .write_end = ext4_da_write_end,
3416 .bmap = ext4_bmap,
3417 .invalidatepage = ext4_da_invalidatepage,
3418 .releasepage = ext4_releasepage,
3419 .direct_IO = ext4_direct_IO,
3420 .migratepage = buffer_migrate_page,
3421 .is_partially_uptodate = block_is_partially_uptodate,
Alex Tomas64769242008-07-11 19:27:31 -04003422};
3423
Mingming Cao617ba132006-10-11 01:20:53 -07003424void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003425{
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04003426 if (ext4_should_order_data(inode) &&
3427 test_opt(inode->i_sb, DELALLOC))
3428 inode->i_mapping->a_ops = &ext4_da_aops;
3429 else if (ext4_should_order_data(inode))
Mingming Cao617ba132006-10-11 01:20:53 -07003430 inode->i_mapping->a_ops = &ext4_ordered_aops;
Alex Tomas64769242008-07-11 19:27:31 -04003431 else if (ext4_should_writeback_data(inode) &&
3432 test_opt(inode->i_sb, DELALLOC))
3433 inode->i_mapping->a_ops = &ext4_da_aops;
Mingming Cao617ba132006-10-11 01:20:53 -07003434 else if (ext4_should_writeback_data(inode))
3435 inode->i_mapping->a_ops = &ext4_writeback_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003436 else
Mingming Cao617ba132006-10-11 01:20:53 -07003437 inode->i_mapping->a_ops = &ext4_journalled_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003438}
3439
3440/*
Mingming Cao617ba132006-10-11 01:20:53 -07003441 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003442 * up to the end of the block which corresponds to `from'.
3443 * This required during truncate. We need to physically zero the tail end
3444 * of that block so it doesn't yield old data if the file is later grown.
3445 */
Jan Karacf108bc2008-07-11 19:27:31 -04003446int ext4_block_truncate_page(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003447 struct address_space *mapping, loff_t from)
3448{
Mingming Cao617ba132006-10-11 01:20:53 -07003449 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003450 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003451 unsigned blocksize, length, pos;
3452 ext4_lblk_t iblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003453 struct inode *inode = mapping->host;
3454 struct buffer_head *bh;
Jan Karacf108bc2008-07-11 19:27:31 -04003455 struct page *page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003456 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003457
Theodore Ts'of4a01012009-07-05 22:08:16 -04003458 page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
3459 mapping_gfp_mask(mapping) & ~__GFP_FS);
Jan Karacf108bc2008-07-11 19:27:31 -04003460 if (!page)
3461 return -EINVAL;
3462
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003463 blocksize = inode->i_sb->s_blocksize;
3464 length = blocksize - (offset & (blocksize - 1));
3465 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
3466
3467 /*
3468 * For "nobh" option, we can only work if we don't need to
3469 * read-in the page - otherwise we create buffers to do the IO.
3470 */
3471 if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) &&
Mingming Cao617ba132006-10-11 01:20:53 -07003472 ext4_should_writeback_data(inode) && PageUptodate(page)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003473 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003474 set_page_dirty(page);
3475 goto unlock;
3476 }
3477
3478 if (!page_has_buffers(page))
3479 create_empty_buffers(page, blocksize, 0);
3480
3481 /* Find the buffer that contains "offset" */
3482 bh = page_buffers(page);
3483 pos = blocksize;
3484 while (offset >= pos) {
3485 bh = bh->b_this_page;
3486 iblock++;
3487 pos += blocksize;
3488 }
3489
3490 err = 0;
3491 if (buffer_freed(bh)) {
3492 BUFFER_TRACE(bh, "freed: skip");
3493 goto unlock;
3494 }
3495
3496 if (!buffer_mapped(bh)) {
3497 BUFFER_TRACE(bh, "unmapped");
Mingming Cao617ba132006-10-11 01:20:53 -07003498 ext4_get_block(inode, iblock, bh, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003499 /* unmapped? It's a hole - nothing to do */
3500 if (!buffer_mapped(bh)) {
3501 BUFFER_TRACE(bh, "still unmapped");
3502 goto unlock;
3503 }
3504 }
3505
3506 /* Ok, it's mapped. Make sure it's up-to-date */
3507 if (PageUptodate(page))
3508 set_buffer_uptodate(bh);
3509
3510 if (!buffer_uptodate(bh)) {
3511 err = -EIO;
3512 ll_rw_block(READ, 1, &bh);
3513 wait_on_buffer(bh);
3514 /* Uhhuh. Read error. Complain and punt. */
3515 if (!buffer_uptodate(bh))
3516 goto unlock;
3517 }
3518
Mingming Cao617ba132006-10-11 01:20:53 -07003519 if (ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003520 BUFFER_TRACE(bh, "get write access");
Mingming Cao617ba132006-10-11 01:20:53 -07003521 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003522 if (err)
3523 goto unlock;
3524 }
3525
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003526 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003527
3528 BUFFER_TRACE(bh, "zeroed end of block");
3529
3530 err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003531 if (ext4_should_journal_data(inode)) {
Frank Mayhar03901312009-01-07 00:06:22 -05003532 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003533 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003534 if (ext4_should_order_data(inode))
Jan Kara678aaf42008-07-11 19:27:31 -04003535 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003536 mark_buffer_dirty(bh);
3537 }
3538
3539unlock:
3540 unlock_page(page);
3541 page_cache_release(page);
3542 return err;
3543}
3544
3545/*
3546 * Probably it should be a library function... search for first non-zero word
3547 * or memcmp with zero_page, whatever is better for particular architecture.
3548 * Linus?
3549 */
3550static inline int all_zeroes(__le32 *p, __le32 *q)
3551{
3552 while (p < q)
3553 if (*p++)
3554 return 0;
3555 return 1;
3556}
3557
3558/**
Mingming Cao617ba132006-10-11 01:20:53 -07003559 * ext4_find_shared - find the indirect blocks for partial truncation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003560 * @inode: inode in question
3561 * @depth: depth of the affected branch
Mingming Cao617ba132006-10-11 01:20:53 -07003562 * @offsets: offsets of pointers in that branch (see ext4_block_to_path)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003563 * @chain: place to store the pointers to partial indirect blocks
3564 * @top: place to the (detached) top of branch
3565 *
Mingming Cao617ba132006-10-11 01:20:53 -07003566 * This is a helper function used by ext4_truncate().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003567 *
3568 * When we do truncate() we may have to clean the ends of several
3569 * indirect blocks but leave the blocks themselves alive. Block is
3570 * partially truncated if some data below the new i_size is refered
3571 * from it (and it is on the path to the first completely truncated
3572 * data block, indeed). We have to free the top of that path along
3573 * with everything to the right of the path. Since no allocation
Mingming Cao617ba132006-10-11 01:20:53 -07003574 * past the truncation point is possible until ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003575 * finishes, we may safely do the latter, but top of branch may
3576 * require special attention - pageout below the truncation point
3577 * might try to populate it.
3578 *
3579 * We atomically detach the top of branch from the tree, store the
3580 * block number of its root in *@top, pointers to buffer_heads of
3581 * partially truncated blocks - in @chain[].bh and pointers to
3582 * their last elements that should not be removed - in
3583 * @chain[].p. Return value is the pointer to last filled element
3584 * of @chain.
3585 *
3586 * The work left to caller to do the actual freeing of subtrees:
3587 * a) free the subtree starting from *@top
3588 * b) free the subtrees whose roots are stored in
3589 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
3590 * c) free the subtrees growing from the inode past the @chain[0].
3591 * (no partially truncated stuff there). */
3592
Mingming Cao617ba132006-10-11 01:20:53 -07003593static Indirect *ext4_find_shared(struct inode *inode, int depth,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003594 ext4_lblk_t offsets[4], Indirect chain[4],
3595 __le32 *top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003596{
3597 Indirect *partial, *p;
3598 int k, err;
3599
3600 *top = 0;
3601 /* Make k index the deepest non-null offest + 1 */
3602 for (k = depth; k > 1 && !offsets[k-1]; k--)
3603 ;
Mingming Cao617ba132006-10-11 01:20:53 -07003604 partial = ext4_get_branch(inode, k, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003605 /* Writer: pointers */
3606 if (!partial)
3607 partial = chain + k-1;
3608 /*
3609 * If the branch acquired continuation since we've looked at it -
3610 * fine, it should all survive and (new) top doesn't belong to us.
3611 */
3612 if (!partial->key && *partial->p)
3613 /* Writer: end */
3614 goto no_top;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003615 for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003616 ;
3617 /*
3618 * OK, we've found the last block that must survive. The rest of our
3619 * branch should be detached before unlocking. However, if that rest
3620 * of branch is all ours and does not grow immediately from the inode
3621 * it's easier to cheat and just decrement partial->p.
3622 */
3623 if (p == chain + k - 1 && p > chain) {
3624 p->p--;
3625 } else {
3626 *top = *p->p;
Mingming Cao617ba132006-10-11 01:20:53 -07003627 /* Nope, don't do this in ext4. Must leave the tree intact */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003628#if 0
3629 *p->p = 0;
3630#endif
3631 }
3632 /* Writer: end */
3633
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003634 while (partial > p) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003635 brelse(partial->bh);
3636 partial--;
3637 }
3638no_top:
3639 return partial;
3640}
3641
3642/*
3643 * Zero a number of block pointers in either an inode or an indirect block.
3644 * If we restart the transaction we must again get write access to the
3645 * indirect block for further modification.
3646 *
3647 * We release `count' blocks on disk, but (last - first) may be greater
3648 * than `count' because there can be holes in there.
3649 */
Mingming Cao617ba132006-10-11 01:20:53 -07003650static void ext4_clear_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003651 struct buffer_head *bh,
3652 ext4_fsblk_t block_to_free,
3653 unsigned long count, __le32 *first,
3654 __le32 *last)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003655{
3656 __le32 *p;
3657 if (try_to_extend_transaction(handle, inode)) {
3658 if (bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05003659 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
3660 ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003661 }
Mingming Cao617ba132006-10-11 01:20:53 -07003662 ext4_mark_inode_dirty(handle, inode);
3663 ext4_journal_test_restart(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003664 if (bh) {
3665 BUFFER_TRACE(bh, "retaking write access");
Mingming Cao617ba132006-10-11 01:20:53 -07003666 ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003667 }
3668 }
3669
3670 /*
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003671 * Any buffers which are on the journal will be in memory. We
3672 * find them on the hash table so jbd2_journal_revoke() will
3673 * run jbd2_journal_forget() on them. We've already detached
3674 * each block from the file, so bforget() in
3675 * jbd2_journal_forget() should be safe.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003676 *
Mingming Caodab291a2006-10-11 01:21:01 -07003677 * AKPM: turn on bforget in jbd2_journal_forget()!!!
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003678 */
3679 for (p = first; p < last; p++) {
3680 u32 nr = le32_to_cpu(*p);
3681 if (nr) {
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003682 struct buffer_head *tbh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003683
3684 *p = 0;
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003685 tbh = sb_find_get_block(inode->i_sb, nr);
3686 ext4_forget(handle, 0, inode, tbh, nr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003687 }
3688 }
3689
Alex Tomasc9de5602008-01-29 00:19:52 -05003690 ext4_free_blocks(handle, inode, block_to_free, count, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003691}
3692
3693/**
Mingming Cao617ba132006-10-11 01:20:53 -07003694 * ext4_free_data - free a list of data blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003695 * @handle: handle for this transaction
3696 * @inode: inode we are dealing with
3697 * @this_bh: indirect buffer_head which contains *@first and *@last
3698 * @first: array of block numbers
3699 * @last: points immediately past the end of array
3700 *
3701 * We are freeing all blocks refered from that array (numbers are stored as
3702 * little-endian 32-bit) and updating @inode->i_blocks appropriately.
3703 *
3704 * We accumulate contiguous runs of blocks to free. Conveniently, if these
3705 * blocks are contiguous then releasing them at one time will only affect one
3706 * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
3707 * actually use a lot of journal space.
3708 *
3709 * @this_bh will be %NULL if @first and @last point into the inode's direct
3710 * block pointers.
3711 */
Mingming Cao617ba132006-10-11 01:20:53 -07003712static void ext4_free_data(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003713 struct buffer_head *this_bh,
3714 __le32 *first, __le32 *last)
3715{
Mingming Cao617ba132006-10-11 01:20:53 -07003716 ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003717 unsigned long count = 0; /* Number of blocks in the run */
3718 __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
3719 corresponding to
3720 block_to_free */
Mingming Cao617ba132006-10-11 01:20:53 -07003721 ext4_fsblk_t nr; /* Current block # */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003722 __le32 *p; /* Pointer into inode/ind
3723 for current block */
3724 int err;
3725
3726 if (this_bh) { /* For indirect block */
3727 BUFFER_TRACE(this_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003728 err = ext4_journal_get_write_access(handle, this_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003729 /* Important: if we can't update the indirect pointers
3730 * to the blocks, we can't free them. */
3731 if (err)
3732 return;
3733 }
3734
3735 for (p = first; p < last; p++) {
3736 nr = le32_to_cpu(*p);
3737 if (nr) {
3738 /* accumulate blocks to free if they're contiguous */
3739 if (count == 0) {
3740 block_to_free = nr;
3741 block_to_free_p = p;
3742 count = 1;
3743 } else if (nr == block_to_free + count) {
3744 count++;
3745 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003746 ext4_clear_blocks(handle, inode, this_bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003747 block_to_free,
3748 count, block_to_free_p, p);
3749 block_to_free = nr;
3750 block_to_free_p = p;
3751 count = 1;
3752 }
3753 }
3754 }
3755
3756 if (count > 0)
Mingming Cao617ba132006-10-11 01:20:53 -07003757 ext4_clear_blocks(handle, inode, this_bh, block_to_free,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003758 count, block_to_free_p, p);
3759
3760 if (this_bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05003761 BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata");
Duane Griffin71dc8fb2008-07-11 19:27:31 -04003762
3763 /*
3764 * The buffer head should have an attached journal head at this
3765 * point. However, if the data is corrupted and an indirect
3766 * block pointed to itself, it would have been detached when
3767 * the block was cleared. Check for this instead of OOPSing.
3768 */
Theodore Ts'oe7f07962009-01-20 09:50:19 -05003769 if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
Frank Mayhar03901312009-01-07 00:06:22 -05003770 ext4_handle_dirty_metadata(handle, inode, this_bh);
Duane Griffin71dc8fb2008-07-11 19:27:31 -04003771 else
3772 ext4_error(inode->i_sb, __func__,
3773 "circular indirect block detected, "
3774 "inode=%lu, block=%llu",
3775 inode->i_ino,
3776 (unsigned long long) this_bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003777 }
3778}
3779
3780/**
Mingming Cao617ba132006-10-11 01:20:53 -07003781 * ext4_free_branches - free an array of branches
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003782 * @handle: JBD handle for this transaction
3783 * @inode: inode we are dealing with
3784 * @parent_bh: the buffer_head which contains *@first and *@last
3785 * @first: array of block numbers
3786 * @last: pointer immediately past the end of array
3787 * @depth: depth of the branches to free
3788 *
3789 * We are freeing all blocks refered from these branches (numbers are
3790 * stored as little-endian 32-bit) and updating @inode->i_blocks
3791 * appropriately.
3792 */
Mingming Cao617ba132006-10-11 01:20:53 -07003793static void ext4_free_branches(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003794 struct buffer_head *parent_bh,
3795 __le32 *first, __le32 *last, int depth)
3796{
Mingming Cao617ba132006-10-11 01:20:53 -07003797 ext4_fsblk_t nr;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003798 __le32 *p;
3799
Frank Mayhar03901312009-01-07 00:06:22 -05003800 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003801 return;
3802
3803 if (depth--) {
3804 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07003805 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003806 p = last;
3807 while (--p >= first) {
3808 nr = le32_to_cpu(*p);
3809 if (!nr)
3810 continue; /* A hole */
3811
3812 /* Go read the buffer for the next level down */
3813 bh = sb_bread(inode->i_sb, nr);
3814
3815 /*
3816 * A read failure? Report error and clear slot
3817 * (should be rare).
3818 */
3819 if (!bh) {
Mingming Cao617ba132006-10-11 01:20:53 -07003820 ext4_error(inode->i_sb, "ext4_free_branches",
Mingming Cao2ae02102006-10-11 01:21:11 -07003821 "Read failure, inode=%lu, block=%llu",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003822 inode->i_ino, nr);
3823 continue;
3824 }
3825
3826 /* This zaps the entire block. Bottom up. */
3827 BUFFER_TRACE(bh, "free child branches");
Mingming Cao617ba132006-10-11 01:20:53 -07003828 ext4_free_branches(handle, inode, bh,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003829 (__le32 *) bh->b_data,
3830 (__le32 *) bh->b_data + addr_per_block,
3831 depth);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003832
3833 /*
3834 * We've probably journalled the indirect block several
3835 * times during the truncate. But it's no longer
3836 * needed and we now drop it from the transaction via
Mingming Caodab291a2006-10-11 01:21:01 -07003837 * jbd2_journal_revoke().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003838 *
3839 * That's easy if it's exclusively part of this
3840 * transaction. But if it's part of the committing
Mingming Caodab291a2006-10-11 01:21:01 -07003841 * transaction then jbd2_journal_forget() will simply
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003842 * brelse() it. That means that if the underlying
Mingming Cao617ba132006-10-11 01:20:53 -07003843 * block is reallocated in ext4_get_block(),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003844 * unmap_underlying_metadata() will find this block
3845 * and will try to get rid of it. damn, damn.
3846 *
3847 * If this block has already been committed to the
3848 * journal, a revoke record will be written. And
3849 * revoke records must be emitted *before* clearing
3850 * this block's bit in the bitmaps.
3851 */
Mingming Cao617ba132006-10-11 01:20:53 -07003852 ext4_forget(handle, 1, inode, bh, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003853
3854 /*
3855 * Everything below this this pointer has been
3856 * released. Now let this top-of-subtree go.
3857 *
3858 * We want the freeing of this indirect block to be
3859 * atomic in the journal with the updating of the
3860 * bitmap block which owns it. So make some room in
3861 * the journal.
3862 *
3863 * We zero the parent pointer *after* freeing its
3864 * pointee in the bitmaps, so if extend_transaction()
3865 * for some reason fails to put the bitmap changes and
3866 * the release into the same transaction, recovery
3867 * will merely complain about releasing a free block,
3868 * rather than leaking blocks.
3869 */
Frank Mayhar03901312009-01-07 00:06:22 -05003870 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003871 return;
3872 if (try_to_extend_transaction(handle, inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003873 ext4_mark_inode_dirty(handle, inode);
3874 ext4_journal_test_restart(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003875 }
3876
Alex Tomasc9de5602008-01-29 00:19:52 -05003877 ext4_free_blocks(handle, inode, nr, 1, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003878
3879 if (parent_bh) {
3880 /*
3881 * The block which we have just freed is
3882 * pointed to by an indirect block: journal it
3883 */
3884 BUFFER_TRACE(parent_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003885 if (!ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003886 parent_bh)){
3887 *p = 0;
3888 BUFFER_TRACE(parent_bh,
Frank Mayhar03901312009-01-07 00:06:22 -05003889 "call ext4_handle_dirty_metadata");
3890 ext4_handle_dirty_metadata(handle,
3891 inode,
3892 parent_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003893 }
3894 }
3895 }
3896 } else {
3897 /* We have reached the bottom of the tree. */
3898 BUFFER_TRACE(parent_bh, "free data blocks");
Mingming Cao617ba132006-10-11 01:20:53 -07003899 ext4_free_data(handle, inode, parent_bh, first, last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003900 }
3901}
3902
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003903int ext4_can_truncate(struct inode *inode)
3904{
3905 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3906 return 0;
3907 if (S_ISREG(inode->i_mode))
3908 return 1;
3909 if (S_ISDIR(inode->i_mode))
3910 return 1;
3911 if (S_ISLNK(inode->i_mode))
3912 return !ext4_inode_is_fast_symlink(inode);
3913 return 0;
3914}
3915
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003916/*
Mingming Cao617ba132006-10-11 01:20:53 -07003917 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003918 *
Mingming Cao617ba132006-10-11 01:20:53 -07003919 * We block out ext4_get_block() block instantiations across the entire
3920 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003921 * simultaneously on behalf of the same inode.
3922 *
3923 * As we work through the truncate and commmit bits of it to the journal there
3924 * is one core, guiding principle: the file's tree must always be consistent on
3925 * disk. We must be able to restart the truncate after a crash.
3926 *
3927 * The file's tree may be transiently inconsistent in memory (although it
3928 * probably isn't), but whenever we close off and commit a journal transaction,
3929 * the contents of (the filesystem + the journal) must be consistent and
3930 * restartable. It's pretty simple, really: bottom up, right to left (although
3931 * left-to-right works OK too).
3932 *
3933 * Note that at recovery time, journal replay occurs *before* the restart of
3934 * truncate against the orphan inode list.
3935 *
3936 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07003937 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003938 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07003939 * ext4_truncate() to have another go. So there will be instantiated blocks
3940 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003941 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07003942 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003943 */
Mingming Cao617ba132006-10-11 01:20:53 -07003944void ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003945{
3946 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -07003947 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003948 __le32 *i_data = ei->i_data;
Mingming Cao617ba132006-10-11 01:20:53 -07003949 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003950 struct address_space *mapping = inode->i_mapping;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003951 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003952 Indirect chain[4];
3953 Indirect *partial;
3954 __le32 nr = 0;
3955 int n;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003956 ext4_lblk_t last_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003957 unsigned blocksize = inode->i_sb->s_blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003958
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003959 if (!ext4_can_truncate(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003960 return;
3961
Theodore Ts'o0eab9282009-06-09 09:54:40 -04003962 if (ei->i_disksize && inode->i_size == 0 &&
3963 !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o7d8f9f72009-02-24 08:21:14 -05003964 ei->i_state |= EXT4_STATE_DA_ALLOC_CLOSE;
3965
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003966 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
Jan Karacf108bc2008-07-11 19:27:31 -04003967 ext4_ext_truncate(inode);
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003968 return;
3969 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003970
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003971 handle = start_transaction(inode);
Jan Karacf108bc2008-07-11 19:27:31 -04003972 if (IS_ERR(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003973 return; /* AKPM: return what? */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003974
3975 last_block = (inode->i_size + blocksize-1)
Mingming Cao617ba132006-10-11 01:20:53 -07003976 >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003977
Jan Karacf108bc2008-07-11 19:27:31 -04003978 if (inode->i_size & (blocksize - 1))
3979 if (ext4_block_truncate_page(handle, mapping, inode->i_size))
3980 goto out_stop;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003981
Mingming Cao617ba132006-10-11 01:20:53 -07003982 n = ext4_block_to_path(inode, last_block, offsets, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003983 if (n == 0)
3984 goto out_stop; /* error */
3985
3986 /*
3987 * OK. This truncate is going to happen. We add the inode to the
3988 * orphan list, so that if this truncate spans multiple transactions,
3989 * and we crash, we will resume the truncate when the filesystem
3990 * recovers. It also marks the inode dirty, to catch the new size.
3991 *
3992 * Implication: the file must always be in a sane, consistent
3993 * truncatable state while each transaction commits.
3994 */
Mingming Cao617ba132006-10-11 01:20:53 -07003995 if (ext4_orphan_add(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003996 goto out_stop;
3997
3998 /*
Mingming Cao632eaea2008-07-11 19:27:31 -04003999 * From here we block out all ext4_get_block() callers who want to
4000 * modify the block allocation tree.
4001 */
4002 down_write(&ei->i_data_sem);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04004003
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004004 ext4_discard_preallocations(inode);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04004005
Mingming Cao632eaea2008-07-11 19:27:31 -04004006 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004007 * The orphan list entry will now protect us from any crash which
4008 * occurs before the truncate completes, so it is now safe to propagate
4009 * the new, shorter inode size (held for now in i_size) into the
4010 * on-disk inode. We do this via i_disksize, which is the value which
Mingming Cao617ba132006-10-11 01:20:53 -07004011 * ext4 *really* writes onto the disk inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004012 */
4013 ei->i_disksize = inode->i_size;
4014
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004015 if (n == 1) { /* direct blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07004016 ext4_free_data(handle, inode, NULL, i_data+offsets[0],
4017 i_data + EXT4_NDIR_BLOCKS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004018 goto do_indirects;
4019 }
4020
Mingming Cao617ba132006-10-11 01:20:53 -07004021 partial = ext4_find_shared(inode, n, offsets, chain, &nr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004022 /* Kill the top of shared branch (not detached) */
4023 if (nr) {
4024 if (partial == chain) {
4025 /* Shared branch grows from the inode */
Mingming Cao617ba132006-10-11 01:20:53 -07004026 ext4_free_branches(handle, inode, NULL,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004027 &nr, &nr+1, (chain+n-1) - partial);
4028 *partial->p = 0;
4029 /*
4030 * We mark the inode dirty prior to restart,
4031 * and prior to stop. No need for it here.
4032 */
4033 } else {
4034 /* Shared branch grows from an indirect block */
4035 BUFFER_TRACE(partial->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07004036 ext4_free_branches(handle, inode, partial->bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004037 partial->p,
4038 partial->p+1, (chain+n-1) - partial);
4039 }
4040 }
4041 /* Clear the ends of indirect blocks on the shared branch */
4042 while (partial > chain) {
Mingming Cao617ba132006-10-11 01:20:53 -07004043 ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004044 (__le32*)partial->bh->b_data+addr_per_block,
4045 (chain+n-1) - partial);
4046 BUFFER_TRACE(partial->bh, "call brelse");
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004047 brelse(partial->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004048 partial--;
4049 }
4050do_indirects:
4051 /* Kill the remaining (whole) subtrees */
4052 switch (offsets[0]) {
4053 default:
Mingming Cao617ba132006-10-11 01:20:53 -07004054 nr = i_data[EXT4_IND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004055 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004056 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
4057 i_data[EXT4_IND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004058 }
Mingming Cao617ba132006-10-11 01:20:53 -07004059 case EXT4_IND_BLOCK:
4060 nr = i_data[EXT4_DIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004061 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004062 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
4063 i_data[EXT4_DIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004064 }
Mingming Cao617ba132006-10-11 01:20:53 -07004065 case EXT4_DIND_BLOCK:
4066 nr = i_data[EXT4_TIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004067 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004068 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
4069 i_data[EXT4_TIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004070 }
Mingming Cao617ba132006-10-11 01:20:53 -07004071 case EXT4_TIND_BLOCK:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004072 ;
4073 }
4074
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004075 up_write(&ei->i_data_sem);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004076 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07004077 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004078
4079 /*
4080 * In a multi-transaction truncate, we only make the final transaction
4081 * synchronous
4082 */
4083 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05004084 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004085out_stop:
4086 /*
4087 * If this was a simple ftruncate(), and the file will remain alive
4088 * then we need to clear up the orphan record which we created above.
4089 * However, if this was a real unlink then we were called by
Mingming Cao617ba132006-10-11 01:20:53 -07004090 * ext4_delete_inode(), and we allow that function to clean up the
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004091 * orphan info for us.
4092 */
4093 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07004094 ext4_orphan_del(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004095
Mingming Cao617ba132006-10-11 01:20:53 -07004096 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004097}
4098
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004099/*
Mingming Cao617ba132006-10-11 01:20:53 -07004100 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004101 * underlying buffer_head on success. If 'in_mem' is true, we have all
4102 * data in memory that is needed to recreate the on-disk version of this
4103 * inode.
4104 */
Mingming Cao617ba132006-10-11 01:20:53 -07004105static int __ext4_get_inode_loc(struct inode *inode,
4106 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004107{
Theodore Ts'o240799c2008-10-09 23:53:47 -04004108 struct ext4_group_desc *gdp;
4109 struct buffer_head *bh;
4110 struct super_block *sb = inode->i_sb;
4111 ext4_fsblk_t block;
4112 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004113
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004114 iloc->bh = NULL;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004115 if (!ext4_valid_inum(sb, inode->i_ino))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004116 return -EIO;
4117
Theodore Ts'o240799c2008-10-09 23:53:47 -04004118 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
4119 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
4120 if (!gdp)
4121 return -EIO;
4122
4123 /*
4124 * Figure out the offset within the block group inode table
4125 */
4126 inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb));
4127 inode_offset = ((inode->i_ino - 1) %
4128 EXT4_INODES_PER_GROUP(sb));
4129 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4130 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4131
4132 bh = sb_getblk(sb, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004133 if (!bh) {
Theodore Ts'o240799c2008-10-09 23:53:47 -04004134 ext4_error(sb, "ext4_get_inode_loc", "unable to read "
4135 "inode block - inode=%lu, block=%llu",
4136 inode->i_ino, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004137 return -EIO;
4138 }
4139 if (!buffer_uptodate(bh)) {
4140 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04004141
4142 /*
4143 * If the buffer has the write error flag, we have failed
4144 * to write out another inode in the same block. In this
4145 * case, we don't have to read the block because we may
4146 * read the old inode data successfully.
4147 */
4148 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
4149 set_buffer_uptodate(bh);
4150
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004151 if (buffer_uptodate(bh)) {
4152 /* someone brought it uptodate while we waited */
4153 unlock_buffer(bh);
4154 goto has_buffer;
4155 }
4156
4157 /*
4158 * If we have all information of the inode in memory and this
4159 * is the only valid inode in the block, we need not read the
4160 * block.
4161 */
4162 if (in_mem) {
4163 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004164 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004165
Theodore Ts'o240799c2008-10-09 23:53:47 -04004166 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004167
4168 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004169 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004170 if (!bitmap_bh)
4171 goto make_io;
4172
4173 /*
4174 * If the inode bitmap isn't in cache then the
4175 * optimisation may end up performing two reads instead
4176 * of one, so skip it.
4177 */
4178 if (!buffer_uptodate(bitmap_bh)) {
4179 brelse(bitmap_bh);
4180 goto make_io;
4181 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04004182 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004183 if (i == inode_offset)
4184 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07004185 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004186 break;
4187 }
4188 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004189 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004190 /* all other inodes are free, so skip I/O */
4191 memset(bh->b_data, 0, bh->b_size);
4192 set_buffer_uptodate(bh);
4193 unlock_buffer(bh);
4194 goto has_buffer;
4195 }
4196 }
4197
4198make_io:
4199 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04004200 * If we need to do any I/O, try to pre-readahead extra
4201 * blocks from the inode table.
4202 */
4203 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4204 ext4_fsblk_t b, end, table;
4205 unsigned num;
4206
4207 table = ext4_inode_table(sb, gdp);
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004208 /* s_inode_readahead_blks is always a power of 2 */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004209 b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
4210 if (table > b)
4211 b = table;
4212 end = b + EXT4_SB(sb)->s_inode_readahead_blks;
4213 num = EXT4_INODES_PER_GROUP(sb);
4214 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4215 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004216 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004217 table += num / inodes_per_block;
4218 if (end > table)
4219 end = table;
4220 while (b <= end)
4221 sb_breadahead(sb, b++);
4222 }
4223
4224 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004225 * There are other valid inodes in the buffer, this inode
4226 * has in-inode xattrs, or we don't have this inode in memory.
4227 * Read the block from disk.
4228 */
4229 get_bh(bh);
4230 bh->b_end_io = end_buffer_read_sync;
4231 submit_bh(READ_META, bh);
4232 wait_on_buffer(bh);
4233 if (!buffer_uptodate(bh)) {
Theodore Ts'o240799c2008-10-09 23:53:47 -04004234 ext4_error(sb, __func__,
4235 "unable to read inode block - inode=%lu, "
4236 "block=%llu", inode->i_ino, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004237 brelse(bh);
4238 return -EIO;
4239 }
4240 }
4241has_buffer:
4242 iloc->bh = bh;
4243 return 0;
4244}
4245
Mingming Cao617ba132006-10-11 01:20:53 -07004246int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004247{
4248 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07004249 return __ext4_get_inode_loc(inode, iloc,
4250 !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004251}
4252
Mingming Cao617ba132006-10-11 01:20:53 -07004253void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004254{
Mingming Cao617ba132006-10-11 01:20:53 -07004255 unsigned int flags = EXT4_I(inode)->i_flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004256
4257 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
Mingming Cao617ba132006-10-11 01:20:53 -07004258 if (flags & EXT4_SYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004259 inode->i_flags |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07004260 if (flags & EXT4_APPEND_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004261 inode->i_flags |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07004262 if (flags & EXT4_IMMUTABLE_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004263 inode->i_flags |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07004264 if (flags & EXT4_NOATIME_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004265 inode->i_flags |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004266 if (flags & EXT4_DIRSYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004267 inode->i_flags |= S_DIRSYNC;
4268}
4269
Jan Karaff9ddf72007-07-18 09:24:20 -04004270/* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4271void ext4_get_inode_flags(struct ext4_inode_info *ei)
4272{
4273 unsigned int flags = ei->vfs_inode.i_flags;
4274
4275 ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
4276 EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL);
4277 if (flags & S_SYNC)
4278 ei->i_flags |= EXT4_SYNC_FL;
4279 if (flags & S_APPEND)
4280 ei->i_flags |= EXT4_APPEND_FL;
4281 if (flags & S_IMMUTABLE)
4282 ei->i_flags |= EXT4_IMMUTABLE_FL;
4283 if (flags & S_NOATIME)
4284 ei->i_flags |= EXT4_NOATIME_FL;
4285 if (flags & S_DIRSYNC)
4286 ei->i_flags |= EXT4_DIRSYNC_FL;
4287}
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004288
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004289static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004290 struct ext4_inode_info *ei)
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004291{
4292 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004293 struct inode *inode = &(ei->vfs_inode);
4294 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004295
4296 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4297 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
4298 /* we are using combined 48 bit field */
4299 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4300 le32_to_cpu(raw_inode->i_blocks_lo);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004301 if (ei->i_flags & EXT4_HUGE_FILE_FL) {
4302 /* i_blocks represent file system block size */
4303 return i_blocks << (inode->i_blkbits - 9);
4304 } else {
4305 return i_blocks;
4306 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004307 } else {
4308 return le32_to_cpu(raw_inode->i_blocks_lo);
4309 }
4310}
Jan Karaff9ddf72007-07-18 09:24:20 -04004311
David Howells1d1fe1e2008-02-07 00:15:37 -08004312struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004313{
Mingming Cao617ba132006-10-11 01:20:53 -07004314 struct ext4_iloc iloc;
4315 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004316 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004317 struct buffer_head *bh;
David Howells1d1fe1e2008-02-07 00:15:37 -08004318 struct inode *inode;
4319 long ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004320 int block;
4321
David Howells1d1fe1e2008-02-07 00:15:37 -08004322 inode = iget_locked(sb, ino);
4323 if (!inode)
4324 return ERR_PTR(-ENOMEM);
4325 if (!(inode->i_state & I_NEW))
4326 return inode;
4327
4328 ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004329
David Howells1d1fe1e2008-02-07 00:15:37 -08004330 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4331 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004332 goto bad_inode;
4333 bh = iloc.bh;
Mingming Cao617ba132006-10-11 01:20:53 -07004334 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004335 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
4336 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4337 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004338 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004339 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4340 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
4341 }
4342 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004343
4344 ei->i_state = 0;
4345 ei->i_dir_start_lookup = 0;
4346 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4347 /* We now have enough fields to check if the inode was active or not.
4348 * This is needed because nfsd might try to access dead inodes
4349 * the test is that same one that e2fsck uses
4350 * NeilBrown 1999oct15
4351 */
4352 if (inode->i_nlink == 0) {
4353 if (inode->i_mode == 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07004354 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004355 /* this inode is deleted */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004356 brelse(bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004357 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004358 goto bad_inode;
4359 }
4360 /* The only unlinked inodes we let through here have
4361 * valid i_mode and are being read by the orphan
4362 * recovery code: that's fine, we're about to complete
4363 * the process of deleting those. */
4364 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004365 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004366 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004367 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Theodore Ts'oa9e81742009-04-24 16:11:18 -04004368 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004369 ei->i_file_acl |=
4370 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004371 inode->i_size = ext4_isize(raw_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004372 ei->i_disksize = inode->i_size;
4373 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4374 ei->i_block_group = iloc.block_group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04004375 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004376 /*
4377 * NOTE! The in-memory inode i_data array is in little-endian order
4378 * even on big-endian machines: we do NOT byteswap the block numbers!
4379 */
Mingming Cao617ba132006-10-11 01:20:53 -07004380 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004381 ei->i_data[block] = raw_inode->i_block[block];
4382 INIT_LIST_HEAD(&ei->i_orphan);
4383
Eric Sandeen0040d982008-02-05 22:36:43 -05004384 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004385 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
Mingming Cao617ba132006-10-11 01:20:53 -07004386 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004387 EXT4_INODE_SIZE(inode->i_sb)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004388 brelse(bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004389 ret = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004390 goto bad_inode;
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004391 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004392 if (ei->i_extra_isize == 0) {
4393 /* The extra space is currently unused. Use it. */
Mingming Cao617ba132006-10-11 01:20:53 -07004394 ei->i_extra_isize = sizeof(struct ext4_inode) -
4395 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004396 } else {
4397 __le32 *magic = (void *)raw_inode +
Mingming Cao617ba132006-10-11 01:20:53 -07004398 EXT4_GOOD_OLD_INODE_SIZE +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004399 ei->i_extra_isize;
Mingming Cao617ba132006-10-11 01:20:53 -07004400 if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC))
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004401 ei->i_state |= EXT4_STATE_XATTR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004402 }
4403 } else
4404 ei->i_extra_isize = 0;
4405
Kalpak Shahef7f3832007-07-18 09:15:20 -04004406 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4407 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4408 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4409 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4410
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004411 inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
4412 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4413 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4414 inode->i_version |=
4415 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4416 }
4417
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004418 ret = 0;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004419 if (ei->i_file_acl &&
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004420 ((ei->i_file_acl <
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004421 (le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) +
4422 EXT4_SB(sb)->s_gdb_count)) ||
4423 (ei->i_file_acl >= ext4_blocks_count(EXT4_SB(sb)->s_es)))) {
4424 ext4_error(sb, __func__,
4425 "bad extended attribute block %llu in inode #%lu",
4426 ei->i_file_acl, inode->i_ino);
4427 ret = -EIO;
4428 goto bad_inode;
4429 } else if (ei->i_flags & EXT4_EXTENTS_FL) {
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004430 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4431 (S_ISLNK(inode->i_mode) &&
4432 !ext4_inode_is_fast_symlink(inode)))
4433 /* Validate extent which is part of inode */
4434 ret = ext4_ext_check_inode(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004435 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004436 (S_ISLNK(inode->i_mode) &&
4437 !ext4_inode_is_fast_symlink(inode))) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004438 /* Validate block references which are part of inode */
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004439 ret = ext4_check_inode_blockref(inode);
4440 }
4441 if (ret) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004442 brelse(bh);
4443 goto bad_inode;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -04004444 }
4445
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004446 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004447 inode->i_op = &ext4_file_inode_operations;
4448 inode->i_fop = &ext4_file_operations;
4449 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004450 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004451 inode->i_op = &ext4_dir_inode_operations;
4452 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004453 } else if (S_ISLNK(inode->i_mode)) {
Duane Griffine83c1392008-12-19 20:47:15 +00004454 if (ext4_inode_is_fast_symlink(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004455 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00004456 nd_terminate_link(ei->i_data, inode->i_size,
4457 sizeof(ei->i_data) - 1);
4458 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004459 inode->i_op = &ext4_symlink_inode_operations;
4460 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004461 }
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004462 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4463 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004464 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004465 if (raw_inode->i_block[0])
4466 init_special_inode(inode, inode->i_mode,
4467 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4468 else
4469 init_special_inode(inode, inode->i_mode,
4470 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004471 } else {
4472 brelse(bh);
4473 ret = -EIO;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004474 ext4_error(inode->i_sb, __func__,
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004475 "bogus i_mode (%o) for inode=%lu",
4476 inode->i_mode, inode->i_ino);
4477 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004478 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004479 brelse(iloc.bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004480 ext4_set_inode_flags(inode);
David Howells1d1fe1e2008-02-07 00:15:37 -08004481 unlock_new_inode(inode);
4482 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004483
4484bad_inode:
David Howells1d1fe1e2008-02-07 00:15:37 -08004485 iget_failed(inode);
4486 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004487}
4488
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004489static int ext4_inode_blocks_set(handle_t *handle,
4490 struct ext4_inode *raw_inode,
4491 struct ext4_inode_info *ei)
4492{
4493 struct inode *inode = &(ei->vfs_inode);
4494 u64 i_blocks = inode->i_blocks;
4495 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004496
4497 if (i_blocks <= ~0U) {
4498 /*
4499 * i_blocks can be represnted in a 32 bit variable
4500 * as multiple of 512 bytes
4501 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004502 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004503 raw_inode->i_blocks_high = 0;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004504 ei->i_flags &= ~EXT4_HUGE_FILE_FL;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004505 return 0;
4506 }
4507 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
4508 return -EFBIG;
4509
4510 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004511 /*
4512 * i_blocks can be represented in a 48 bit variable
4513 * as multiple of 512 bytes
4514 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004515 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004516 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004517 ei->i_flags &= ~EXT4_HUGE_FILE_FL;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004518 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004519 ei->i_flags |= EXT4_HUGE_FILE_FL;
4520 /* i_block is stored in file system block size */
4521 i_blocks = i_blocks >> (inode->i_blkbits - 9);
4522 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4523 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004524 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004525 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004526}
4527
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004528/*
4529 * Post the struct inode info into an on-disk inode location in the
4530 * buffer-cache. This gobbles the caller's reference to the
4531 * buffer_head in the inode location struct.
4532 *
4533 * The caller must have write access to iloc->bh.
4534 */
Mingming Cao617ba132006-10-11 01:20:53 -07004535static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004536 struct inode *inode,
Mingming Cao617ba132006-10-11 01:20:53 -07004537 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004538{
Mingming Cao617ba132006-10-11 01:20:53 -07004539 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4540 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004541 struct buffer_head *bh = iloc->bh;
4542 int err = 0, rc, block;
4543
4544 /* For fields not not tracking in the in-memory inode,
4545 * initialise them to zero for new inodes. */
Mingming Cao617ba132006-10-11 01:20:53 -07004546 if (ei->i_state & EXT4_STATE_NEW)
4547 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004548
Jan Karaff9ddf72007-07-18 09:24:20 -04004549 ext4_get_inode_flags(ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004550 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004551 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004552 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
4553 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
4554/*
4555 * Fix up interoperability with old kernels. Otherwise, old inodes get
4556 * re-used with the upper 16 bits of the uid/gid intact
4557 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004558 if (!ei->i_dtime) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004559 raw_inode->i_uid_high =
4560 cpu_to_le16(high_16_bits(inode->i_uid));
4561 raw_inode->i_gid_high =
4562 cpu_to_le16(high_16_bits(inode->i_gid));
4563 } else {
4564 raw_inode->i_uid_high = 0;
4565 raw_inode->i_gid_high = 0;
4566 }
4567 } else {
4568 raw_inode->i_uid_low =
4569 cpu_to_le16(fs_high2lowuid(inode->i_uid));
4570 raw_inode->i_gid_low =
4571 cpu_to_le16(fs_high2lowgid(inode->i_gid));
4572 raw_inode->i_uid_high = 0;
4573 raw_inode->i_gid_high = 0;
4574 }
4575 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004576
4577 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4578 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4579 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4580 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4581
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004582 if (ext4_inode_blocks_set(handle, raw_inode, ei))
4583 goto out_brelse;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004584 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04004585 /* clear the migrate flag in the raw_inode */
4586 raw_inode->i_flags = cpu_to_le32(ei->i_flags & ~EXT4_EXT_MIGRATE);
Mingming Cao9b8f1f02006-10-11 01:21:13 -07004587 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
4588 cpu_to_le32(EXT4_OS_HURD))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004589 raw_inode->i_file_acl_high =
4590 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004591 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004592 ext4_isize_set(raw_inode, ei->i_disksize);
4593 if (ei->i_disksize > 0x7fffffffULL) {
4594 struct super_block *sb = inode->i_sb;
4595 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
4596 EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
4597 EXT4_SB(sb)->s_es->s_rev_level ==
4598 cpu_to_le32(EXT4_GOOD_OLD_REV)) {
4599 /* If this is the first large file
4600 * created, add a flag to the superblock.
4601 */
4602 err = ext4_journal_get_write_access(handle,
4603 EXT4_SB(sb)->s_sbh);
4604 if (err)
4605 goto out_brelse;
4606 ext4_update_dynamic_rev(sb);
4607 EXT4_SET_RO_COMPAT_FEATURE(sb,
Mingming Cao617ba132006-10-11 01:20:53 -07004608 EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004609 sb->s_dirt = 1;
Frank Mayhar03901312009-01-07 00:06:22 -05004610 ext4_handle_sync(handle);
4611 err = ext4_handle_dirty_metadata(handle, inode,
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004612 EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004613 }
4614 }
4615 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
4616 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4617 if (old_valid_dev(inode->i_rdev)) {
4618 raw_inode->i_block[0] =
4619 cpu_to_le32(old_encode_dev(inode->i_rdev));
4620 raw_inode->i_block[1] = 0;
4621 } else {
4622 raw_inode->i_block[0] = 0;
4623 raw_inode->i_block[1] =
4624 cpu_to_le32(new_encode_dev(inode->i_rdev));
4625 raw_inode->i_block[2] = 0;
4626 }
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004627 } else
4628 for (block = 0; block < EXT4_N_BLOCKS; block++)
4629 raw_inode->i_block[block] = ei->i_data[block];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004630
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004631 raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
4632 if (ei->i_extra_isize) {
4633 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4634 raw_inode->i_version_hi =
4635 cpu_to_le32(inode->i_version >> 32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004636 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004637 }
4638
Frank Mayhar03901312009-01-07 00:06:22 -05004639 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
4640 rc = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004641 if (!err)
4642 err = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07004643 ei->i_state &= ~EXT4_STATE_NEW;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004644
4645out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004646 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004647 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004648 return err;
4649}
4650
4651/*
Mingming Cao617ba132006-10-11 01:20:53 -07004652 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004653 *
4654 * We are called from a few places:
4655 *
4656 * - Within generic_file_write() for O_SYNC files.
4657 * Here, there will be no transaction running. We wait for any running
4658 * trasnaction to commit.
4659 *
4660 * - Within sys_sync(), kupdate and such.
4661 * We wait on commit, if tol to.
4662 *
4663 * - Within prune_icache() (PF_MEMALLOC == true)
4664 * Here we simply return. We can't afford to block kswapd on the
4665 * journal commit.
4666 *
4667 * In all cases it is actually safe for us to return without doing anything,
4668 * because the inode has been copied into a raw inode buffer in
Mingming Cao617ba132006-10-11 01:20:53 -07004669 * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004670 * knfsd.
4671 *
4672 * Note that we are absolutely dependent upon all inode dirtiers doing the
4673 * right thing: they *must* call mark_inode_dirty() after dirtying info in
4674 * which we are interested.
4675 *
4676 * It would be a bug for them to not do this. The code:
4677 *
4678 * mark_inode_dirty(inode)
4679 * stuff();
4680 * inode->i_size = expr;
4681 *
4682 * is in error because a kswapd-driven write_inode() could occur while
4683 * `stuff()' is running, and the new i_size will be lost. Plus the inode
4684 * will no longer be on the superblock's dirty inode list.
4685 */
Mingming Cao617ba132006-10-11 01:20:53 -07004686int ext4_write_inode(struct inode *inode, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004687{
4688 if (current->flags & PF_MEMALLOC)
4689 return 0;
4690
Mingming Cao617ba132006-10-11 01:20:53 -07004691 if (ext4_journal_current_handle()) {
Mingming Caob38bd332007-07-19 01:48:35 -07004692 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004693 dump_stack();
4694 return -EIO;
4695 }
4696
4697 if (!wait)
4698 return 0;
4699
Mingming Cao617ba132006-10-11 01:20:53 -07004700 return ext4_force_commit(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004701}
4702
4703/*
Mingming Cao617ba132006-10-11 01:20:53 -07004704 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004705 *
4706 * Called from notify_change.
4707 *
4708 * We want to trap VFS attempts to truncate the file as soon as
4709 * possible. In particular, we want to make sure that when the VFS
4710 * shrinks i_size, we put the inode on the orphan list and modify
4711 * i_disksize immediately, so that during the subsequent flushing of
4712 * dirty pages and freeing of disk blocks, we can guarantee that any
4713 * commit will leave the blocks being flushed in an unused state on
4714 * disk. (On recovery, the inode will get truncated and the blocks will
4715 * be freed, so we have a strong guarantee that no future commit will
4716 * leave these blocks visible to the user.)
4717 *
Jan Kara678aaf42008-07-11 19:27:31 -04004718 * Another thing we have to assure is that if we are in ordered mode
4719 * and inode is still attached to the committing transaction, we must
4720 * we start writeout of all the dirty pages which are being truncated.
4721 * This way we are sure that all the data written in the previous
4722 * transaction are already on disk (truncate waits for pages under
4723 * writeback).
4724 *
4725 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004726 */
Mingming Cao617ba132006-10-11 01:20:53 -07004727int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004728{
4729 struct inode *inode = dentry->d_inode;
4730 int error, rc = 0;
4731 const unsigned int ia_valid = attr->ia_valid;
4732
4733 error = inode_change_ok(inode, attr);
4734 if (error)
4735 return error;
4736
4737 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
4738 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
4739 handle_t *handle;
4740
4741 /* (user+group)*(old+new) structure, inode write (sb,
4742 * inode block, ? - but truncate inode update has it) */
Mingming Cao617ba132006-10-11 01:20:53 -07004743 handle = ext4_journal_start(inode, 2*(EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)+
4744 EXT4_QUOTA_DEL_BLOCKS(inode->i_sb))+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004745 if (IS_ERR(handle)) {
4746 error = PTR_ERR(handle);
4747 goto err_out;
4748 }
Jan Karaa269eb12009-01-26 17:04:39 +01004749 error = vfs_dq_transfer(inode, attr) ? -EDQUOT : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004750 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07004751 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004752 return error;
4753 }
4754 /* Update corresponding info in inode so that everything is in
4755 * one transaction */
4756 if (attr->ia_valid & ATTR_UID)
4757 inode->i_uid = attr->ia_uid;
4758 if (attr->ia_valid & ATTR_GID)
4759 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07004760 error = ext4_mark_inode_dirty(handle, inode);
4761 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004762 }
4763
Eric Sandeene2b46572008-01-28 23:58:27 -05004764 if (attr->ia_valid & ATTR_SIZE) {
4765 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) {
4766 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4767
4768 if (attr->ia_size > sbi->s_bitmap_maxbytes) {
4769 error = -EFBIG;
4770 goto err_out;
4771 }
4772 }
4773 }
4774
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004775 if (S_ISREG(inode->i_mode) &&
4776 attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
4777 handle_t *handle;
4778
Mingming Cao617ba132006-10-11 01:20:53 -07004779 handle = ext4_journal_start(inode, 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004780 if (IS_ERR(handle)) {
4781 error = PTR_ERR(handle);
4782 goto err_out;
4783 }
4784
Mingming Cao617ba132006-10-11 01:20:53 -07004785 error = ext4_orphan_add(handle, inode);
4786 EXT4_I(inode)->i_disksize = attr->ia_size;
4787 rc = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004788 if (!error)
4789 error = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07004790 ext4_journal_stop(handle);
Jan Kara678aaf42008-07-11 19:27:31 -04004791
4792 if (ext4_should_order_data(inode)) {
4793 error = ext4_begin_ordered_truncate(inode,
4794 attr->ia_size);
4795 if (error) {
4796 /* Do as much error cleanup as possible */
4797 handle = ext4_journal_start(inode, 3);
4798 if (IS_ERR(handle)) {
4799 ext4_orphan_del(NULL, inode);
4800 goto err_out;
4801 }
4802 ext4_orphan_del(handle, inode);
4803 ext4_journal_stop(handle);
4804 goto err_out;
4805 }
4806 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004807 }
4808
4809 rc = inode_setattr(inode, attr);
4810
Mingming Cao617ba132006-10-11 01:20:53 -07004811 /* If inode_setattr's call to ext4_truncate failed to get a
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004812 * transaction handle at all, we need to clean up the in-core
4813 * orphan list manually. */
4814 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07004815 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004816
4817 if (!rc && (ia_valid & ATTR_MODE))
Mingming Cao617ba132006-10-11 01:20:53 -07004818 rc = ext4_acl_chmod(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004819
4820err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07004821 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004822 if (!error)
4823 error = rc;
4824 return error;
4825}
4826
Mingming Cao3e3398a2008-07-11 19:27:31 -04004827int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
4828 struct kstat *stat)
4829{
4830 struct inode *inode;
4831 unsigned long delalloc_blocks;
4832
4833 inode = dentry->d_inode;
4834 generic_fillattr(inode, stat);
4835
4836 /*
4837 * We can't update i_blocks if the block allocation is delayed
4838 * otherwise in the case of system crash before the real block
4839 * allocation is done, we will have i_blocks inconsistent with
4840 * on-disk file blocks.
4841 * We always keep i_blocks updated together with real
4842 * allocation. But to not confuse with user, stat
4843 * will return the blocks that include the delayed allocation
4844 * blocks for this file.
4845 */
4846 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
4847 delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks;
4848 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
4849
4850 stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
4851 return 0;
4852}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004853
Mingming Caoa02908f2008-08-19 22:16:07 -04004854static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks,
4855 int chunk)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004856{
Mingming Caoa02908f2008-08-19 22:16:07 -04004857 int indirects;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004858
Mingming Caoa02908f2008-08-19 22:16:07 -04004859 /* if nrblocks are contiguous */
4860 if (chunk) {
4861 /*
4862 * With N contiguous data blocks, it need at most
4863 * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks
4864 * 2 dindirect blocks
4865 * 1 tindirect block
4866 */
4867 indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb);
4868 return indirects + 3;
4869 }
4870 /*
4871 * if nrblocks are not contiguous, worse case, each block touch
4872 * a indirect block, and each indirect block touch a double indirect
4873 * block, plus a triple indirect block
4874 */
4875 indirects = nrblocks * 2 + 1;
4876 return indirects;
4877}
Alex Tomasa86c6182006-10-11 01:21:03 -07004878
Mingming Caoa02908f2008-08-19 22:16:07 -04004879static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4880{
4881 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
Theodore Ts'oac51d832008-11-06 16:49:36 -05004882 return ext4_indirect_trans_blocks(inode, nrblocks, chunk);
4883 return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
Mingming Caoa02908f2008-08-19 22:16:07 -04004884}
Theodore Ts'oac51d832008-11-06 16:49:36 -05004885
Mingming Caoa02908f2008-08-19 22:16:07 -04004886/*
4887 * Account for index blocks, block groups bitmaps and block group
4888 * descriptor blocks if modify datablocks and index blocks
4889 * worse case, the indexs blocks spread over different block groups
4890 *
4891 * If datablocks are discontiguous, they are possible to spread over
4892 * different block groups too. If they are contiugous, with flexbg,
4893 * they could still across block group boundary.
4894 *
4895 * Also account for superblock, inode, quota and xattr blocks
4896 */
4897int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4898{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004899 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
4900 int gdpblocks;
Mingming Caoa02908f2008-08-19 22:16:07 -04004901 int idxblocks;
4902 int ret = 0;
4903
4904 /*
4905 * How many index blocks need to touch to modify nrblocks?
4906 * The "Chunk" flag indicating whether the nrblocks is
4907 * physically contiguous on disk
4908 *
4909 * For Direct IO and fallocate, they calls get_block to allocate
4910 * one single extent at a time, so they could set the "Chunk" flag
4911 */
4912 idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
4913
4914 ret = idxblocks;
4915
4916 /*
4917 * Now let's see how many group bitmaps and group descriptors need
4918 * to account
4919 */
4920 groups = idxblocks;
4921 if (chunk)
4922 groups += 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004923 else
Mingming Caoa02908f2008-08-19 22:16:07 -04004924 groups += nrblocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004925
Mingming Caoa02908f2008-08-19 22:16:07 -04004926 gdpblocks = groups;
Theodore Ts'o8df96752009-05-01 08:50:38 -04004927 if (groups > ngroups)
4928 groups = ngroups;
Mingming Caoa02908f2008-08-19 22:16:07 -04004929 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
4930 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
4931
4932 /* bitmaps and block group descriptor blocks */
4933 ret += groups + gdpblocks;
4934
4935 /* Blocks for super block, inode, quota and xattr blocks */
4936 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004937
4938 return ret;
4939}
4940
4941/*
Mingming Caoa02908f2008-08-19 22:16:07 -04004942 * Calulate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04004943 * the modification of a single pages into a single transaction,
4944 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04004945 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004946 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04004947 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004948 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04004949 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04004950 */
4951int ext4_writepage_trans_blocks(struct inode *inode)
4952{
4953 int bpp = ext4_journal_blocks_per_page(inode);
4954 int ret;
4955
4956 ret = ext4_meta_trans_blocks(inode, bpp, 0);
4957
4958 /* Account for data blocks for journalled mode */
4959 if (ext4_should_journal_data(inode))
4960 ret += bpp;
4961 return ret;
4962}
Mingming Caof3bd1f32008-08-19 22:16:03 -04004963
4964/*
4965 * Calculate the journal credits for a chunk of data modification.
4966 *
4967 * This is called from DIO, fallocate or whoever calling
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04004968 * ext4_get_blocks() to map/allocate a chunk of contigous disk blocks.
Mingming Caof3bd1f32008-08-19 22:16:03 -04004969 *
4970 * journal buffers for data blocks are not included here, as DIO
4971 * and fallocate do no need to journal data buffers.
4972 */
4973int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
4974{
4975 return ext4_meta_trans_blocks(inode, nrblocks, 1);
4976}
4977
Mingming Caoa02908f2008-08-19 22:16:07 -04004978/*
Mingming Cao617ba132006-10-11 01:20:53 -07004979 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004980 * Give this, we know that the caller already has write access to iloc->bh.
4981 */
Mingming Cao617ba132006-10-11 01:20:53 -07004982int ext4_mark_iloc_dirty(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004983 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004984{
4985 int err = 0;
4986
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004987 if (test_opt(inode->i_sb, I_VERSION))
4988 inode_inc_iversion(inode);
4989
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004990 /* the do_update_inode consumes one bh->b_count */
4991 get_bh(iloc->bh);
4992
Mingming Caodab291a2006-10-11 01:21:01 -07004993 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Mingming Cao617ba132006-10-11 01:20:53 -07004994 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004995 put_bh(iloc->bh);
4996 return err;
4997}
4998
4999/*
5000 * On success, We end up with an outstanding reference count against
5001 * iloc->bh. This _must_ be cleaned up later.
5002 */
5003
5004int
Mingming Cao617ba132006-10-11 01:20:53 -07005005ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
5006 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005007{
Frank Mayhar03901312009-01-07 00:06:22 -05005008 int err;
5009
5010 err = ext4_get_inode_loc(inode, iloc);
5011 if (!err) {
5012 BUFFER_TRACE(iloc->bh, "get_write_access");
5013 err = ext4_journal_get_write_access(handle, iloc->bh);
5014 if (err) {
5015 brelse(iloc->bh);
5016 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005017 }
5018 }
Mingming Cao617ba132006-10-11 01:20:53 -07005019 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005020 return err;
5021}
5022
5023/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005024 * Expand an inode by new_extra_isize bytes.
5025 * Returns 0 on success or negative error number on failure.
5026 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05005027static int ext4_expand_extra_isize(struct inode *inode,
5028 unsigned int new_extra_isize,
5029 struct ext4_iloc iloc,
5030 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005031{
5032 struct ext4_inode *raw_inode;
5033 struct ext4_xattr_ibody_header *header;
5034 struct ext4_xattr_entry *entry;
5035
5036 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
5037 return 0;
5038
5039 raw_inode = ext4_raw_inode(&iloc);
5040
5041 header = IHDR(inode, raw_inode);
5042 entry = IFIRST(header);
5043
5044 /* No extended attributes present */
5045 if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) ||
5046 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
5047 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
5048 new_extra_isize);
5049 EXT4_I(inode)->i_extra_isize = new_extra_isize;
5050 return 0;
5051 }
5052
5053 /* try to expand with EAs present */
5054 return ext4_expand_extra_isize_ea(inode, new_extra_isize,
5055 raw_inode, handle);
5056}
5057
5058/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005059 * What we do here is to mark the in-core inode as clean with respect to inode
5060 * dirtiness (it may still be data-dirty).
5061 * This means that the in-core inode may be reaped by prune_icache
5062 * without having to perform any I/O. This is a very good thing,
5063 * because *any* task may call prune_icache - even ones which
5064 * have a transaction open against a different journal.
5065 *
5066 * Is this cheating? Not really. Sure, we haven't written the
5067 * inode out, but prune_icache isn't a user-visible syncing function.
5068 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5069 * we start and wait on commits.
5070 *
5071 * Is this efficient/effective? Well, we're being nice to the system
5072 * by cleaning up our inodes proactively so they can be reaped
5073 * without I/O. But we are potentially leaving up to five seconds'
5074 * worth of inodes floating about which prune_icache wants us to
5075 * write out. One way to fix that would be to get prune_icache()
5076 * to do a write_super() to free up some memory. It has the desired
5077 * effect.
5078 */
Mingming Cao617ba132006-10-11 01:20:53 -07005079int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005080{
Mingming Cao617ba132006-10-11 01:20:53 -07005081 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005082 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5083 static unsigned int mnt_count;
5084 int err, ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005085
5086 might_sleep();
Mingming Cao617ba132006-10-11 01:20:53 -07005087 err = ext4_reserve_inode_write(handle, inode, &iloc);
Frank Mayhar03901312009-01-07 00:06:22 -05005088 if (ext4_handle_valid(handle) &&
5089 EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005090 !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) {
5091 /*
5092 * We need extra buffer credits since we may write into EA block
5093 * with this same handle. If journal_extend fails, then it will
5094 * only result in a minor loss of functionality for that inode.
5095 * If this is felt to be critical, then e2fsck should be run to
5096 * force a large enough s_min_extra_isize.
5097 */
5098 if ((jbd2_journal_extend(handle,
5099 EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
5100 ret = ext4_expand_extra_isize(inode,
5101 sbi->s_want_extra_isize,
5102 iloc, handle);
5103 if (ret) {
5104 EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND;
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005105 if (mnt_count !=
5106 le16_to_cpu(sbi->s_es->s_mnt_count)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04005107 ext4_warning(inode->i_sb, __func__,
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005108 "Unable to expand inode %lu. Delete"
5109 " some EAs or run e2fsck.",
5110 inode->i_ino);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005111 mnt_count =
5112 le16_to_cpu(sbi->s_es->s_mnt_count);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005113 }
5114 }
5115 }
5116 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005117 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07005118 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005119 return err;
5120}
5121
5122/*
Mingming Cao617ba132006-10-11 01:20:53 -07005123 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005124 *
5125 * We're really interested in the case where a file is being extended.
5126 * i_size has been changed by generic_commit_write() and we thus need
5127 * to include the updated inode in the current transaction.
5128 *
Jan Karaa269eb12009-01-26 17:04:39 +01005129 * Also, vfs_dq_alloc_block() will always dirty the inode when blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005130 * are allocated to the file.
5131 *
5132 * If the inode is marked synchronous, we don't honour that here - doing
5133 * so would cause a commit on atime updates, which we don't bother doing.
5134 * We handle synchronous inodes at the highest possible level.
5135 */
Mingming Cao617ba132006-10-11 01:20:53 -07005136void ext4_dirty_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005137{
Mingming Cao617ba132006-10-11 01:20:53 -07005138 handle_t *current_handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005139 handle_t *handle;
5140
Frank Mayhar03901312009-01-07 00:06:22 -05005141 if (!ext4_handle_valid(current_handle)) {
5142 ext4_mark_inode_dirty(current_handle, inode);
5143 return;
5144 }
5145
Mingming Cao617ba132006-10-11 01:20:53 -07005146 handle = ext4_journal_start(inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005147 if (IS_ERR(handle))
5148 goto out;
5149 if (current_handle &&
5150 current_handle->h_transaction != handle->h_transaction) {
5151 /* This task has a transaction open against a different fs */
5152 printk(KERN_EMERG "%s: transactions do not match!\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04005153 __func__);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005154 } else {
5155 jbd_debug(5, "marking dirty. outer handle=%p\n",
5156 current_handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005157 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005158 }
Mingming Cao617ba132006-10-11 01:20:53 -07005159 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005160out:
5161 return;
5162}
5163
5164#if 0
5165/*
5166 * Bind an inode's backing buffer_head into this transaction, to prevent
5167 * it from being flushed to disk early. Unlike
Mingming Cao617ba132006-10-11 01:20:53 -07005168 * ext4_reserve_inode_write, this leaves behind no bh reference and
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005169 * returns no iloc structure, so the caller needs to repeat the iloc
5170 * lookup to mark the inode dirty later.
5171 */
Mingming Cao617ba132006-10-11 01:20:53 -07005172static int ext4_pin_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005173{
Mingming Cao617ba132006-10-11 01:20:53 -07005174 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005175
5176 int err = 0;
5177 if (handle) {
Mingming Cao617ba132006-10-11 01:20:53 -07005178 err = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005179 if (!err) {
5180 BUFFER_TRACE(iloc.bh, "get_write_access");
Mingming Caodab291a2006-10-11 01:21:01 -07005181 err = jbd2_journal_get_write_access(handle, iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005182 if (!err)
Frank Mayhar03901312009-01-07 00:06:22 -05005183 err = ext4_handle_dirty_metadata(handle,
5184 inode,
5185 iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005186 brelse(iloc.bh);
5187 }
5188 }
Mingming Cao617ba132006-10-11 01:20:53 -07005189 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005190 return err;
5191}
5192#endif
5193
Mingming Cao617ba132006-10-11 01:20:53 -07005194int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005195{
5196 journal_t *journal;
5197 handle_t *handle;
5198 int err;
5199
5200 /*
5201 * We have to be very careful here: changing a data block's
5202 * journaling status dynamically is dangerous. If we write a
5203 * data block to the journal, change the status and then delete
5204 * that block, we risk forgetting to revoke the old log record
5205 * from the journal and so a subsequent replay can corrupt data.
5206 * So, first we make sure that the journal is empty and that
5207 * nobody is changing anything.
5208 */
5209
Mingming Cao617ba132006-10-11 01:20:53 -07005210 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005211 if (!journal)
5212 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04005213 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005214 return -EROFS;
5215
Mingming Caodab291a2006-10-11 01:21:01 -07005216 jbd2_journal_lock_updates(journal);
5217 jbd2_journal_flush(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005218
5219 /*
5220 * OK, there are no updates running now, and all cached data is
5221 * synced to disk. We are now in a completely consistent state
5222 * which doesn't have anything in the journal, and we know that
5223 * no filesystem updates are running, so it is safe to modify
5224 * the inode's in-core data-journaling state flag now.
5225 */
5226
5227 if (val)
Mingming Cao617ba132006-10-11 01:20:53 -07005228 EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005229 else
Mingming Cao617ba132006-10-11 01:20:53 -07005230 EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL;
5231 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005232
Mingming Caodab291a2006-10-11 01:21:01 -07005233 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005234
5235 /* Finally we can mark the inode as dirty. */
5236
Mingming Cao617ba132006-10-11 01:20:53 -07005237 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005238 if (IS_ERR(handle))
5239 return PTR_ERR(handle);
5240
Mingming Cao617ba132006-10-11 01:20:53 -07005241 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005242 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005243 ext4_journal_stop(handle);
5244 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005245
5246 return err;
5247}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005248
5249static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5250{
5251 return !buffer_mapped(bh);
5252}
5253
Nick Pigginc2ec1752009-03-31 15:23:21 -07005254int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005255{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005256 struct page *page = vmf->page;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005257 loff_t size;
5258 unsigned long len;
5259 int ret = -EINVAL;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005260 void *fsdata;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005261 struct file *file = vma->vm_file;
5262 struct inode *inode = file->f_path.dentry->d_inode;
5263 struct address_space *mapping = inode->i_mapping;
5264
5265 /*
5266 * Get i_alloc_sem to stop truncates messing with the inode. We cannot
5267 * get i_mutex because we are already holding mmap_sem.
5268 */
5269 down_read(&inode->i_alloc_sem);
5270 size = i_size_read(inode);
5271 if (page->mapping != mapping || size <= page_offset(page)
5272 || !PageUptodate(page)) {
5273 /* page got truncated from under us? */
5274 goto out_unlock;
5275 }
5276 ret = 0;
5277 if (PageMappedToDisk(page))
5278 goto out_unlock;
5279
5280 if (page->index == size >> PAGE_CACHE_SHIFT)
5281 len = size & ~PAGE_CACHE_MASK;
5282 else
5283 len = PAGE_CACHE_SIZE;
5284
5285 if (page_has_buffers(page)) {
5286 /* return if we have all the buffers mapped */
5287 if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
5288 ext4_bh_unmapped))
5289 goto out_unlock;
5290 }
5291 /*
5292 * OK, we need to fill the hole... Do write_begin write_end
5293 * to do block allocation/reservation.We are not holding
5294 * inode.i__mutex here. That allow * parallel write_begin,
5295 * write_end call. lock_page prevent this from happening
5296 * on the same page though
5297 */
5298 ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005299 len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005300 if (ret < 0)
5301 goto out_unlock;
5302 ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005303 len, len, page, fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005304 if (ret < 0)
5305 goto out_unlock;
5306 ret = 0;
5307out_unlock:
Nick Pigginc2ec1752009-03-31 15:23:21 -07005308 if (ret)
5309 ret = VM_FAULT_SIGBUS;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005310 up_read(&inode->i_alloc_sem);
5311 return ret;
5312}