blob: c98e3afea30a0bff2b2e201358c03d7c102c24ed [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 *
81 * If the handle isn't valid we're not journaling so there's nothing to do.
Dave Kleikampac27a0e2006-10-11 01:20:50 -070082 */
Mingming Cao617ba132006-10-11 01:20:53 -070083int ext4_forget(handle_t *handle, int is_metadata, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -040084 struct buffer_head *bh, ext4_fsblk_t blocknr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070085{
86 int err;
87
Frank Mayhar03901312009-01-07 00:06:22 -050088 if (!ext4_handle_valid(handle))
89 return 0;
90
Dave Kleikampac27a0e2006-10-11 01:20:50 -070091 might_sleep();
92
93 BUFFER_TRACE(bh, "enter");
94
95 jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
Theodore Ts'o7f4520c2009-06-13 10:09:41 -040096 "data mode %x\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -070097 bh, is_metadata, inode->i_mode,
98 test_opt(inode->i_sb, DATA_FLAGS));
99
100 /* Never use the revoke function if we are doing full data
101 * journaling: there is no need to, and a V1 superblock won't
102 * support it. Otherwise, only skip the revoke on un-journaled
103 * data blocks. */
104
Mingming Cao617ba132006-10-11 01:20:53 -0700105 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
106 (!is_metadata && !ext4_should_journal_data(inode))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700107 if (bh) {
Mingming Caodab291a2006-10-11 01:21:01 -0700108 BUFFER_TRACE(bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700109 return ext4_journal_forget(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700110 }
111 return 0;
112 }
113
114 /*
115 * data!=journal && (is_metadata || should_journal_data(inode))
116 */
Mingming Cao617ba132006-10-11 01:20:53 -0700117 BUFFER_TRACE(bh, "call ext4_journal_revoke");
118 err = ext4_journal_revoke(handle, blocknr, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700119 if (err)
Harvey Harrison46e665e2008-04-17 10:38:59 -0400120 ext4_abort(inode->i_sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700121 "error %d when attempting revoke", err);
122 BUFFER_TRACE(bh, "exit");
123 return err;
124}
125
126/*
127 * Work out how many blocks we need to proceed with the next chunk of a
128 * truncate transaction.
129 */
130static unsigned long blocks_for_truncate(struct inode *inode)
131{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500132 ext4_lblk_t needed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700133
134 needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
135
136 /* Give ourselves just enough room to cope with inodes in which
137 * i_blocks is corrupt: we've seen disk corruptions in the past
138 * which resulted in random data in an inode which looked enough
Mingming Cao617ba132006-10-11 01:20:53 -0700139 * like a regular file for ext4 to try to delete it. Things
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700140 * will go a bit crazy if that happens, but at least we should
141 * try not to panic the whole kernel. */
142 if (needed < 2)
143 needed = 2;
144
145 /* But we need to bound the transaction so we don't overflow the
146 * journal. */
Mingming Cao617ba132006-10-11 01:20:53 -0700147 if (needed > EXT4_MAX_TRANS_DATA)
148 needed = EXT4_MAX_TRANS_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700149
Mingming Cao617ba132006-10-11 01:20:53 -0700150 return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700151}
152
153/*
154 * Truncate transactions can be complex and absolutely huge. So we need to
155 * be able to restart the transaction at a conventient checkpoint to make
156 * sure we don't overflow the journal.
157 *
158 * start_transaction gets us a new handle for a truncate transaction,
159 * and extend_transaction tries to extend the existing one a bit. If
160 * extend fails, we need to propagate the failure up and restart the
161 * transaction in the top-level truncate loop. --sct
162 */
163static handle_t *start_transaction(struct inode *inode)
164{
165 handle_t *result;
166
Mingming Cao617ba132006-10-11 01:20:53 -0700167 result = ext4_journal_start(inode, blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700168 if (!IS_ERR(result))
169 return result;
170
Mingming Cao617ba132006-10-11 01:20:53 -0700171 ext4_std_error(inode->i_sb, PTR_ERR(result));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700172 return result;
173}
174
175/*
176 * Try to extend this transaction for the purposes of truncation.
177 *
178 * Returns 0 if we managed to create more room. If we can't create more
179 * room, and the transaction must be restarted we return 1.
180 */
181static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
182{
Frank Mayhar03901312009-01-07 00:06:22 -0500183 if (!ext4_handle_valid(handle))
184 return 0;
185 if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700186 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700187 if (!ext4_journal_extend(handle, blocks_for_truncate(inode)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700188 return 0;
189 return 1;
190}
191
192/*
193 * Restart the transaction associated with *handle. This does a commit,
194 * so before we call here everything must be consistently dirtied against
195 * this transaction.
196 */
Mingming Cao617ba132006-10-11 01:20:53 -0700197static int ext4_journal_test_restart(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700198{
Frank Mayhar03901312009-01-07 00:06:22 -0500199 BUG_ON(EXT4_JOURNAL(inode) == NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700200 jbd_debug(2, "restarting handle %p\n", handle);
Mingming Cao617ba132006-10-11 01:20:53 -0700201 return ext4_journal_restart(handle, blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700202}
203
204/*
205 * Called at the last iput() if i_nlink is zero.
206 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400207void ext4_delete_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700208{
209 handle_t *handle;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400210 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700211
Jan Kara678aaf42008-07-11 19:27:31 -0400212 if (ext4_should_order_data(inode))
213 ext4_begin_ordered_truncate(inode, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700214 truncate_inode_pages(&inode->i_data, 0);
215
216 if (is_bad_inode(inode))
217 goto no_delete;
218
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400219 handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700220 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400221 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700222 /*
223 * If we're going to skip the normal cleanup, we still need to
224 * make sure that the in-core orphan linked list is properly
225 * cleaned up.
226 */
Mingming Cao617ba132006-10-11 01:20:53 -0700227 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700228 goto no_delete;
229 }
230
231 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500232 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700233 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400234 err = ext4_mark_inode_dirty(handle, inode);
235 if (err) {
236 ext4_warning(inode->i_sb, __func__,
237 "couldn't mark inode dirty (err %d)", err);
238 goto stop_handle;
239 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700240 if (inode->i_blocks)
Mingming Cao617ba132006-10-11 01:20:53 -0700241 ext4_truncate(inode);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400242
243 /*
244 * ext4_ext_truncate() doesn't reserve any slop when it
245 * restarts journal transactions; therefore there may not be
246 * enough credits left in the handle to remove the inode from
247 * the orphan list and set the dtime field.
248 */
Frank Mayhar03901312009-01-07 00:06:22 -0500249 if (!ext4_handle_has_enough_credits(handle, 3)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400250 err = ext4_journal_extend(handle, 3);
251 if (err > 0)
252 err = ext4_journal_restart(handle, 3);
253 if (err != 0) {
254 ext4_warning(inode->i_sb, __func__,
255 "couldn't extend journal (err %d)", err);
256 stop_handle:
257 ext4_journal_stop(handle);
258 goto no_delete;
259 }
260 }
261
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700262 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700263 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700264 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700265 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700266 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700267 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700268 * (Well, we could do this if we need to, but heck - it works)
269 */
Mingming Cao617ba132006-10-11 01:20:53 -0700270 ext4_orphan_del(handle, inode);
271 EXT4_I(inode)->i_dtime = get_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700272
273 /*
274 * One subtle ordering requirement: if anything has gone wrong
275 * (transaction abort, IO errors, whatever), then we can still
276 * do these next steps (the fs will already have been marked as
277 * having errors), but we can't free the inode if the mark_dirty
278 * fails.
279 */
Mingming Cao617ba132006-10-11 01:20:53 -0700280 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700281 /* If that failed, just do the required in-core inode clear. */
282 clear_inode(inode);
283 else
Mingming Cao617ba132006-10-11 01:20:53 -0700284 ext4_free_inode(handle, inode);
285 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700286 return;
287no_delete:
288 clear_inode(inode); /* We must guarantee clearing of inode... */
289}
290
291typedef struct {
292 __le32 *p;
293 __le32 key;
294 struct buffer_head *bh;
295} Indirect;
296
297static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
298{
299 p->key = *(p->p = v);
300 p->bh = bh;
301}
302
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700303/**
Mingming Cao617ba132006-10-11 01:20:53 -0700304 * ext4_block_to_path - parse the block number into array of offsets
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700305 * @inode: inode in question (we are only interested in its superblock)
306 * @i_block: block number to be parsed
307 * @offsets: array to store the offsets in
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400308 * @boundary: set this non-zero if the referred-to block is likely to be
309 * followed (on disk) by an indirect block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700310 *
Mingming Cao617ba132006-10-11 01:20:53 -0700311 * To store the locations of file's data ext4 uses a data structure common
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700312 * for UNIX filesystems - tree of pointers anchored in the inode, with
313 * data blocks at leaves and indirect blocks in intermediate nodes.
314 * This function translates the block number into path in that tree -
315 * return value is the path length and @offsets[n] is the offset of
316 * pointer to (n+1)th node in the nth one. If @block is out of range
317 * (negative or too large) warning is printed and zero returned.
318 *
319 * Note: function doesn't find node addresses, so no IO is needed. All
320 * we need to know is the capacity of indirect blocks (taken from the
321 * inode->i_sb).
322 */
323
324/*
325 * Portability note: the last comparison (check that we fit into triple
326 * indirect block) is spelled differently, because otherwise on an
327 * architecture with 32-bit longs and 8Kb pages we might get into trouble
328 * if our filesystem had 8Kb blocks. We might use long long, but that would
329 * kill us on x86. Oh, well, at least the sign propagation does not matter -
330 * i_block would have to be negative in the very beginning, so we would not
331 * get there at all.
332 */
333
Mingming Cao617ba132006-10-11 01:20:53 -0700334static int ext4_block_to_path(struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400335 ext4_lblk_t i_block,
336 ext4_lblk_t offsets[4], int *boundary)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700337{
Mingming Cao617ba132006-10-11 01:20:53 -0700338 int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb);
339 int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb);
340 const long direct_blocks = EXT4_NDIR_BLOCKS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700341 indirect_blocks = ptrs,
342 double_blocks = (1 << (ptrs_bits * 2));
343 int n = 0;
344 int final = 0;
345
346 if (i_block < 0) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400347 ext4_warning(inode->i_sb, "ext4_block_to_path", "block < 0");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700348 } else if (i_block < direct_blocks) {
349 offsets[n++] = i_block;
350 final = direct_blocks;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400351 } else if ((i_block -= direct_blocks) < indirect_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700352 offsets[n++] = EXT4_IND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700353 offsets[n++] = i_block;
354 final = ptrs;
355 } else if ((i_block -= indirect_blocks) < double_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700356 offsets[n++] = EXT4_DIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700357 offsets[n++] = i_block >> ptrs_bits;
358 offsets[n++] = i_block & (ptrs - 1);
359 final = ptrs;
360 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
Mingming Cao617ba132006-10-11 01:20:53 -0700361 offsets[n++] = EXT4_TIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700362 offsets[n++] = i_block >> (ptrs_bits * 2);
363 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
364 offsets[n++] = i_block & (ptrs - 1);
365 final = ptrs;
366 } else {
Eric Sandeene2b46572008-01-28 23:58:27 -0500367 ext4_warning(inode->i_sb, "ext4_block_to_path",
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400368 "block %lu > max in inode %lu",
369 i_block + direct_blocks +
370 indirect_blocks + double_blocks, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700371 }
372 if (boundary)
373 *boundary = final - 1 - (i_block & (ptrs - 1));
374 return n;
375}
376
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400377static int __ext4_check_blockref(const char *function, struct inode *inode,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400378 __le32 *p, unsigned int max)
379{
Thiemo Nagelf73953c2009-04-07 18:46:47 -0400380 __le32 *bref = p;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400381 unsigned int blk;
382
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400383 while (bref < p+max) {
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400384 blk = le32_to_cpu(*bref++);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400385 if (blk &&
386 unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb),
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400387 blk, 1))) {
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400388 ext4_error(inode->i_sb, function,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400389 "invalid block reference %u "
390 "in inode #%lu", blk, inode->i_ino);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400391 return -EIO;
392 }
393 }
394 return 0;
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400395}
396
397
398#define ext4_check_indirect_blockref(inode, bh) \
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400399 __ext4_check_blockref(__func__, inode, (__le32 *)(bh)->b_data, \
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400400 EXT4_ADDR_PER_BLOCK((inode)->i_sb))
401
402#define ext4_check_inode_blockref(inode) \
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400403 __ext4_check_blockref(__func__, inode, EXT4_I(inode)->i_data, \
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400404 EXT4_NDIR_BLOCKS)
405
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700406/**
Mingming Cao617ba132006-10-11 01:20:53 -0700407 * ext4_get_branch - read the chain of indirect blocks leading to data
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700408 * @inode: inode in question
409 * @depth: depth of the chain (1 - direct pointer, etc.)
410 * @offsets: offsets of pointers in inode/indirect blocks
411 * @chain: place to store the result
412 * @err: here we store the error value
413 *
414 * Function fills the array of triples <key, p, bh> and returns %NULL
415 * if everything went OK or the pointer to the last filled triple
416 * (incomplete one) otherwise. Upon the return chain[i].key contains
417 * the number of (i+1)-th block in the chain (as it is stored in memory,
418 * i.e. little-endian 32-bit), chain[i].p contains the address of that
419 * number (it points into struct inode for i==0 and into the bh->b_data
420 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
421 * block for i>0 and NULL for i==0. In other words, it holds the block
422 * numbers of the chain, addresses they were taken from (and where we can
423 * verify that chain did not change) and buffer_heads hosting these
424 * numbers.
425 *
426 * Function stops when it stumbles upon zero pointer (absent block)
427 * (pointer to last triple returned, *@err == 0)
428 * or when it gets an IO error reading an indirect block
429 * (ditto, *@err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700430 * or when it reads all @depth-1 indirect blocks successfully and finds
431 * the whole chain, all way to the data (returns %NULL, *err == 0).
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500432 *
433 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500434 * down_read(&EXT4_I(inode)->i_data_sem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700435 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500436static Indirect *ext4_get_branch(struct inode *inode, int depth,
437 ext4_lblk_t *offsets,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700438 Indirect chain[4], int *err)
439{
440 struct super_block *sb = inode->i_sb;
441 Indirect *p = chain;
442 struct buffer_head *bh;
443
444 *err = 0;
445 /* i_data is not going away, no lock needed */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400446 add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700447 if (!p->key)
448 goto no_block;
449 while (--depth) {
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400450 bh = sb_getblk(sb, le32_to_cpu(p->key));
451 if (unlikely(!bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700452 goto failure;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400453
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400454 if (!bh_uptodate_or_lock(bh)) {
455 if (bh_submit_read(bh) < 0) {
456 put_bh(bh);
457 goto failure;
458 }
459 /* validate block references */
460 if (ext4_check_indirect_blockref(inode, bh)) {
461 put_bh(bh);
462 goto failure;
463 }
464 }
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400465
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400466 add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700467 /* Reader: end */
468 if (!p->key)
469 goto no_block;
470 }
471 return NULL;
472
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700473failure:
474 *err = -EIO;
475no_block:
476 return p;
477}
478
479/**
Mingming Cao617ba132006-10-11 01:20:53 -0700480 * ext4_find_near - find a place for allocation with sufficient locality
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700481 * @inode: owner
482 * @ind: descriptor of indirect block.
483 *
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000484 * This function returns the preferred place for block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700485 * It is used when heuristic for sequential allocation fails.
486 * Rules are:
487 * + if there is a block to the left of our position - allocate near it.
488 * + if pointer will live in indirect block - allocate near that block.
489 * + if pointer will live in inode - allocate in the same
490 * cylinder group.
491 *
492 * In the latter case we colour the starting block by the callers PID to
493 * prevent it from clashing with concurrent allocations for a different inode
494 * in the same block group. The PID is used here so that functionally related
495 * files will be close-by on-disk.
496 *
497 * Caller must make sure that @ind is valid and will stay that way.
498 */
Mingming Cao617ba132006-10-11 01:20:53 -0700499static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500{
Mingming Cao617ba132006-10-11 01:20:53 -0700501 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400502 __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700503 __le32 *p;
Mingming Cao617ba132006-10-11 01:20:53 -0700504 ext4_fsblk_t bg_start;
Valerie Clement74d34872008-02-15 13:43:07 -0500505 ext4_fsblk_t last_block;
Mingming Cao617ba132006-10-11 01:20:53 -0700506 ext4_grpblk_t colour;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400507 ext4_group_t block_group;
508 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700509
510 /* Try to find previous block */
511 for (p = ind->p - 1; p >= start; p--) {
512 if (*p)
513 return le32_to_cpu(*p);
514 }
515
516 /* No such thing, so let's try location of indirect block */
517 if (ind->bh)
518 return ind->bh->b_blocknr;
519
520 /*
521 * It is going to be referred to from the inode itself? OK, just put it
522 * into the same cylinder group then.
523 */
Theodore Ts'oa4912122009-03-12 12:18:34 -0400524 block_group = ei->i_block_group;
525 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
526 block_group &= ~(flex_size-1);
527 if (S_ISREG(inode->i_mode))
528 block_group++;
529 }
530 bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
Valerie Clement74d34872008-02-15 13:43:07 -0500531 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
532
Theodore Ts'oa4912122009-03-12 12:18:34 -0400533 /*
534 * If we are doing delayed allocation, we don't need take
535 * colour into account.
536 */
537 if (test_opt(inode->i_sb, DELALLOC))
538 return bg_start;
539
Valerie Clement74d34872008-02-15 13:43:07 -0500540 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
541 colour = (current->pid % 16) *
Mingming Cao617ba132006-10-11 01:20:53 -0700542 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
Valerie Clement74d34872008-02-15 13:43:07 -0500543 else
544 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700545 return bg_start + colour;
546}
547
548/**
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000549 * ext4_find_goal - find a preferred place for allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700550 * @inode: owner
551 * @block: block we want
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700552 * @partial: pointer to the last triple within a chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700553 *
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000554 * Normally this function find the preferred place for block allocation,
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800555 * returns it.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700556 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500557static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400558 Indirect *partial)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700559{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700560 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400561 * XXX need to get goal block from mballoc's data structures
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700562 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563
Mingming Cao617ba132006-10-11 01:20:53 -0700564 return ext4_find_near(inode, partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700565}
566
567/**
Mingming Cao617ba132006-10-11 01:20:53 -0700568 * ext4_blks_to_allocate: Look up the block map and count the number
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700569 * of direct blocks need to be allocated for the given branch.
570 *
571 * @branch: chain of indirect blocks
572 * @k: number of blocks need for indirect blocks
573 * @blks: number of data blocks to be mapped.
574 * @blocks_to_boundary: the offset in the indirect block
575 *
576 * return the total number of blocks to be allocate, including the
577 * direct and indirect blocks.
578 */
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500579static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400580 int blocks_to_boundary)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700581{
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500582 unsigned int count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700583
584 /*
585 * Simple case, [t,d]Indirect block(s) has not allocated yet
586 * then it's clear blocks on that path have not allocated
587 */
588 if (k > 0) {
589 /* right now we don't handle cross boundary allocation */
590 if (blks < blocks_to_boundary + 1)
591 count += blks;
592 else
593 count += blocks_to_boundary + 1;
594 return count;
595 }
596
597 count++;
598 while (count < blks && count <= blocks_to_boundary &&
599 le32_to_cpu(*(branch[0].p + count)) == 0) {
600 count++;
601 }
602 return count;
603}
604
605/**
Mingming Cao617ba132006-10-11 01:20:53 -0700606 * ext4_alloc_blocks: multiple allocate blocks needed for a branch
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700607 * @indirect_blks: the number of blocks need to allocate for indirect
608 * blocks
609 *
610 * @new_blocks: on return it will store the new block numbers for
611 * the indirect blocks(if needed) and the first direct block,
612 * @blks: on return it will store the total number of allocated
613 * direct blocks
614 */
Mingming Cao617ba132006-10-11 01:20:53 -0700615static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400616 ext4_lblk_t iblock, ext4_fsblk_t goal,
617 int indirect_blks, int blks,
618 ext4_fsblk_t new_blocks[4], int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700619{
Theodore Ts'o815a1132009-01-01 23:59:43 -0500620 struct ext4_allocation_request ar;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700621 int target, i;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400622 unsigned long count = 0, blk_allocated = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700623 int index = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700624 ext4_fsblk_t current_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700625 int ret = 0;
626
627 /*
628 * Here we try to allocate the requested multiple blocks at once,
629 * on a best-effort basis.
630 * To build a branch, we should allocate blocks for
631 * the indirect blocks(if not allocated yet), and at least
632 * the first direct block of this branch. That's the
633 * minimum number of blocks need to allocate(required)
634 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400635 /* first we try to allocate the indirect blocks */
636 target = indirect_blks;
637 while (target > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700638 count = target;
639 /* allocating blocks for indirect blocks and direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400640 current_block = ext4_new_meta_blocks(handle, inode,
641 goal, &count, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700642 if (*err)
643 goto failed_out;
644
645 target -= count;
646 /* allocate blocks for indirect blocks */
647 while (index < indirect_blks && count) {
648 new_blocks[index++] = current_block++;
649 count--;
650 }
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400651 if (count > 0) {
652 /*
653 * save the new block number
654 * for the first direct block
655 */
656 new_blocks[index] = current_block;
657 printk(KERN_INFO "%s returned more blocks than "
658 "requested\n", __func__);
659 WARN_ON(1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700660 break;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400661 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700662 }
663
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400664 target = blks - count ;
665 blk_allocated = count;
666 if (!target)
667 goto allocated;
668 /* Now allocate data blocks */
Theodore Ts'o815a1132009-01-01 23:59:43 -0500669 memset(&ar, 0, sizeof(ar));
670 ar.inode = inode;
671 ar.goal = goal;
672 ar.len = target;
673 ar.logical = iblock;
674 if (S_ISREG(inode->i_mode))
675 /* enable in-core preallocation only for regular files */
676 ar.flags = EXT4_MB_HINT_DATA;
677
678 current_block = ext4_mb_new_blocks(handle, &ar, err);
679
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400680 if (*err && (target == blks)) {
681 /*
682 * if the allocation failed and we didn't allocate
683 * any blocks before
684 */
685 goto failed_out;
686 }
687 if (!*err) {
688 if (target == blks) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400689 /*
690 * save the new block number
691 * for the first direct block
692 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400693 new_blocks[index] = current_block;
694 }
Theodore Ts'o815a1132009-01-01 23:59:43 -0500695 blk_allocated += ar.len;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400696 }
697allocated:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700698 /* total number of blocks allocated for direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400699 ret = blk_allocated;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700700 *err = 0;
701 return ret;
702failed_out:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400703 for (i = 0; i < index; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500704 ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700705 return ret;
706}
707
708/**
Mingming Cao617ba132006-10-11 01:20:53 -0700709 * ext4_alloc_branch - allocate and set up a chain of blocks.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700710 * @inode: owner
711 * @indirect_blks: number of allocated indirect blocks
712 * @blks: number of allocated direct blocks
713 * @offsets: offsets (in the blocks) to store the pointers to next.
714 * @branch: place to store the chain in.
715 *
716 * This function allocates blocks, zeroes out all but the last one,
717 * links them into chain and (if we are synchronous) writes them to disk.
718 * In other words, it prepares a branch that can be spliced onto the
719 * inode. It stores the information about that chain in the branch[], in
Mingming Cao617ba132006-10-11 01:20:53 -0700720 * the same format as ext4_get_branch() would do. We are calling it after
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700721 * we had read the existing part of chain and partial points to the last
722 * triple of that (one with zero ->key). Upon the exit we have the same
Mingming Cao617ba132006-10-11 01:20:53 -0700723 * picture as after the successful ext4_get_block(), except that in one
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700724 * place chain is disconnected - *branch->p is still zero (we did not
725 * set the last link), but branch->key contains the number that should
726 * be placed into *branch->p to fill that gap.
727 *
728 * If allocation fails we free all blocks we've allocated (and forget
729 * their buffer_heads) and return the error value the from failed
Mingming Cao617ba132006-10-11 01:20:53 -0700730 * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700731 * as described above and return 0.
732 */
Mingming Cao617ba132006-10-11 01:20:53 -0700733static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400734 ext4_lblk_t iblock, int indirect_blks,
735 int *blks, ext4_fsblk_t goal,
736 ext4_lblk_t *offsets, Indirect *branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700737{
738 int blocksize = inode->i_sb->s_blocksize;
739 int i, n = 0;
740 int err = 0;
741 struct buffer_head *bh;
742 int num;
Mingming Cao617ba132006-10-11 01:20:53 -0700743 ext4_fsblk_t new_blocks[4];
744 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700745
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400746 num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700747 *blks, new_blocks, &err);
748 if (err)
749 return err;
750
751 branch[0].key = cpu_to_le32(new_blocks[0]);
752 /*
753 * metadata blocks and data blocks are allocated.
754 */
755 for (n = 1; n <= indirect_blks; n++) {
756 /*
757 * Get buffer_head for parent block, zero it out
758 * and set the pointer to new one, then send
759 * parent to disk.
760 */
761 bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
762 branch[n].bh = bh;
763 lock_buffer(bh);
764 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700765 err = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700766 if (err) {
767 unlock_buffer(bh);
768 brelse(bh);
769 goto failed;
770 }
771
772 memset(bh->b_data, 0, blocksize);
773 branch[n].p = (__le32 *) bh->b_data + offsets[n];
774 branch[n].key = cpu_to_le32(new_blocks[n]);
775 *branch[n].p = branch[n].key;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400776 if (n == indirect_blks) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700777 current_block = new_blocks[n];
778 /*
779 * End of chain, update the last new metablock of
780 * the chain to point to the new allocated
781 * data blocks numbers
782 */
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400783 for (i = 1; i < num; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700784 *(branch[n].p + i) = cpu_to_le32(++current_block);
785 }
786 BUFFER_TRACE(bh, "marking uptodate");
787 set_buffer_uptodate(bh);
788 unlock_buffer(bh);
789
Frank Mayhar03901312009-01-07 00:06:22 -0500790 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
791 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700792 if (err)
793 goto failed;
794 }
795 *blks = num;
796 return err;
797failed:
798 /* Allocation failed, free what we already allocated */
799 for (i = 1; i <= n ; i++) {
Mingming Caodab291a2006-10-11 01:21:01 -0700800 BUFFER_TRACE(branch[i].bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700801 ext4_journal_forget(handle, branch[i].bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700802 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400803 for (i = 0; i < indirect_blks; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500804 ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700805
Alex Tomasc9de5602008-01-29 00:19:52 -0500806 ext4_free_blocks(handle, inode, new_blocks[i], num, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700807
808 return err;
809}
810
811/**
Mingming Cao617ba132006-10-11 01:20:53 -0700812 * ext4_splice_branch - splice the allocated branch onto inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700813 * @inode: owner
814 * @block: (logical) number of block we are adding
815 * @chain: chain of indirect blocks (with a missing link - see
Mingming Cao617ba132006-10-11 01:20:53 -0700816 * ext4_alloc_branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700817 * @where: location of missing link
818 * @num: number of indirect blocks we are adding
819 * @blks: number of direct blocks we are adding
820 *
821 * This function fills the missing link and does all housekeeping needed in
822 * inode (->i_blocks, etc.). In case of success we end up with the full
823 * chain to new block and return 0.
824 */
Mingming Cao617ba132006-10-11 01:20:53 -0700825static int ext4_splice_branch(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400826 ext4_lblk_t block, Indirect *where, int num,
827 int blks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700828{
829 int i;
830 int err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700831 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700832
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700833 /*
834 * If we're splicing into a [td]indirect block (as opposed to the
835 * inode) then we need to get write access to the [td]indirect block
836 * before the splice.
837 */
838 if (where->bh) {
839 BUFFER_TRACE(where->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700840 err = ext4_journal_get_write_access(handle, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700841 if (err)
842 goto err_out;
843 }
844 /* That's it */
845
846 *where->p = where->key;
847
848 /*
849 * Update the host buffer_head or inode to point to more just allocated
850 * direct blocks blocks
851 */
852 if (num == 0 && blks > 1) {
853 current_block = le32_to_cpu(where->key) + 1;
854 for (i = 1; i < blks; i++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400855 *(where->p + i) = cpu_to_le32(current_block++);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700856 }
857
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700858 /* We are done with atomic stuff, now do the rest of housekeeping */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700859 /* had we spliced it onto indirect block? */
860 if (where->bh) {
861 /*
862 * If we spliced it onto an indirect block, we haven't
863 * altered the inode. Note however that if it is being spliced
864 * onto an indirect block at the very end of the file (the
865 * file is growing) then we *will* alter the inode to reflect
866 * the new i_size. But that is not done here - it is done in
Mingming Cao617ba132006-10-11 01:20:53 -0700867 * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700868 */
869 jbd_debug(5, "splicing indirect only\n");
Frank Mayhar03901312009-01-07 00:06:22 -0500870 BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata");
871 err = ext4_handle_dirty_metadata(handle, inode, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700872 if (err)
873 goto err_out;
874 } else {
875 /*
876 * OK, we spliced it into the inode itself on a direct block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700877 */
Theodore Ts'o41591752009-06-15 03:41:23 -0400878 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700879 jbd_debug(5, "splicing direct\n");
880 }
881 return err;
882
883err_out:
884 for (i = 1; i <= num; i++) {
Mingming Caodab291a2006-10-11 01:21:01 -0700885 BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700886 ext4_journal_forget(handle, where[i].bh);
Alex Tomasc9de5602008-01-29 00:19:52 -0500887 ext4_free_blocks(handle, inode,
888 le32_to_cpu(where[i-1].key), 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700889 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500890 ext4_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700891
892 return err;
893}
894
895/*
Theodore Ts'ob920c752009-05-14 00:54:29 -0400896 * The ext4_ind_get_blocks() function handles non-extents inodes
897 * (i.e., using the traditional indirect/double-indirect i_blocks
898 * scheme) for ext4_get_blocks().
899 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700900 * Allocation strategy is simple: if we have to allocate something, we will
901 * have to go the whole way to leaf. So let's do it before attaching anything
902 * to tree, set linkage between the newborn blocks, write them if sync is
903 * required, recheck the path, free and repeat if check fails, otherwise
904 * set the last missing link (that will protect us from any truncate-generated
905 * removals - all blocks on the path are immune now) and possibly force the
906 * write on the parent block.
907 * That has a nice additional property: no special recovery from the failed
908 * allocations is needed - we simply release blocks and do not touch anything
909 * reachable from inode.
910 *
911 * `handle' can be NULL if create == 0.
912 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700913 * return > 0, # of blocks mapped or allocated.
914 * return = 0, if plain lookup failed.
915 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500916 *
Theodore Ts'ob920c752009-05-14 00:54:29 -0400917 * The ext4_ind_get_blocks() function should be called with
918 * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem
919 * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or
920 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system
921 * blocks.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700922 */
Theodore Ts'oe4d996c2009-05-12 00:25:28 -0400923static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400924 ext4_lblk_t iblock, unsigned int maxblocks,
925 struct buffer_head *bh_result,
926 int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700927{
928 int err = -EIO;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500929 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700930 Indirect chain[4];
931 Indirect *partial;
Mingming Cao617ba132006-10-11 01:20:53 -0700932 ext4_fsblk_t goal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700933 int indirect_blks;
934 int blocks_to_boundary = 0;
935 int depth;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700936 int count = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700937 ext4_fsblk_t first_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700938
Alex Tomasa86c6182006-10-11 01:21:03 -0700939 J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL));
Theodore Ts'oc2177052009-05-14 00:58:52 -0400940 J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500941 depth = ext4_block_to_path(inode, iblock, offsets,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400942 &blocks_to_boundary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700943
944 if (depth == 0)
945 goto out;
946
Mingming Cao617ba132006-10-11 01:20:53 -0700947 partial = ext4_get_branch(inode, depth, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700948
949 /* Simplest case - block found, no allocation needed */
950 if (!partial) {
951 first_block = le32_to_cpu(chain[depth - 1].key);
952 clear_buffer_new(bh_result);
953 count++;
954 /*map more blocks*/
955 while (count < maxblocks && count <= blocks_to_boundary) {
Mingming Cao617ba132006-10-11 01:20:53 -0700956 ext4_fsblk_t blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700957
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700958 blk = le32_to_cpu(*(chain[depth-1].p + count));
959
960 if (blk == first_block + count)
961 count++;
962 else
963 break;
964 }
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500965 goto got_it;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700966 }
967
968 /* Next simple case - plain lookup or failed read of indirect block */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400969 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700970 goto cleanup;
971
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700972 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400973 * Okay, we need to do block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700974 */
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800975 goal = ext4_find_goal(inode, iblock, partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700976
977 /* the number of blocks need to allocate for [d,t]indirect blocks */
978 indirect_blks = (chain + depth) - partial - 1;
979
980 /*
981 * Next look up the indirect map to count the totoal number of
982 * direct blocks to allocate for this branch.
983 */
Mingming Cao617ba132006-10-11 01:20:53 -0700984 count = ext4_blks_to_allocate(partial, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700985 maxblocks, blocks_to_boundary);
986 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700987 * Block out ext4_truncate while we alter the tree
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700988 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400989 err = ext4_alloc_branch(handle, inode, iblock, indirect_blks,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400990 &count, goal,
991 offsets + (partial - chain), partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700992
993 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700994 * The ext4_splice_branch call will free and forget any buffers
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700995 * on the new chain if there is a failure, but that risks using
996 * up transaction credits, especially for bitmaps where the
997 * credits cannot be returned. Can we handle this somehow? We
998 * may need to return -EAGAIN upwards in the worst case. --sct
999 */
1000 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07001001 err = ext4_splice_branch(handle, inode, iblock,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001002 partial, indirect_blks, count);
1003 else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001004 goto cleanup;
1005
1006 set_buffer_new(bh_result);
1007got_it:
1008 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
1009 if (count > blocks_to_boundary)
1010 set_buffer_boundary(bh_result);
1011 err = count;
1012 /* Clean up and exit */
1013 partial = chain + depth - 1; /* the whole chain */
1014cleanup:
1015 while (partial > chain) {
1016 BUFFER_TRACE(partial->bh, "call brelse");
1017 brelse(partial->bh);
1018 partial--;
1019 }
1020 BUFFER_TRACE(bh_result, "returned");
1021out:
1022 return err;
1023}
1024
Mingming Cao60e58e02009-01-22 18:13:05 +01001025qsize_t ext4_get_reserved_space(struct inode *inode)
1026{
1027 unsigned long long total;
1028
1029 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1030 total = EXT4_I(inode)->i_reserved_data_blocks +
1031 EXT4_I(inode)->i_reserved_meta_blocks;
1032 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1033
1034 return total;
1035}
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001036/*
1037 * Calculate the number of metadata blocks need to reserve
1038 * to allocate @blocks for non extent file based file
1039 */
1040static int ext4_indirect_calc_metadata_amount(struct inode *inode, int blocks)
1041{
1042 int icap = EXT4_ADDR_PER_BLOCK(inode->i_sb);
1043 int ind_blks, dind_blks, tind_blks;
1044
1045 /* number of new indirect blocks needed */
1046 ind_blks = (blocks + icap - 1) / icap;
1047
1048 dind_blks = (ind_blks + icap - 1) / icap;
1049
1050 tind_blks = 1;
1051
1052 return ind_blks + dind_blks + tind_blks;
1053}
1054
1055/*
1056 * Calculate the number of metadata blocks need to reserve
1057 * to allocate given number of blocks
1058 */
1059static int ext4_calc_metadata_amount(struct inode *inode, int blocks)
1060{
Mingming Caocd213222008-08-19 22:16:59 -04001061 if (!blocks)
1062 return 0;
1063
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001064 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)
1065 return ext4_ext_calc_metadata_amount(inode, blocks);
1066
1067 return ext4_indirect_calc_metadata_amount(inode, blocks);
1068}
1069
1070static void ext4_da_update_reserve_space(struct inode *inode, int used)
1071{
1072 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1073 int total, mdb, mdb_free;
1074
1075 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1076 /* recalculate the number of metablocks still need to be reserved */
1077 total = EXT4_I(inode)->i_reserved_data_blocks - used;
1078 mdb = ext4_calc_metadata_amount(inode, total);
1079
1080 /* figure out how many metablocks to release */
1081 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1082 mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
1083
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001084 if (mdb_free) {
1085 /* Account for allocated meta_blocks */
1086 mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001087
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001088 /* update fs dirty blocks counter */
1089 percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free);
1090 EXT4_I(inode)->i_allocated_meta_blocks = 0;
1091 EXT4_I(inode)->i_reserved_meta_blocks = mdb;
1092 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001093
1094 /* update per-inode reservations */
1095 BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks);
1096 EXT4_I(inode)->i_reserved_data_blocks -= used;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001097 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001098
1099 /*
1100 * free those over-booking quota for metadata blocks
1101 */
Mingming Cao60e58e02009-01-22 18:13:05 +01001102 if (mdb_free)
1103 vfs_dq_release_reservation_block(inode, mdb_free);
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -04001104
1105 /*
1106 * If we have done all the pending block allocations and if
1107 * there aren't any writers on the inode, we can discard the
1108 * inode's preallocations.
1109 */
1110 if (!total && (atomic_read(&inode->i_writecount) == 0))
1111 ext4_discard_preallocations(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001112}
1113
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001114static int check_block_validity(struct inode *inode, sector_t logical,
1115 sector_t phys, int len)
1116{
1117 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), phys, len)) {
1118 ext4_error(inode->i_sb, "check_block_validity",
1119 "inode #%lu logical block %llu mapped to %llu "
1120 "(size %d)", inode->i_ino,
1121 (unsigned long long) logical,
1122 (unsigned long long) phys, len);
1123 WARN_ON(1);
1124 return -EIO;
1125 }
1126 return 0;
1127}
1128
Mingming Caof5ab0d12008-02-25 15:29:55 -05001129/*
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001130 * The ext4_get_blocks() function tries to look up the requested blocks,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001131 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -05001132 *
Mingming Caof5ab0d12008-02-25 15:29:55 -05001133 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
1134 * and store the allocated blocks in the result buffer head and mark it
1135 * mapped.
1136 *
1137 * If file type is extents based, it will call ext4_ext_get_blocks(),
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001138 * Otherwise, call with ext4_ind_get_blocks() to handle indirect mapping
Mingming Caof5ab0d12008-02-25 15:29:55 -05001139 * based files
1140 *
1141 * On success, it returns the number of blocks being mapped or allocate.
1142 * if create==0 and the blocks are pre-allocated and uninitialized block,
1143 * the result buffer head is unmapped. If the create ==1, it will make sure
1144 * the buffer head is mapped.
1145 *
1146 * It returns 0 if plain look up failed (blocks have not been allocated), in
1147 * that casem, buffer head is unmapped
1148 *
1149 * It returns the error in case of allocation failure.
1150 */
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001151int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block,
1152 unsigned int max_blocks, struct buffer_head *bh,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001153 int flags)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001154{
1155 int retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -05001156
1157 clear_buffer_mapped(bh);
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -04001158 clear_buffer_unwritten(bh);
Mingming Caof5ab0d12008-02-25 15:29:55 -05001159
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001160 /*
Theodore Ts'ob920c752009-05-14 00:54:29 -04001161 * Try to see if we can get the block without requesting a new
1162 * file system block.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001163 */
1164 down_read((&EXT4_I(inode)->i_data_sem));
1165 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1166 retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001167 bh, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001168 } else {
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001169 retval = ext4_ind_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001170 bh, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001171 }
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001172 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -05001173
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001174 if (retval > 0 && buffer_mapped(bh)) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001175 int ret = check_block_validity(inode, block,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001176 bh->b_blocknr, retval);
1177 if (ret != 0)
1178 return ret;
1179 }
1180
Mingming Caof5ab0d12008-02-25 15:29:55 -05001181 /* If it is only a block(s) look up */
Theodore Ts'oc2177052009-05-14 00:58:52 -04001182 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001183 return retval;
1184
1185 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001186 * Returns if the blocks have already allocated
1187 *
1188 * Note that if blocks have been preallocated
1189 * ext4_ext_get_block() returns th create = 0
1190 * with buffer head unmapped.
1191 */
1192 if (retval > 0 && buffer_mapped(bh))
1193 return retval;
1194
1195 /*
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -04001196 * When we call get_blocks without the create flag, the
1197 * BH_Unwritten flag could have gotten set if the blocks
1198 * requested were part of a uninitialized extent. We need to
1199 * clear this flag now that we are committed to convert all or
1200 * part of the uninitialized extent to be an initialized
1201 * extent. This is because we need to avoid the combination
1202 * of BH_Unwritten and BH_Mapped flags being simultaneously
1203 * set on the buffer_head.
1204 */
1205 clear_buffer_unwritten(bh);
1206
1207 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001208 * New blocks allocate and/or writing to uninitialized extent
1209 * will possibly result in updating i_data, so we take
1210 * the write lock of i_data_sem, and call get_blocks()
1211 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001212 */
1213 down_write((&EXT4_I(inode)->i_data_sem));
Mingming Caod2a17632008-07-14 17:52:37 -04001214
1215 /*
1216 * if the caller is from delayed allocation writeout path
1217 * we have already reserved fs blocks for allocation
1218 * let the underlying get_block() function know to
1219 * avoid double accounting
1220 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04001221 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Mingming Caod2a17632008-07-14 17:52:37 -04001222 EXT4_I(inode)->i_delalloc_reserved_flag = 1;
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001223 /*
1224 * We need to check for EXT4 here because migrate
1225 * could have changed the inode type in between
1226 */
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001227 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1228 retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001229 bh, flags);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001230 } else {
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001231 retval = ext4_ind_get_blocks(handle, inode, block,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001232 max_blocks, bh, flags);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04001233
1234 if (retval > 0 && buffer_new(bh)) {
1235 /*
1236 * We allocated new blocks which will result in
1237 * i_data's format changing. Force the migrate
1238 * to fail by clearing migrate flags
1239 */
1240 EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags &
1241 ~EXT4_EXT_MIGRATE;
1242 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001243 }
Mingming Caod2a17632008-07-14 17:52:37 -04001244
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001245 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Mingming Caod2a17632008-07-14 17:52:37 -04001246 EXT4_I(inode)->i_delalloc_reserved_flag = 0;
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001247
1248 /*
1249 * Update reserved blocks/metadata blocks after successful
1250 * block allocation which had been deferred till now.
1251 */
1252 if ((retval > 0) && (flags & EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE))
1253 ext4_da_update_reserve_space(inode, retval);
Mingming Caod2a17632008-07-14 17:52:37 -04001254
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001255 up_write((&EXT4_I(inode)->i_data_sem));
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001256 if (retval > 0 && buffer_mapped(bh)) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001257 int ret = check_block_validity(inode, block,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001258 bh->b_blocknr, retval);
1259 if (ret != 0)
1260 return ret;
1261 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001262 return retval;
1263}
1264
Mingming Caof3bd1f32008-08-19 22:16:03 -04001265/* Maximum number of blocks we map for direct IO at once. */
1266#define DIO_MAX_BLOCKS 4096
1267
Eric Sandeen6873fa02008-10-07 00:46:36 -04001268int ext4_get_block(struct inode *inode, sector_t iblock,
1269 struct buffer_head *bh_result, int create)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001270{
Dmitriy Monakhov3e4fdaf2007-02-10 01:46:35 -08001271 handle_t *handle = ext4_journal_current_handle();
Jan Kara7fb54092008-02-10 01:08:38 -05001272 int ret = 0, started = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001273 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001274 int dio_credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001275
Jan Kara7fb54092008-02-10 01:08:38 -05001276 if (create && !handle) {
1277 /* Direct IO write... */
1278 if (max_blocks > DIO_MAX_BLOCKS)
1279 max_blocks = DIO_MAX_BLOCKS;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001280 dio_credits = ext4_chunk_trans_blocks(inode, max_blocks);
1281 handle = ext4_journal_start(inode, dio_credits);
Jan Kara7fb54092008-02-10 01:08:38 -05001282 if (IS_ERR(handle)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001283 ret = PTR_ERR(handle);
Jan Kara7fb54092008-02-10 01:08:38 -05001284 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001285 }
Jan Kara7fb54092008-02-10 01:08:38 -05001286 started = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001287 }
1288
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001289 ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001290 create ? EXT4_GET_BLOCKS_CREATE : 0);
Jan Kara7fb54092008-02-10 01:08:38 -05001291 if (ret > 0) {
1292 bh_result->b_size = (ret << inode->i_blkbits);
1293 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001294 }
Jan Kara7fb54092008-02-10 01:08:38 -05001295 if (started)
1296 ext4_journal_stop(handle);
1297out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001298 return ret;
1299}
1300
1301/*
1302 * `handle' can be NULL if create is zero
1303 */
Mingming Cao617ba132006-10-11 01:20:53 -07001304struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001305 ext4_lblk_t block, int create, int *errp)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001306{
1307 struct buffer_head dummy;
1308 int fatal = 0, err;
Jan Kara03f5d8b2009-06-09 00:17:05 -04001309 int flags = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001310
1311 J_ASSERT(handle != NULL || create == 0);
1312
1313 dummy.b_state = 0;
1314 dummy.b_blocknr = -1000;
1315 buffer_trace_init(&dummy.b_history);
Theodore Ts'oc2177052009-05-14 00:58:52 -04001316 if (create)
1317 flags |= EXT4_GET_BLOCKS_CREATE;
1318 err = ext4_get_blocks(handle, inode, block, 1, &dummy, flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001319 /*
Theodore Ts'oc2177052009-05-14 00:58:52 -04001320 * ext4_get_blocks() returns number of blocks mapped. 0 in
1321 * case of a HOLE.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001322 */
1323 if (err > 0) {
1324 if (err > 1)
1325 WARN_ON(1);
1326 err = 0;
1327 }
1328 *errp = err;
1329 if (!err && buffer_mapped(&dummy)) {
1330 struct buffer_head *bh;
1331 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
1332 if (!bh) {
1333 *errp = -EIO;
1334 goto err;
1335 }
1336 if (buffer_new(&dummy)) {
1337 J_ASSERT(create != 0);
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001338 J_ASSERT(handle != NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001339
1340 /*
1341 * Now that we do not always journal data, we should
1342 * keep in mind whether this should always journal the
1343 * new buffer as metadata. For now, regular file
Mingming Cao617ba132006-10-11 01:20:53 -07001344 * writes use ext4_get_block instead, so it's not a
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001345 * problem.
1346 */
1347 lock_buffer(bh);
1348 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001349 fatal = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001350 if (!fatal && !buffer_uptodate(bh)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001351 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001352 set_buffer_uptodate(bh);
1353 }
1354 unlock_buffer(bh);
Frank Mayhar03901312009-01-07 00:06:22 -05001355 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
1356 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001357 if (!fatal)
1358 fatal = err;
1359 } else {
1360 BUFFER_TRACE(bh, "not a new buffer");
1361 }
1362 if (fatal) {
1363 *errp = fatal;
1364 brelse(bh);
1365 bh = NULL;
1366 }
1367 return bh;
1368 }
1369err:
1370 return NULL;
1371}
1372
Mingming Cao617ba132006-10-11 01:20:53 -07001373struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001374 ext4_lblk_t block, int create, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001375{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001376 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001377
Mingming Cao617ba132006-10-11 01:20:53 -07001378 bh = ext4_getblk(handle, inode, block, create, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001379 if (!bh)
1380 return bh;
1381 if (buffer_uptodate(bh))
1382 return bh;
1383 ll_rw_block(READ_META, 1, &bh);
1384 wait_on_buffer(bh);
1385 if (buffer_uptodate(bh))
1386 return bh;
1387 put_bh(bh);
1388 *err = -EIO;
1389 return NULL;
1390}
1391
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001392static int walk_page_buffers(handle_t *handle,
1393 struct buffer_head *head,
1394 unsigned from,
1395 unsigned to,
1396 int *partial,
1397 int (*fn)(handle_t *handle,
1398 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001399{
1400 struct buffer_head *bh;
1401 unsigned block_start, block_end;
1402 unsigned blocksize = head->b_size;
1403 int err, ret = 0;
1404 struct buffer_head *next;
1405
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001406 for (bh = head, block_start = 0;
1407 ret == 0 && (bh != head || !block_start);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001408 block_start = block_end, bh = next) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001409 next = bh->b_this_page;
1410 block_end = block_start + blocksize;
1411 if (block_end <= from || block_start >= to) {
1412 if (partial && !buffer_uptodate(bh))
1413 *partial = 1;
1414 continue;
1415 }
1416 err = (*fn)(handle, bh);
1417 if (!ret)
1418 ret = err;
1419 }
1420 return ret;
1421}
1422
1423/*
1424 * To preserve ordering, it is essential that the hole instantiation and
1425 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -07001426 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -07001427 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001428 * prepare_write() is the right place.
1429 *
Mingming Cao617ba132006-10-11 01:20:53 -07001430 * Also, this function can nest inside ext4_writepage() ->
1431 * block_write_full_page(). In that case, we *know* that ext4_writepage()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001432 * has generated enough buffer credits to do the whole page. So we won't
1433 * block on the journal in that case, which is good, because the caller may
1434 * be PF_MEMALLOC.
1435 *
Mingming Cao617ba132006-10-11 01:20:53 -07001436 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001437 * quota file writes. If we were to commit the transaction while thus
1438 * reentered, there can be a deadlock - we would be holding a quota
1439 * lock, and the commit would never complete if another thread had a
1440 * transaction open and was blocking on the quota lock - a ranking
1441 * violation.
1442 *
Mingming Caodab291a2006-10-11 01:21:01 -07001443 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001444 * will _not_ run commit under these circumstances because handle->h_ref
1445 * is elevated. We'll still have enough credits for the tiny quotafile
1446 * write.
1447 */
1448static int do_journal_get_write_access(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001449 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001450{
1451 if (!buffer_mapped(bh) || buffer_freed(bh))
1452 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001453 return ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001454}
1455
Nick Pigginbfc1af62007-10-16 01:25:05 -07001456static int ext4_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001457 loff_t pos, unsigned len, unsigned flags,
1458 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001459{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001460 struct inode *inode = mapping->host;
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001461 int ret, needed_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001462 handle_t *handle;
1463 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001464 struct page *page;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001465 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001466 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001467
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001468 trace_ext4_write_begin(inode, pos, len, flags);
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001469 /*
1470 * Reserve one block more for addition to orphan list in case
1471 * we allocate blocks but write fails for some reason
1472 */
1473 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001474 index = pos >> PAGE_CACHE_SHIFT;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001475 from = pos & (PAGE_CACHE_SIZE - 1);
1476 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001477
1478retry:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001479 handle = ext4_journal_start(inode, needed_blocks);
1480 if (IS_ERR(handle)) {
1481 ret = PTR_ERR(handle);
1482 goto out;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001483 }
1484
Jan Karaebd36102009-02-22 21:09:59 -05001485 /* We cannot recurse into the filesystem as the transaction is already
1486 * started */
1487 flags |= AOP_FLAG_NOFS;
1488
Nick Piggin54566b22009-01-04 12:00:53 -08001489 page = grab_cache_page_write_begin(mapping, index, flags);
Jan Karacf108bc2008-07-11 19:27:31 -04001490 if (!page) {
1491 ext4_journal_stop(handle);
1492 ret = -ENOMEM;
1493 goto out;
1494 }
1495 *pagep = page;
1496
Nick Pigginbfc1af62007-10-16 01:25:05 -07001497 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
Jan Karaebd36102009-02-22 21:09:59 -05001498 ext4_get_block);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001499
1500 if (!ret && ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001501 ret = walk_page_buffers(handle, page_buffers(page),
1502 from, to, NULL, do_journal_get_write_access);
1503 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001504
1505 if (ret) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001506 unlock_page(page);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001507 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001508 /*
1509 * block_write_begin may have instantiated a few blocks
1510 * outside i_size. Trim these off again. Don't need
1511 * i_size_read because we hold i_mutex.
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001512 *
1513 * Add inode to orphan list in case we crash before
1514 * truncate finishes
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001515 */
Jan Karaffacfa72009-07-13 16:22:22 -04001516 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001517 ext4_orphan_add(handle, inode);
1518
1519 ext4_journal_stop(handle);
1520 if (pos + len > inode->i_size) {
Jan Karaffacfa72009-07-13 16:22:22 -04001521 ext4_truncate(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001522 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001523 * If truncate failed early the inode might
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001524 * still be on the orphan list; we need to
1525 * make sure the inode is removed from the
1526 * orphan list in that case.
1527 */
1528 if (inode->i_nlink)
1529 ext4_orphan_del(NULL, inode);
1530 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001531 }
1532
Mingming Cao617ba132006-10-11 01:20:53 -07001533 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001534 goto retry;
Andrew Morton7479d2b2007-04-01 23:49:44 -07001535out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001536 return ret;
1537}
1538
Nick Pigginbfc1af62007-10-16 01:25:05 -07001539/* For write_end() in data=journal mode */
1540static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001541{
1542 if (!buffer_mapped(bh) || buffer_freed(bh))
1543 return 0;
1544 set_buffer_uptodate(bh);
Frank Mayhar03901312009-01-07 00:06:22 -05001545 return ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001546}
1547
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001548static int ext4_generic_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001549 struct address_space *mapping,
1550 loff_t pos, unsigned len, unsigned copied,
1551 struct page *page, void *fsdata)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001552{
1553 int i_size_changed = 0;
1554 struct inode *inode = mapping->host;
1555 handle_t *handle = ext4_journal_current_handle();
1556
1557 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
1558
1559 /*
1560 * No need to use i_size_read() here, the i_size
1561 * cannot change under us because we hold i_mutex.
1562 *
1563 * But it's important to update i_size while still holding page lock:
1564 * page writeout could otherwise come in and zero beyond i_size.
1565 */
1566 if (pos + copied > inode->i_size) {
1567 i_size_write(inode, pos + copied);
1568 i_size_changed = 1;
1569 }
1570
1571 if (pos + copied > EXT4_I(inode)->i_disksize) {
1572 /* We need to mark inode dirty even if
1573 * new_i_size is less that inode->i_size
1574 * bu greater than i_disksize.(hint delalloc)
1575 */
1576 ext4_update_i_disksize(inode, (pos + copied));
1577 i_size_changed = 1;
1578 }
1579 unlock_page(page);
1580 page_cache_release(page);
1581
1582 /*
1583 * Don't mark the inode dirty under page lock. First, it unnecessarily
1584 * makes the holding time of page lock longer. Second, it forces lock
1585 * ordering of page lock and transaction start for journaling
1586 * filesystems.
1587 */
1588 if (i_size_changed)
1589 ext4_mark_inode_dirty(handle, inode);
1590
1591 return copied;
1592}
1593
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001594/*
1595 * We need to pick up the new inode size which generic_commit_write gave us
1596 * `file' can be NULL - eg, when called from page_symlink().
1597 *
Mingming Cao617ba132006-10-11 01:20:53 -07001598 * ext4 never places buffers on inode->i_mapping->private_list. metadata
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001599 * buffers are managed internally.
1600 */
Nick Pigginbfc1af62007-10-16 01:25:05 -07001601static int ext4_ordered_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001602 struct address_space *mapping,
1603 loff_t pos, unsigned len, unsigned copied,
1604 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001605{
Mingming Cao617ba132006-10-11 01:20:53 -07001606 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001607 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001608 int ret = 0, ret2;
1609
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001610 trace_ext4_ordered_write_end(inode, pos, len, copied);
Jan Kara678aaf42008-07-11 19:27:31 -04001611 ret = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001612
1613 if (ret == 0) {
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001614 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001615 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001616 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -04001617 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001618 /* if we have allocated more blocks and copied
1619 * less. We will have blocks allocated outside
1620 * inode->i_size. So truncate them
1621 */
1622 ext4_orphan_add(handle, inode);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001623 if (ret2 < 0)
1624 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001625 }
Mingming Cao617ba132006-10-11 01:20:53 -07001626 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001627 if (!ret)
1628 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001629
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001630 if (pos + len > inode->i_size) {
Jan Karaffacfa72009-07-13 16:22:22 -04001631 ext4_truncate(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001632 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001633 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001634 * on the orphan list; we need to make sure the inode
1635 * is removed from the orphan list in that case.
1636 */
1637 if (inode->i_nlink)
1638 ext4_orphan_del(NULL, inode);
1639 }
1640
1641
Nick Pigginbfc1af62007-10-16 01:25:05 -07001642 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001643}
1644
Nick Pigginbfc1af62007-10-16 01:25:05 -07001645static int ext4_writeback_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001646 struct address_space *mapping,
1647 loff_t pos, unsigned len, unsigned copied,
1648 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001649{
Mingming Cao617ba132006-10-11 01:20:53 -07001650 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001651 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001652 int ret = 0, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001653
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001654 trace_ext4_writeback_write_end(inode, pos, len, copied);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001655 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001656 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001657 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -04001658 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001659 /* if we have allocated more blocks and copied
1660 * less. We will have blocks allocated outside
1661 * inode->i_size. So truncate them
1662 */
1663 ext4_orphan_add(handle, inode);
1664
Roel Kluinf8a87d82008-04-29 22:01:18 -04001665 if (ret2 < 0)
1666 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001667
Mingming Cao617ba132006-10-11 01:20:53 -07001668 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001669 if (!ret)
1670 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001671
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001672 if (pos + len > inode->i_size) {
Jan Karaffacfa72009-07-13 16:22:22 -04001673 ext4_truncate(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001674 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001675 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001676 * on the orphan list; we need to make sure the inode
1677 * is removed from the orphan list in that case.
1678 */
1679 if (inode->i_nlink)
1680 ext4_orphan_del(NULL, inode);
1681 }
1682
Nick Pigginbfc1af62007-10-16 01:25:05 -07001683 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001684}
1685
Nick Pigginbfc1af62007-10-16 01:25:05 -07001686static int ext4_journalled_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001687 struct address_space *mapping,
1688 loff_t pos, unsigned len, unsigned copied,
1689 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001690{
Mingming Cao617ba132006-10-11 01:20:53 -07001691 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001692 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001693 int ret = 0, ret2;
1694 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001695 unsigned from, to;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001696 loff_t new_i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001697
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001698 trace_ext4_journalled_write_end(inode, pos, len, copied);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001699 from = pos & (PAGE_CACHE_SIZE - 1);
1700 to = from + len;
1701
1702 if (copied < len) {
1703 if (!PageUptodate(page))
1704 copied = 0;
1705 page_zero_new_buffers(page, from+copied, to);
1706 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001707
1708 ret = walk_page_buffers(handle, page_buffers(page), from,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001709 to, &partial, write_end_fn);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001710 if (!partial)
1711 SetPageUptodate(page);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001712 new_i_size = pos + copied;
1713 if (new_i_size > inode->i_size)
Nick Pigginbfc1af62007-10-16 01:25:05 -07001714 i_size_write(inode, pos+copied);
Mingming Cao617ba132006-10-11 01:20:53 -07001715 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001716 if (new_i_size > EXT4_I(inode)->i_disksize) {
1717 ext4_update_i_disksize(inode, new_i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001718 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001719 if (!ret)
1720 ret = ret2;
1721 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001722
Jan Karacf108bc2008-07-11 19:27:31 -04001723 unlock_page(page);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001724 page_cache_release(page);
Jan Karaffacfa72009-07-13 16:22:22 -04001725 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001726 /* if we have allocated more blocks and copied
1727 * less. We will have blocks allocated outside
1728 * inode->i_size. So truncate them
1729 */
1730 ext4_orphan_add(handle, inode);
1731
Mingming Cao617ba132006-10-11 01:20:53 -07001732 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001733 if (!ret)
1734 ret = ret2;
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001735 if (pos + len > inode->i_size) {
Jan Karaffacfa72009-07-13 16:22:22 -04001736 ext4_truncate(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001737 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001738 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001739 * on the orphan list; we need to make sure the inode
1740 * is removed from the orphan list in that case.
1741 */
1742 if (inode->i_nlink)
1743 ext4_orphan_del(NULL, inode);
1744 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001745
1746 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001747}
Mingming Caod2a17632008-07-14 17:52:37 -04001748
1749static int ext4_da_reserve_space(struct inode *inode, int nrblocks)
1750{
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001751 int retries = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01001752 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1753 unsigned long md_needed, mdblocks, total = 0;
Mingming Caod2a17632008-07-14 17:52:37 -04001754
1755 /*
1756 * recalculate the amount of metadata blocks to reserve
1757 * in order to allocate nrblocks
1758 * worse case is one extent per block
1759 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001760repeat:
Mingming Caod2a17632008-07-14 17:52:37 -04001761 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1762 total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks;
1763 mdblocks = ext4_calc_metadata_amount(inode, total);
1764 BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks);
1765
1766 md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks;
1767 total = md_needed + nrblocks;
1768
Mingming Cao60e58e02009-01-22 18:13:05 +01001769 /*
1770 * Make quota reservation here to prevent quota overflow
1771 * later. Real quota accounting is done at pages writeout
1772 * time.
1773 */
1774 if (vfs_dq_reserve_block(inode, total)) {
1775 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1776 return -EDQUOT;
1777 }
1778
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04001779 if (ext4_claim_free_blocks(sbi, total)) {
Mingming Caod2a17632008-07-14 17:52:37 -04001780 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001781 if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1782 yield();
1783 goto repeat;
1784 }
Mingming Cao60e58e02009-01-22 18:13:05 +01001785 vfs_dq_release_reservation_block(inode, total);
Mingming Caod2a17632008-07-14 17:52:37 -04001786 return -ENOSPC;
1787 }
Mingming Caod2a17632008-07-14 17:52:37 -04001788 EXT4_I(inode)->i_reserved_data_blocks += nrblocks;
1789 EXT4_I(inode)->i_reserved_meta_blocks = mdblocks;
1790
1791 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1792 return 0; /* success */
1793}
1794
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001795static void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001796{
1797 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1798 int total, mdb, mdb_free, release;
1799
Mingming Caocd213222008-08-19 22:16:59 -04001800 if (!to_free)
1801 return; /* Nothing to release, exit */
1802
Mingming Caod2a17632008-07-14 17:52:37 -04001803 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001804
1805 if (!EXT4_I(inode)->i_reserved_data_blocks) {
1806 /*
1807 * if there is no reserved blocks, but we try to free some
1808 * then the counter is messed up somewhere.
1809 * but since this function is called from invalidate
1810 * page, it's harmless to return without any action
1811 */
1812 printk(KERN_INFO "ext4 delalloc try to release %d reserved "
1813 "blocks for inode %lu, but there is no reserved "
1814 "data blocks\n", to_free, inode->i_ino);
1815 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1816 return;
1817 }
1818
Mingming Caod2a17632008-07-14 17:52:37 -04001819 /* recalculate the number of metablocks still need to be reserved */
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001820 total = EXT4_I(inode)->i_reserved_data_blocks - to_free;
Mingming Caod2a17632008-07-14 17:52:37 -04001821 mdb = ext4_calc_metadata_amount(inode, total);
1822
1823 /* figure out how many metablocks to release */
1824 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1825 mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
1826
Mingming Caod2a17632008-07-14 17:52:37 -04001827 release = to_free + mdb_free;
1828
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001829 /* update fs dirty blocks counter for truncate case */
1830 percpu_counter_sub(&sbi->s_dirtyblocks_counter, release);
Mingming Caod2a17632008-07-14 17:52:37 -04001831
1832 /* update per-inode reservations */
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001833 BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks);
1834 EXT4_I(inode)->i_reserved_data_blocks -= to_free;
Mingming Caod2a17632008-07-14 17:52:37 -04001835
1836 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1837 EXT4_I(inode)->i_reserved_meta_blocks = mdb;
Mingming Caod2a17632008-07-14 17:52:37 -04001838 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001839
1840 vfs_dq_release_reservation_block(inode, release);
Mingming Caod2a17632008-07-14 17:52:37 -04001841}
1842
1843static void ext4_da_page_release_reservation(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001844 unsigned long offset)
Mingming Caod2a17632008-07-14 17:52:37 -04001845{
1846 int to_release = 0;
1847 struct buffer_head *head, *bh;
1848 unsigned int curr_off = 0;
1849
1850 head = page_buffers(page);
1851 bh = head;
1852 do {
1853 unsigned int next_off = curr_off + bh->b_size;
1854
1855 if ((offset <= curr_off) && (buffer_delay(bh))) {
1856 to_release++;
1857 clear_buffer_delay(bh);
1858 }
1859 curr_off = next_off;
1860 } while ((bh = bh->b_this_page) != head);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001861 ext4_da_release_space(page->mapping->host, to_release);
Mingming Caod2a17632008-07-14 17:52:37 -04001862}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001863
1864/*
Alex Tomas64769242008-07-11 19:27:31 -04001865 * Delayed allocation stuff
1866 */
1867
1868struct mpage_da_data {
1869 struct inode *inode;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001870 sector_t b_blocknr; /* start block number of extent */
1871 size_t b_size; /* size of extent */
1872 unsigned long b_state; /* state of the extent */
Alex Tomas64769242008-07-11 19:27:31 -04001873 unsigned long first_page, next_page; /* extent of pages */
Alex Tomas64769242008-07-11 19:27:31 -04001874 struct writeback_control *wbc;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001875 int io_done;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001876 int pages_written;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001877 int retval;
Alex Tomas64769242008-07-11 19:27:31 -04001878};
1879
1880/*
1881 * mpage_da_submit_io - walks through extent of pages and try to write
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001882 * them with writepage() call back
Alex Tomas64769242008-07-11 19:27:31 -04001883 *
1884 * @mpd->inode: inode
1885 * @mpd->first_page: first page of the extent
1886 * @mpd->next_page: page after the last page of the extent
Alex Tomas64769242008-07-11 19:27:31 -04001887 *
1888 * By the time mpage_da_submit_io() is called we expect all blocks
1889 * to be allocated. this may be wrong if allocation failed.
1890 *
1891 * As pages are already locked by write_cache_pages(), we can't use it
1892 */
1893static int mpage_da_submit_io(struct mpage_da_data *mpd)
1894{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001895 long pages_skipped;
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001896 struct pagevec pvec;
1897 unsigned long index, end;
1898 int ret = 0, err, nr_pages, i;
1899 struct inode *inode = mpd->inode;
1900 struct address_space *mapping = inode->i_mapping;
Alex Tomas64769242008-07-11 19:27:31 -04001901
1902 BUG_ON(mpd->next_page <= mpd->first_page);
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001903 /*
1904 * We need to start from the first_page to the next_page - 1
1905 * to make sure we also write the mapped dirty buffer_heads.
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001906 * If we look at mpd->b_blocknr we would only be looking
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001907 * at the currently mapped buffer_heads.
1908 */
Alex Tomas64769242008-07-11 19:27:31 -04001909 index = mpd->first_page;
1910 end = mpd->next_page - 1;
1911
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001912 pagevec_init(&pvec, 0);
Alex Tomas64769242008-07-11 19:27:31 -04001913 while (index <= end) {
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001914 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
Alex Tomas64769242008-07-11 19:27:31 -04001915 if (nr_pages == 0)
1916 break;
1917 for (i = 0; i < nr_pages; i++) {
1918 struct page *page = pvec.pages[i];
1919
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001920 index = page->index;
1921 if (index > end)
1922 break;
1923 index++;
1924
1925 BUG_ON(!PageLocked(page));
1926 BUG_ON(PageWriteback(page));
1927
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001928 pages_skipped = mpd->wbc->pages_skipped;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001929 err = mapping->a_ops->writepage(page, mpd->wbc);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001930 if (!err && (pages_skipped == mpd->wbc->pages_skipped))
1931 /*
1932 * have successfully written the page
1933 * without skipping the same
1934 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001935 mpd->pages_written++;
Alex Tomas64769242008-07-11 19:27:31 -04001936 /*
1937 * In error case, we have to continue because
1938 * remaining pages are still locked
1939 * XXX: unlock and re-dirty them?
1940 */
1941 if (ret == 0)
1942 ret = err;
1943 }
1944 pagevec_release(&pvec);
1945 }
Alex Tomas64769242008-07-11 19:27:31 -04001946 return ret;
1947}
1948
1949/*
1950 * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers
1951 *
1952 * @mpd->inode - inode to walk through
1953 * @exbh->b_blocknr - first block on a disk
1954 * @exbh->b_size - amount of space in bytes
1955 * @logical - first logical block to start assignment with
1956 *
1957 * the function goes through all passed space and put actual disk
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001958 * block numbers into buffer heads, dropping BH_Delay and BH_Unwritten
Alex Tomas64769242008-07-11 19:27:31 -04001959 */
1960static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical,
1961 struct buffer_head *exbh)
1962{
1963 struct inode *inode = mpd->inode;
1964 struct address_space *mapping = inode->i_mapping;
1965 int blocks = exbh->b_size >> inode->i_blkbits;
1966 sector_t pblock = exbh->b_blocknr, cur_logical;
1967 struct buffer_head *head, *bh;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001968 pgoff_t index, end;
Alex Tomas64769242008-07-11 19:27:31 -04001969 struct pagevec pvec;
1970 int nr_pages, i;
1971
1972 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
1973 end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
1974 cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1975
1976 pagevec_init(&pvec, 0);
1977
1978 while (index <= end) {
1979 /* XXX: optimize tail */
1980 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1981 if (nr_pages == 0)
1982 break;
1983 for (i = 0; i < nr_pages; i++) {
1984 struct page *page = pvec.pages[i];
1985
1986 index = page->index;
1987 if (index > end)
1988 break;
1989 index++;
1990
1991 BUG_ON(!PageLocked(page));
1992 BUG_ON(PageWriteback(page));
1993 BUG_ON(!page_has_buffers(page));
1994
1995 bh = page_buffers(page);
1996 head = bh;
1997
1998 /* skip blocks out of the range */
1999 do {
2000 if (cur_logical >= logical)
2001 break;
2002 cur_logical++;
2003 } while ((bh = bh->b_this_page) != head);
2004
2005 do {
2006 if (cur_logical >= logical + blocks)
2007 break;
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002008
2009 if (buffer_delay(bh) ||
2010 buffer_unwritten(bh)) {
2011
2012 BUG_ON(bh->b_bdev != inode->i_sb->s_bdev);
2013
2014 if (buffer_delay(bh)) {
2015 clear_buffer_delay(bh);
2016 bh->b_blocknr = pblock;
2017 } else {
2018 /*
2019 * unwritten already should have
2020 * blocknr assigned. Verify that
2021 */
2022 clear_buffer_unwritten(bh);
2023 BUG_ON(bh->b_blocknr != pblock);
2024 }
2025
Mingming Cao61628a32008-07-11 19:27:31 -04002026 } else if (buffer_mapped(bh))
Alex Tomas64769242008-07-11 19:27:31 -04002027 BUG_ON(bh->b_blocknr != pblock);
Alex Tomas64769242008-07-11 19:27:31 -04002028
2029 cur_logical++;
2030 pblock++;
2031 } while ((bh = bh->b_this_page) != head);
2032 }
2033 pagevec_release(&pvec);
2034 }
2035}
2036
2037
2038/*
2039 * __unmap_underlying_blocks - just a helper function to unmap
2040 * set of blocks described by @bh
2041 */
2042static inline void __unmap_underlying_blocks(struct inode *inode,
2043 struct buffer_head *bh)
2044{
2045 struct block_device *bdev = inode->i_sb->s_bdev;
2046 int blocks, i;
2047
2048 blocks = bh->b_size >> inode->i_blkbits;
2049 for (i = 0; i < blocks; i++)
2050 unmap_underlying_metadata(bdev, bh->b_blocknr + i);
2051}
2052
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002053static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd,
2054 sector_t logical, long blk_cnt)
2055{
2056 int nr_pages, i;
2057 pgoff_t index, end;
2058 struct pagevec pvec;
2059 struct inode *inode = mpd->inode;
2060 struct address_space *mapping = inode->i_mapping;
2061
2062 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
2063 end = (logical + blk_cnt - 1) >>
2064 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2065 while (index <= end) {
2066 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
2067 if (nr_pages == 0)
2068 break;
2069 for (i = 0; i < nr_pages; i++) {
2070 struct page *page = pvec.pages[i];
2071 index = page->index;
2072 if (index > end)
2073 break;
2074 index++;
2075
2076 BUG_ON(!PageLocked(page));
2077 BUG_ON(PageWriteback(page));
2078 block_invalidatepage(page, 0);
2079 ClearPageUptodate(page);
2080 unlock_page(page);
2081 }
2082 }
2083 return;
2084}
2085
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002086static void ext4_print_free_blocks(struct inode *inode)
2087{
2088 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2089 printk(KERN_EMERG "Total free blocks count %lld\n",
2090 ext4_count_free_blocks(inode->i_sb));
2091 printk(KERN_EMERG "Free/Dirty block details\n");
2092 printk(KERN_EMERG "free_blocks=%lld\n",
Alexander Beregalov8f72fbd2008-10-29 17:13:08 -04002093 (long long)percpu_counter_sum(&sbi->s_freeblocks_counter));
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002094 printk(KERN_EMERG "dirty_blocks=%lld\n",
Alexander Beregalov8f72fbd2008-10-29 17:13:08 -04002095 (long long)percpu_counter_sum(&sbi->s_dirtyblocks_counter));
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002096 printk(KERN_EMERG "Block reservation details\n");
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002097 printk(KERN_EMERG "i_reserved_data_blocks=%u\n",
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002098 EXT4_I(inode)->i_reserved_data_blocks);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002099 printk(KERN_EMERG "i_reserved_meta_blocks=%u\n",
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002100 EXT4_I(inode)->i_reserved_meta_blocks);
2101 return;
2102}
2103
Theodore Ts'ob920c752009-05-14 00:54:29 -04002104/*
Alex Tomas64769242008-07-11 19:27:31 -04002105 * mpage_da_map_blocks - go through given space
2106 *
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002107 * @mpd - bh describing space
Alex Tomas64769242008-07-11 19:27:31 -04002108 *
2109 * The function skips space we know is already mapped to disk blocks.
2110 *
Alex Tomas64769242008-07-11 19:27:31 -04002111 */
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002112static int mpage_da_map_blocks(struct mpage_da_data *mpd)
Alex Tomas64769242008-07-11 19:27:31 -04002113{
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002114 int err, blks, get_blocks_flags;
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002115 struct buffer_head new;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002116 sector_t next = mpd->b_blocknr;
2117 unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
2118 loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
2119 handle_t *handle = NULL;
Alex Tomas64769242008-07-11 19:27:31 -04002120
2121 /*
2122 * We consider only non-mapped and non-allocated blocks
2123 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002124 if ((mpd->b_state & (1 << BH_Mapped)) &&
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002125 !(mpd->b_state & (1 << BH_Delay)) &&
2126 !(mpd->b_state & (1 << BH_Unwritten)))
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002127 return 0;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002128
2129 /*
2130 * If we didn't accumulate anything to write simply return
2131 */
2132 if (!mpd->b_size)
2133 return 0;
2134
2135 handle = ext4_journal_current_handle();
2136 BUG_ON(!handle);
2137
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04002138 /*
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002139 * Call ext4_get_blocks() to allocate any delayed allocation
2140 * blocks, or to convert an uninitialized extent to be
2141 * initialized (in the case where we have written into
2142 * one or more preallocated blocks).
2143 *
2144 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
2145 * indicate that we are on the delayed allocation path. This
2146 * affects functions in many different parts of the allocation
2147 * call path. This flag exists primarily because we don't
2148 * want to change *many* call functions, so ext4_get_blocks()
2149 * will set the magic i_delalloc_reserved_flag once the
2150 * inode's allocation semaphore is taken.
2151 *
2152 * If the blocks in questions were delalloc blocks, set
2153 * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
2154 * variables are updated after the blocks have been allocated.
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04002155 */
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002156 new.b_state = 0;
2157 get_blocks_flags = (EXT4_GET_BLOCKS_CREATE |
2158 EXT4_GET_BLOCKS_DELALLOC_RESERVE);
2159 if (mpd->b_state & (1 << BH_Delay))
2160 get_blocks_flags |= EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002161 blks = ext4_get_blocks(handle, mpd->inode, next, max_blocks,
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002162 &new, get_blocks_flags);
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002163 if (blks < 0) {
2164 err = blks;
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002165 /*
2166 * If get block returns with error we simply
2167 * return. Later writepage will redirty the page and
2168 * writepages will find the dirty page again
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002169 */
2170 if (err == -EAGAIN)
2171 return 0;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002172
2173 if (err == -ENOSPC &&
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002174 ext4_count_free_blocks(mpd->inode->i_sb)) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002175 mpd->retval = err;
2176 return 0;
2177 }
2178
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002179 /*
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002180 * get block failure will cause us to loop in
2181 * writepages, because a_ops->writepage won't be able
2182 * to make progress. The page will be redirtied by
2183 * writepage and writepages will again try to write
2184 * the same.
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002185 */
2186 printk(KERN_EMERG "%s block allocation failed for inode %lu "
2187 "at logical offset %llu with max blocks "
2188 "%zd with error %d\n",
2189 __func__, mpd->inode->i_ino,
2190 (unsigned long long)next,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002191 mpd->b_size >> mpd->inode->i_blkbits, err);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002192 printk(KERN_EMERG "This should not happen.!! "
2193 "Data will be lost\n");
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002194 if (err == -ENOSPC) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002195 ext4_print_free_blocks(mpd->inode);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002196 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002197 /* invalidate all the pages */
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002198 ext4_da_block_invalidatepages(mpd, next,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002199 mpd->b_size >> mpd->inode->i_blkbits);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002200 return err;
2201 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002202 BUG_ON(blks == 0);
2203
2204 new.b_size = (blks << mpd->inode->i_blkbits);
Alex Tomas64769242008-07-11 19:27:31 -04002205
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002206 if (buffer_new(&new))
2207 __unmap_underlying_blocks(mpd->inode, &new);
Alex Tomas64769242008-07-11 19:27:31 -04002208
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002209 /*
2210 * If blocks are delayed marked, we need to
2211 * put actual blocknr and drop delayed bit
2212 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002213 if ((mpd->b_state & (1 << BH_Delay)) ||
2214 (mpd->b_state & (1 << BH_Unwritten)))
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002215 mpage_put_bnr_to_bhs(mpd, next, &new);
2216
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002217 if (ext4_should_order_data(mpd->inode)) {
2218 err = ext4_jbd2_file_inode(handle, mpd->inode);
2219 if (err)
2220 return err;
2221 }
2222
2223 /*
Jan Kara03f5d8b2009-06-09 00:17:05 -04002224 * Update on-disk size along with block allocation.
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002225 */
2226 disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
2227 if (disksize > i_size_read(mpd->inode))
2228 disksize = i_size_read(mpd->inode);
2229 if (disksize > EXT4_I(mpd->inode)->i_disksize) {
2230 ext4_update_i_disksize(mpd->inode, disksize);
2231 return ext4_mark_inode_dirty(handle, mpd->inode);
2232 }
2233
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002234 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04002235}
2236
Aneesh Kumar K.Vbf068ee2008-08-19 22:16:43 -04002237#define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
2238 (1 << BH_Delay) | (1 << BH_Unwritten))
Alex Tomas64769242008-07-11 19:27:31 -04002239
2240/*
2241 * mpage_add_bh_to_extent - try to add one more block to extent of blocks
2242 *
2243 * @mpd->lbh - extent of blocks
2244 * @logical - logical number of the block in the file
2245 * @bh - bh of the block (used to access block's state)
2246 *
2247 * the function is used to collect contig. blocks in same state
2248 */
2249static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002250 sector_t logical, size_t b_size,
2251 unsigned long b_state)
Alex Tomas64769242008-07-11 19:27:31 -04002252{
Alex Tomas64769242008-07-11 19:27:31 -04002253 sector_t next;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002254 int nrblocks = mpd->b_size >> mpd->inode->i_blkbits;
Alex Tomas64769242008-07-11 19:27:31 -04002255
Mingming Cao525f4ed2008-08-19 22:15:58 -04002256 /* check if thereserved journal credits might overflow */
2257 if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) {
2258 if (nrblocks >= EXT4_MAX_TRANS_DATA) {
2259 /*
2260 * With non-extent format we are limited by the journal
2261 * credit available. Total credit needed to insert
2262 * nrblocks contiguous blocks is dependent on the
2263 * nrblocks. So limit nrblocks.
2264 */
2265 goto flush_it;
2266 } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
2267 EXT4_MAX_TRANS_DATA) {
2268 /*
2269 * Adding the new buffer_head would make it cross the
2270 * allowed limit for which we have journal credit
2271 * reserved. So limit the new bh->b_size
2272 */
2273 b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
2274 mpd->inode->i_blkbits;
2275 /* we will do mpage_da_submit_io in the next loop */
2276 }
2277 }
Alex Tomas64769242008-07-11 19:27:31 -04002278 /*
2279 * First block in the extent
2280 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002281 if (mpd->b_size == 0) {
2282 mpd->b_blocknr = logical;
2283 mpd->b_size = b_size;
2284 mpd->b_state = b_state & BH_FLAGS;
Alex Tomas64769242008-07-11 19:27:31 -04002285 return;
2286 }
2287
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002288 next = mpd->b_blocknr + nrblocks;
Alex Tomas64769242008-07-11 19:27:31 -04002289 /*
2290 * Can we merge the block to our big extent?
2291 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002292 if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
2293 mpd->b_size += b_size;
Alex Tomas64769242008-07-11 19:27:31 -04002294 return;
2295 }
2296
Mingming Cao525f4ed2008-08-19 22:15:58 -04002297flush_it:
Alex Tomas64769242008-07-11 19:27:31 -04002298 /*
2299 * We couldn't merge the block to our extent, so we
2300 * need to flush current extent and start new one
2301 */
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002302 if (mpage_da_map_blocks(mpd) == 0)
2303 mpage_da_submit_io(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002304 mpd->io_done = 1;
2305 return;
Alex Tomas64769242008-07-11 19:27:31 -04002306}
2307
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002308static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002309{
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002310 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002311}
2312
Alex Tomas64769242008-07-11 19:27:31 -04002313/*
2314 * __mpage_da_writepage - finds extent of pages and blocks
2315 *
2316 * @page: page to consider
2317 * @wbc: not used, we just follow rules
2318 * @data: context
2319 *
2320 * The function finds extents of pages and scan them for all blocks.
2321 */
2322static int __mpage_da_writepage(struct page *page,
2323 struct writeback_control *wbc, void *data)
2324{
2325 struct mpage_da_data *mpd = data;
2326 struct inode *inode = mpd->inode;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002327 struct buffer_head *bh, *head;
Alex Tomas64769242008-07-11 19:27:31 -04002328 sector_t logical;
2329
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002330 if (mpd->io_done) {
2331 /*
2332 * Rest of the page in the page_vec
2333 * redirty then and skip then. We will
2334 * try to to write them again after
2335 * starting a new transaction
2336 */
2337 redirty_page_for_writepage(wbc, page);
2338 unlock_page(page);
2339 return MPAGE_DA_EXTENT_TAIL;
2340 }
Alex Tomas64769242008-07-11 19:27:31 -04002341 /*
2342 * Can we merge this page to current extent?
2343 */
2344 if (mpd->next_page != page->index) {
2345 /*
2346 * Nope, we can't. So, we map non-allocated blocks
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002347 * and start IO on them using writepage()
Alex Tomas64769242008-07-11 19:27:31 -04002348 */
2349 if (mpd->next_page != mpd->first_page) {
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002350 if (mpage_da_map_blocks(mpd) == 0)
2351 mpage_da_submit_io(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002352 /*
2353 * skip rest of the page in the page_vec
2354 */
2355 mpd->io_done = 1;
2356 redirty_page_for_writepage(wbc, page);
2357 unlock_page(page);
2358 return MPAGE_DA_EXTENT_TAIL;
Alex Tomas64769242008-07-11 19:27:31 -04002359 }
2360
2361 /*
2362 * Start next extent of pages ...
2363 */
2364 mpd->first_page = page->index;
2365
2366 /*
2367 * ... and blocks
2368 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002369 mpd->b_size = 0;
2370 mpd->b_state = 0;
2371 mpd->b_blocknr = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002372 }
2373
2374 mpd->next_page = page->index + 1;
2375 logical = (sector_t) page->index <<
2376 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2377
2378 if (!page_has_buffers(page)) {
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002379 mpage_add_bh_to_extent(mpd, logical, PAGE_CACHE_SIZE,
2380 (1 << BH_Dirty) | (1 << BH_Uptodate));
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002381 if (mpd->io_done)
2382 return MPAGE_DA_EXTENT_TAIL;
Alex Tomas64769242008-07-11 19:27:31 -04002383 } else {
2384 /*
2385 * Page with regular buffer heads, just add all dirty ones
2386 */
2387 head = page_buffers(page);
2388 bh = head;
2389 do {
2390 BUG_ON(buffer_locked(bh));
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002391 /*
2392 * We need to try to allocate
2393 * unmapped blocks in the same page.
2394 * Otherwise we won't make progress
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002395 * with the page in ext4_writepage
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002396 */
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002397 if (ext4_bh_delay_or_unwritten(NULL, bh)) {
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002398 mpage_add_bh_to_extent(mpd, logical,
2399 bh->b_size,
2400 bh->b_state);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002401 if (mpd->io_done)
2402 return MPAGE_DA_EXTENT_TAIL;
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002403 } else if (buffer_dirty(bh) && (buffer_mapped(bh))) {
2404 /*
2405 * mapped dirty buffer. We need to update
2406 * the b_state because we look at
2407 * b_state in mpage_da_map_blocks. We don't
2408 * update b_size because if we find an
2409 * unmapped buffer_head later we need to
2410 * use the b_state flag of that buffer_head.
2411 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002412 if (mpd->b_size == 0)
2413 mpd->b_state = bh->b_state & BH_FLAGS;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002414 }
Alex Tomas64769242008-07-11 19:27:31 -04002415 logical++;
2416 } while ((bh = bh->b_this_page) != head);
2417 }
2418
2419 return 0;
2420}
2421
2422/*
Theodore Ts'ob920c752009-05-14 00:54:29 -04002423 * This is a special get_blocks_t callback which is used by
2424 * ext4_da_write_begin(). It will either return mapped block or
2425 * reserve space for a single block.
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002426 *
2427 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
2428 * We also have b_blocknr = -1 and b_bdev initialized properly
2429 *
2430 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
2431 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
2432 * initialized properly.
Alex Tomas64769242008-07-11 19:27:31 -04002433 */
2434static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
2435 struct buffer_head *bh_result, int create)
2436{
2437 int ret = 0;
Aneesh Kumar K.V33b98172009-05-12 14:40:37 -04002438 sector_t invalid_block = ~((sector_t) 0xffff);
2439
2440 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
2441 invalid_block = ~0;
Alex Tomas64769242008-07-11 19:27:31 -04002442
2443 BUG_ON(create == 0);
2444 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
2445
2446 /*
2447 * first, we need to know whether the block is allocated already
2448 * preallocated blocks are unmapped but should treated
2449 * the same as allocated blocks.
2450 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04002451 ret = ext4_get_blocks(NULL, inode, iblock, 1, bh_result, 0);
Mingming Caod2a17632008-07-14 17:52:37 -04002452 if ((ret == 0) && !buffer_delay(bh_result)) {
2453 /* the block isn't (pre)allocated yet, let's reserve space */
Alex Tomas64769242008-07-11 19:27:31 -04002454 /*
2455 * XXX: __block_prepare_write() unmaps passed block,
2456 * is it OK?
2457 */
Mingming Caod2a17632008-07-14 17:52:37 -04002458 ret = ext4_da_reserve_space(inode, 1);
2459 if (ret)
2460 /* not enough space to reserve */
2461 return ret;
2462
Aneesh Kumar K.V33b98172009-05-12 14:40:37 -04002463 map_bh(bh_result, inode->i_sb, invalid_block);
Alex Tomas64769242008-07-11 19:27:31 -04002464 set_buffer_new(bh_result);
2465 set_buffer_delay(bh_result);
2466 } else if (ret > 0) {
2467 bh_result->b_size = (ret << inode->i_blkbits);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002468 if (buffer_unwritten(bh_result)) {
2469 /* A delayed write to unwritten bh should
2470 * be marked new and mapped. Mapped ensures
2471 * that we don't do get_block multiple times
2472 * when we write to the same offset and new
2473 * ensures that we do proper zero out for
2474 * partial write.
2475 */
Aneesh Kumar K.V9c1ee182009-05-13 18:36:58 -04002476 set_buffer_new(bh_result);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002477 set_buffer_mapped(bh_result);
2478 }
Alex Tomas64769242008-07-11 19:27:31 -04002479 ret = 0;
2480 }
2481
2482 return ret;
2483}
Mingming Cao61628a32008-07-11 19:27:31 -04002484
Theodore Ts'ob920c752009-05-14 00:54:29 -04002485/*
2486 * This function is used as a standard get_block_t calback function
2487 * when there is no desire to allocate any blocks. It is used as a
2488 * callback function for block_prepare_write(), nobh_writepage(), and
2489 * block_write_full_page(). These functions should only try to map a
2490 * single block at a time.
2491 *
2492 * Since this function doesn't do block allocations even if the caller
2493 * requests it by passing in create=1, it is critically important that
2494 * any caller checks to make sure that any buffer heads are returned
2495 * by this function are either all already mapped or marked for
2496 * delayed allocation before calling nobh_writepage() or
2497 * block_write_full_page(). Otherwise, b_blocknr could be left
2498 * unitialized, and the page write functions will be taken by
2499 * surprise.
2500 */
2501static int noalloc_get_block_write(struct inode *inode, sector_t iblock,
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002502 struct buffer_head *bh_result, int create)
2503{
2504 int ret = 0;
2505 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
2506
Theodore Ts'oa2dc52b2009-05-12 13:51:29 -04002507 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
2508
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002509 /*
2510 * we don't want to do block allocation in writepage
2511 * so call get_block_wrap with create = 0
2512 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04002513 ret = ext4_get_blocks(NULL, inode, iblock, max_blocks, bh_result, 0);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002514 if (ret > 0) {
2515 bh_result->b_size = (ret << inode->i_blkbits);
2516 ret = 0;
2517 }
2518 return ret;
Mingming Cao61628a32008-07-11 19:27:31 -04002519}
2520
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002521static int bget_one(handle_t *handle, struct buffer_head *bh)
2522{
2523 get_bh(bh);
2524 return 0;
2525}
2526
2527static int bput_one(handle_t *handle, struct buffer_head *bh)
2528{
2529 put_bh(bh);
2530 return 0;
2531}
2532
2533static int __ext4_journalled_writepage(struct page *page,
2534 struct writeback_control *wbc,
2535 unsigned int len)
2536{
2537 struct address_space *mapping = page->mapping;
2538 struct inode *inode = mapping->host;
2539 struct buffer_head *page_bufs;
2540 handle_t *handle = NULL;
2541 int ret = 0;
2542 int err;
2543
2544 page_bufs = page_buffers(page);
2545 BUG_ON(!page_bufs);
2546 walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one);
2547 /* As soon as we unlock the page, it can go away, but we have
2548 * references to buffers so we are safe */
2549 unlock_page(page);
2550
2551 handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
2552 if (IS_ERR(handle)) {
2553 ret = PTR_ERR(handle);
2554 goto out;
2555 }
2556
2557 ret = walk_page_buffers(handle, page_bufs, 0, len, NULL,
2558 do_journal_get_write_access);
2559
2560 err = walk_page_buffers(handle, page_bufs, 0, len, NULL,
2561 write_end_fn);
2562 if (ret == 0)
2563 ret = err;
2564 err = ext4_journal_stop(handle);
2565 if (!ret)
2566 ret = err;
2567
2568 walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one);
2569 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
2570out:
2571 return ret;
2572}
2573
Mingming Cao61628a32008-07-11 19:27:31 -04002574/*
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002575 * Note that we don't need to start a transaction unless we're journaling data
2576 * because we should have holes filled from ext4_page_mkwrite(). We even don't
2577 * need to file the inode to the transaction's list in ordered mode because if
2578 * we are writing back data added by write(), the inode is already there and if
2579 * we are writing back data modified via mmap(), noone guarantees in which
2580 * transaction the data will hit the disk. In case we are journaling data, we
2581 * cannot start transaction directly because transaction start ranks above page
2582 * lock so we have to do some magic.
2583 *
Theodore Ts'ob920c752009-05-14 00:54:29 -04002584 * This function can get called via...
2585 * - ext4_da_writepages after taking page lock (have journal handle)
2586 * - journal_submit_inode_data_buffers (no journal handle)
2587 * - shrink_page_list via pdflush (no journal handle)
2588 * - grab_page_cache when doing write_begin (have journal handle)
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002589 *
2590 * We don't do any block allocation in this function. If we have page with
2591 * multiple blocks we need to write those buffer_heads that are mapped. This
2592 * is important for mmaped based write. So if we do with blocksize 1K
2593 * truncate(f, 1024);
2594 * a = mmap(f, 0, 4096);
2595 * a[0] = 'a';
2596 * truncate(f, 4096);
2597 * we have in the page first buffer_head mapped via page_mkwrite call back
2598 * but other bufer_heads would be unmapped but dirty(dirty done via the
2599 * do_wp_page). So writepage should write the first block. If we modify
2600 * the mmap area beyond 1024 we will again get a page_fault and the
2601 * page_mkwrite callback will do the block allocation and mark the
2602 * buffer_heads mapped.
2603 *
2604 * We redirty the page if we have any buffer_heads that is either delay or
2605 * unwritten in the page.
2606 *
2607 * We can get recursively called as show below.
2608 *
2609 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
2610 * ext4_writepage()
2611 *
2612 * But since we don't do any block allocation we should not deadlock.
2613 * Page also have the dirty flag cleared so we don't get recurive page_lock.
Mingming Cao61628a32008-07-11 19:27:31 -04002614 */
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002615static int ext4_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002616 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002617{
Alex Tomas64769242008-07-11 19:27:31 -04002618 int ret = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002619 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002620 unsigned int len;
Mingming Cao61628a32008-07-11 19:27:31 -04002621 struct buffer_head *page_bufs;
2622 struct inode *inode = page->mapping->host;
Alex Tomas64769242008-07-11 19:27:31 -04002623
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002624 trace_ext4_writepage(inode, page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002625 size = i_size_read(inode);
2626 if (page->index == size >> PAGE_CACHE_SHIFT)
2627 len = size & ~PAGE_CACHE_MASK;
2628 else
2629 len = PAGE_CACHE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04002630
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002631 if (page_has_buffers(page)) {
Mingming Cao61628a32008-07-11 19:27:31 -04002632 page_bufs = page_buffers(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002633 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002634 ext4_bh_delay_or_unwritten)) {
Mingming Cao61628a32008-07-11 19:27:31 -04002635 /*
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002636 * We don't want to do block allocation
2637 * So redirty the page and return
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04002638 * We may reach here when we do a journal commit
2639 * via journal_submit_inode_data_buffers.
2640 * If we don't have mapping block we just ignore
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002641 * them. We can also reach here via shrink_page_list
2642 */
2643 redirty_page_for_writepage(wbc, page);
2644 unlock_page(page);
2645 return 0;
2646 }
2647 } else {
2648 /*
2649 * The test for page_has_buffers() is subtle:
2650 * We know the page is dirty but it lost buffers. That means
2651 * that at some moment in time after write_begin()/write_end()
2652 * has been called all buffers have been clean and thus they
2653 * must have been written at least once. So they are all
2654 * mapped and we can happily proceed with mapping them
2655 * and writing the page.
2656 *
2657 * Try to initialize the buffer_heads and check whether
2658 * all are mapped and non delay. We don't want to
2659 * do block allocation here.
2660 */
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04002661 ret = block_prepare_write(page, 0, len,
Theodore Ts'ob920c752009-05-14 00:54:29 -04002662 noalloc_get_block_write);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002663 if (!ret) {
2664 page_bufs = page_buffers(page);
2665 /* check whether all are mapped and non delay */
2666 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002667 ext4_bh_delay_or_unwritten)) {
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002668 redirty_page_for_writepage(wbc, page);
2669 unlock_page(page);
2670 return 0;
2671 }
2672 } else {
2673 /*
2674 * We can't do block allocation here
2675 * so just redity the page and unlock
2676 * and return
Mingming Cao61628a32008-07-11 19:27:31 -04002677 */
Mingming Cao61628a32008-07-11 19:27:31 -04002678 redirty_page_for_writepage(wbc, page);
2679 unlock_page(page);
2680 return 0;
2681 }
Aneesh Kumar K.Ved9b3e32008-11-07 09:06:45 -05002682 /* now mark the buffer_heads as dirty and uptodate */
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04002683 block_commit_write(page, 0, len);
Alex Tomas64769242008-07-11 19:27:31 -04002684 }
2685
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002686 if (PageChecked(page) && ext4_should_journal_data(inode)) {
2687 /*
2688 * It's mmapped pagecache. Add buffers and journal it. There
2689 * doesn't seem much point in redirtying the page here.
2690 */
2691 ClearPageChecked(page);
2692 return __ext4_journalled_writepage(page, wbc, len);
2693 }
2694
Alex Tomas64769242008-07-11 19:27:31 -04002695 if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode))
Theodore Ts'ob920c752009-05-14 00:54:29 -04002696 ret = nobh_writepage(page, noalloc_get_block_write, wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002697 else
Theodore Ts'ob920c752009-05-14 00:54:29 -04002698 ret = block_write_full_page(page, noalloc_get_block_write,
2699 wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002700
Alex Tomas64769242008-07-11 19:27:31 -04002701 return ret;
2702}
2703
Mingming Cao61628a32008-07-11 19:27:31 -04002704/*
Mingming Cao525f4ed2008-08-19 22:15:58 -04002705 * This is called via ext4_da_writepages() to
2706 * calulate the total number of credits to reserve to fit
2707 * a single extent allocation into a single transaction,
2708 * ext4_da_writpeages() will loop calling this before
2709 * the block allocation.
Mingming Cao61628a32008-07-11 19:27:31 -04002710 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002711
2712static int ext4_da_writepages_trans_blocks(struct inode *inode)
2713{
2714 int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
2715
2716 /*
2717 * With non-extent format the journal credit needed to
2718 * insert nrblocks contiguous block is dependent on
2719 * number of contiguous block. So we will limit
2720 * number of contiguous block to a sane value
2721 */
2722 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2723 (max_blocks > EXT4_MAX_TRANS_DATA))
2724 max_blocks = EXT4_MAX_TRANS_DATA;
2725
2726 return ext4_chunk_trans_blocks(inode, max_blocks);
2727}
Mingming Cao61628a32008-07-11 19:27:31 -04002728
Alex Tomas64769242008-07-11 19:27:31 -04002729static int ext4_da_writepages(struct address_space *mapping,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002730 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002731{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002732 pgoff_t index;
2733 int range_whole = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002734 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002735 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002736 struct inode *inode = mapping->host;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002737 int no_nrwrite_index_update;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002738 int pages_written = 0;
2739 long pages_skipped;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002740 int range_cyclic, cycled = 1, io_done = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002741 int needed_blocks, ret = 0, nr_to_writebump = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002742 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Mingming Cao61628a32008-07-11 19:27:31 -04002743
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002744 trace_ext4_da_writepages(inode, wbc);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002745
Mingming Cao61628a32008-07-11 19:27:31 -04002746 /*
2747 * No pages to write? This is mainly a kludge to avoid starting
2748 * a transaction for special inodes like journal inode on last iput()
2749 * because that could violate lock ordering on umount
2750 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002751 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Mingming Cao61628a32008-07-11 19:27:31 -04002752 return 0;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002753
2754 /*
2755 * If the filesystem has aborted, it is read-only, so return
2756 * right away instead of dumping stack traces later on that
2757 * will obscure the real source of the problem. We test
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002758 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002759 * the latter could be true if the filesystem is mounted
2760 * read-only, and in that case, ext4_da_writepages should
2761 * *never* be called, so if that ever happens, we would want
2762 * the stack trace.
2763 */
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002764 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002765 return -EROFS;
2766
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002767 /*
2768 * Make sure nr_to_write is >= sbi->s_mb_stream_request
2769 * This make sure small files blocks are allocated in
2770 * single attempt. This ensure that small files
2771 * get less fragmented.
2772 */
2773 if (wbc->nr_to_write < sbi->s_mb_stream_request) {
2774 nr_to_writebump = sbi->s_mb_stream_request - wbc->nr_to_write;
2775 wbc->nr_to_write = sbi->s_mb_stream_request;
2776 }
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002777 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2778 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002779
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002780 range_cyclic = wbc->range_cyclic;
2781 if (wbc->range_cyclic) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002782 index = mapping->writeback_index;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002783 if (index)
2784 cycled = 0;
2785 wbc->range_start = index << PAGE_CACHE_SHIFT;
2786 wbc->range_end = LLONG_MAX;
2787 wbc->range_cyclic = 0;
2788 } else
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002789 index = wbc->range_start >> PAGE_CACHE_SHIFT;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002790
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002791 mpd.wbc = wbc;
2792 mpd.inode = mapping->host;
2793
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002794 /*
2795 * we don't want write_cache_pages to update
2796 * nr_to_write and writeback_index
2797 */
2798 no_nrwrite_index_update = wbc->no_nrwrite_index_update;
2799 wbc->no_nrwrite_index_update = 1;
2800 pages_skipped = wbc->pages_skipped;
2801
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002802retry:
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002803 while (!ret && wbc->nr_to_write > 0) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002804
2805 /*
2806 * we insert one extent at a time. So we need
2807 * credit needed for single extent allocation.
2808 * journalled mode is currently not supported
2809 * by delalloc
2810 */
2811 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002812 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002813
Mingming Cao61628a32008-07-11 19:27:31 -04002814 /* start a new transaction*/
2815 handle = ext4_journal_start(inode, needed_blocks);
2816 if (IS_ERR(handle)) {
2817 ret = PTR_ERR(handle);
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002818 printk(KERN_CRIT "%s: jbd2_start: "
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002819 "%ld pages, ino %lu; err %d\n", __func__,
2820 wbc->nr_to_write, inode->i_ino, ret);
2821 dump_stack();
Mingming Cao61628a32008-07-11 19:27:31 -04002822 goto out_writepages;
2823 }
Theodore Ts'of63e6002009-02-23 16:42:39 -05002824
2825 /*
2826 * Now call __mpage_da_writepage to find the next
2827 * contiguous region of logical blocks that need
2828 * blocks to be allocated by ext4. We don't actually
2829 * submit the blocks for I/O here, even though
2830 * write_cache_pages thinks it will, and will set the
2831 * pages as clean for write before calling
2832 * __mpage_da_writepage().
2833 */
2834 mpd.b_size = 0;
2835 mpd.b_state = 0;
2836 mpd.b_blocknr = 0;
2837 mpd.first_page = 0;
2838 mpd.next_page = 0;
2839 mpd.io_done = 0;
2840 mpd.pages_written = 0;
2841 mpd.retval = 0;
2842 ret = write_cache_pages(mapping, wbc, __mpage_da_writepage,
2843 &mpd);
2844 /*
2845 * If we have a contigous extent of pages and we
2846 * haven't done the I/O yet, map the blocks and submit
2847 * them for I/O.
2848 */
2849 if (!mpd.io_done && mpd.next_page != mpd.first_page) {
2850 if (mpage_da_map_blocks(&mpd) == 0)
2851 mpage_da_submit_io(&mpd);
2852 mpd.io_done = 1;
2853 ret = MPAGE_DA_EXTENT_TAIL;
2854 }
2855 wbc->nr_to_write -= mpd.pages_written;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002856
Mingming Cao61628a32008-07-11 19:27:31 -04002857 ext4_journal_stop(handle);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002858
Eric Sandeen8f64b322009-02-26 00:57:35 -05002859 if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002860 /* commit the transaction which would
2861 * free blocks released in the transaction
2862 * and try again
2863 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002864 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002865 wbc->pages_skipped = pages_skipped;
2866 ret = 0;
2867 } else if (ret == MPAGE_DA_EXTENT_TAIL) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002868 /*
2869 * got one extent now try with
2870 * rest of the pages
2871 */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002872 pages_written += mpd.pages_written;
2873 wbc->pages_skipped = pages_skipped;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002874 ret = 0;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002875 io_done = 1;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002876 } else if (wbc->nr_to_write)
Mingming Cao61628a32008-07-11 19:27:31 -04002877 /*
2878 * There is no more writeout needed
2879 * or we requested for a noblocking writeout
2880 * and we found the device congested
2881 */
Mingming Cao61628a32008-07-11 19:27:31 -04002882 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002883 }
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002884 if (!io_done && !cycled) {
2885 cycled = 1;
2886 index = 0;
2887 wbc->range_start = index << PAGE_CACHE_SHIFT;
2888 wbc->range_end = mapping->writeback_index - 1;
2889 goto retry;
2890 }
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002891 if (pages_skipped != wbc->pages_skipped)
2892 printk(KERN_EMERG "This should not happen leaving %s "
2893 "with nr_to_write = %ld ret = %d\n",
2894 __func__, wbc->nr_to_write, ret);
Mingming Cao61628a32008-07-11 19:27:31 -04002895
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002896 /* Update index */
2897 index += pages_written;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002898 wbc->range_cyclic = range_cyclic;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002899 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2900 /*
2901 * set the writeback_index so that range_cyclic
2902 * mode will write it back later
2903 */
2904 mapping->writeback_index = index;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002905
Mingming Cao61628a32008-07-11 19:27:31 -04002906out_writepages:
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002907 if (!no_nrwrite_index_update)
2908 wbc->no_nrwrite_index_update = 0;
2909 wbc->nr_to_write -= nr_to_writebump;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002910 trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
Mingming Cao61628a32008-07-11 19:27:31 -04002911 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002912}
2913
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002914#define FALL_BACK_TO_NONDELALLOC 1
2915static int ext4_nonda_switch(struct super_block *sb)
2916{
2917 s64 free_blocks, dirty_blocks;
2918 struct ext4_sb_info *sbi = EXT4_SB(sb);
2919
2920 /*
2921 * switch to non delalloc mode if we are running low
2922 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08002923 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002924 * accumulated on each CPU without updating global counters
2925 * Delalloc need an accurate free block accounting. So switch
2926 * to non delalloc when we are near to error range.
2927 */
2928 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
2929 dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter);
2930 if (2 * free_blocks < 3 * dirty_blocks ||
2931 free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) {
2932 /*
2933 * free block count is less that 150% of dirty blocks
2934 * or free blocks is less that watermark
2935 */
2936 return 1;
2937 }
2938 return 0;
2939}
2940
Alex Tomas64769242008-07-11 19:27:31 -04002941static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002942 loff_t pos, unsigned len, unsigned flags,
2943 struct page **pagep, void **fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002944{
Mingming Caod2a17632008-07-14 17:52:37 -04002945 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002946 struct page *page;
2947 pgoff_t index;
2948 unsigned from, to;
2949 struct inode *inode = mapping->host;
2950 handle_t *handle;
2951
2952 index = pos >> PAGE_CACHE_SHIFT;
2953 from = pos & (PAGE_CACHE_SIZE - 1);
2954 to = from + len;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002955
2956 if (ext4_nonda_switch(inode->i_sb)) {
2957 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2958 return ext4_write_begin(file, mapping, pos,
2959 len, flags, pagep, fsdata);
2960 }
2961 *fsdata = (void *)0;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002962 trace_ext4_da_write_begin(inode, pos, len, flags);
Mingming Caod2a17632008-07-14 17:52:37 -04002963retry:
Alex Tomas64769242008-07-11 19:27:31 -04002964 /*
2965 * With delayed allocation, we don't log the i_disksize update
2966 * if there is delayed block allocation. But we still need
2967 * to journalling the i_disksize update if writes to the end
2968 * of file which has an already mapped buffer.
2969 */
2970 handle = ext4_journal_start(inode, 1);
2971 if (IS_ERR(handle)) {
2972 ret = PTR_ERR(handle);
2973 goto out;
2974 }
Jan Karaebd36102009-02-22 21:09:59 -05002975 /* We cannot recurse into the filesystem as the transaction is already
2976 * started */
2977 flags |= AOP_FLAG_NOFS;
Alex Tomas64769242008-07-11 19:27:31 -04002978
Nick Piggin54566b22009-01-04 12:00:53 -08002979 page = grab_cache_page_write_begin(mapping, index, flags);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002980 if (!page) {
2981 ext4_journal_stop(handle);
2982 ret = -ENOMEM;
2983 goto out;
2984 }
Alex Tomas64769242008-07-11 19:27:31 -04002985 *pagep = page;
2986
2987 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
Theodore Ts'ob920c752009-05-14 00:54:29 -04002988 ext4_da_get_block_prep);
Alex Tomas64769242008-07-11 19:27:31 -04002989 if (ret < 0) {
2990 unlock_page(page);
2991 ext4_journal_stop(handle);
2992 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04002993 /*
2994 * block_write_begin may have instantiated a few blocks
2995 * outside i_size. Trim these off again. Don't need
2996 * i_size_read because we hold i_mutex.
2997 */
2998 if (pos + len > inode->i_size)
Jan Karaffacfa72009-07-13 16:22:22 -04002999 ext4_truncate(inode);
Alex Tomas64769242008-07-11 19:27:31 -04003000 }
3001
Mingming Caod2a17632008-07-14 17:52:37 -04003002 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3003 goto retry;
Alex Tomas64769242008-07-11 19:27:31 -04003004out:
3005 return ret;
3006}
3007
Mingming Cao632eaea2008-07-11 19:27:31 -04003008/*
3009 * Check if we should update i_disksize
3010 * when write to the end of file but not require block allocation
3011 */
3012static int ext4_da_should_update_i_disksize(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003013 unsigned long offset)
Mingming Cao632eaea2008-07-11 19:27:31 -04003014{
3015 struct buffer_head *bh;
3016 struct inode *inode = page->mapping->host;
3017 unsigned int idx;
3018 int i;
3019
3020 bh = page_buffers(page);
3021 idx = offset >> inode->i_blkbits;
3022
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003023 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04003024 bh = bh->b_this_page;
3025
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04003026 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
Mingming Cao632eaea2008-07-11 19:27:31 -04003027 return 0;
3028 return 1;
3029}
3030
Alex Tomas64769242008-07-11 19:27:31 -04003031static int ext4_da_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003032 struct address_space *mapping,
3033 loff_t pos, unsigned len, unsigned copied,
3034 struct page *page, void *fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04003035{
3036 struct inode *inode = mapping->host;
3037 int ret = 0, ret2;
3038 handle_t *handle = ext4_journal_current_handle();
3039 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04003040 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003041 int write_mode = (int)(unsigned long)fsdata;
3042
3043 if (write_mode == FALL_BACK_TO_NONDELALLOC) {
3044 if (ext4_should_order_data(inode)) {
3045 return ext4_ordered_write_end(file, mapping, pos,
3046 len, copied, page, fsdata);
3047 } else if (ext4_should_writeback_data(inode)) {
3048 return ext4_writeback_write_end(file, mapping, pos,
3049 len, copied, page, fsdata);
3050 } else {
3051 BUG();
3052 }
3053 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003054
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003055 trace_ext4_da_write_end(inode, pos, len, copied);
Mingming Cao632eaea2008-07-11 19:27:31 -04003056 start = pos & (PAGE_CACHE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003057 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04003058
3059 /*
3060 * generic_write_end() will run mark_inode_dirty() if i_size
3061 * changes. So let's piggyback the i_disksize mark_inode_dirty
3062 * into that.
3063 */
3064
3065 new_i_size = pos + copied;
Mingming Cao632eaea2008-07-11 19:27:31 -04003066 if (new_i_size > EXT4_I(inode)->i_disksize) {
3067 if (ext4_da_should_update_i_disksize(page, end)) {
3068 down_write(&EXT4_I(inode)->i_data_sem);
3069 if (new_i_size > EXT4_I(inode)->i_disksize) {
3070 /*
3071 * Updating i_disksize when extending file
3072 * without needing block allocation
3073 */
3074 if (ext4_should_order_data(inode))
3075 ret = ext4_jbd2_file_inode(handle,
3076 inode);
Alex Tomas64769242008-07-11 19:27:31 -04003077
Mingming Cao632eaea2008-07-11 19:27:31 -04003078 EXT4_I(inode)->i_disksize = new_i_size;
3079 }
3080 up_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003081 /* We need to mark inode dirty even if
3082 * new_i_size is less that inode->i_size
3083 * bu greater than i_disksize.(hint delalloc)
3084 */
3085 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04003086 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003087 }
Alex Tomas64769242008-07-11 19:27:31 -04003088 ret2 = generic_write_end(file, mapping, pos, len, copied,
3089 page, fsdata);
3090 copied = ret2;
3091 if (ret2 < 0)
3092 ret = ret2;
3093 ret2 = ext4_journal_stop(handle);
3094 if (!ret)
3095 ret = ret2;
3096
3097 return ret ? ret : copied;
3098}
3099
3100static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
3101{
Alex Tomas64769242008-07-11 19:27:31 -04003102 /*
3103 * Drop reserved blocks
3104 */
3105 BUG_ON(!PageLocked(page));
3106 if (!page_has_buffers(page))
3107 goto out;
3108
Mingming Caod2a17632008-07-14 17:52:37 -04003109 ext4_da_page_release_reservation(page, offset);
Alex Tomas64769242008-07-11 19:27:31 -04003110
3111out:
3112 ext4_invalidatepage(page, offset);
3113
3114 return;
3115}
3116
Theodore Ts'occd25062009-02-26 01:04:07 -05003117/*
3118 * Force all delayed allocation blocks to be allocated for a given inode.
3119 */
3120int ext4_alloc_da_blocks(struct inode *inode)
3121{
3122 if (!EXT4_I(inode)->i_reserved_data_blocks &&
3123 !EXT4_I(inode)->i_reserved_meta_blocks)
3124 return 0;
3125
3126 /*
3127 * We do something simple for now. The filemap_flush() will
3128 * also start triggering a write of the data blocks, which is
3129 * not strictly speaking necessary (and for users of
3130 * laptop_mode, not even desirable). However, to do otherwise
3131 * would require replicating code paths in:
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003132 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003133 * ext4_da_writepages() ->
3134 * write_cache_pages() ---> (via passed in callback function)
3135 * __mpage_da_writepage() -->
3136 * mpage_add_bh_to_extent()
3137 * mpage_da_map_blocks()
3138 *
3139 * The problem is that write_cache_pages(), located in
3140 * mm/page-writeback.c, marks pages clean in preparation for
3141 * doing I/O, which is not desirable if we're not planning on
3142 * doing I/O at all.
3143 *
3144 * We could call write_cache_pages(), and then redirty all of
3145 * the pages by calling redirty_page_for_writeback() but that
3146 * would be ugly in the extreme. So instead we would need to
3147 * replicate parts of the code in the above functions,
3148 * simplifying them becuase we wouldn't actually intend to
3149 * write out the pages, but rather only collect contiguous
3150 * logical block extents, call the multi-block allocator, and
3151 * then update the buffer heads with the block allocations.
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003152 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003153 * For now, though, we'll cheat by calling filemap_flush(),
3154 * which will map the blocks, and start the I/O, but not
3155 * actually wait for the I/O to complete.
3156 */
3157 return filemap_flush(inode->i_mapping);
3158}
Alex Tomas64769242008-07-11 19:27:31 -04003159
3160/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003161 * bmap() is special. It gets used by applications such as lilo and by
3162 * the swapper to find the on-disk block of a specific piece of data.
3163 *
3164 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07003165 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003166 * filesystem and enables swap, then they may get a nasty shock when the
3167 * data getting swapped to that swapfile suddenly gets overwritten by
3168 * the original zero's written out previously to the journal and
3169 * awaiting writeback in the kernel's buffer cache.
3170 *
3171 * So, if we see any bmap calls here on a modified, data-journaled file,
3172 * take extra steps to flush any blocks which might be in the cache.
3173 */
Mingming Cao617ba132006-10-11 01:20:53 -07003174static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003175{
3176 struct inode *inode = mapping->host;
3177 journal_t *journal;
3178 int err;
3179
Alex Tomas64769242008-07-11 19:27:31 -04003180 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
3181 test_opt(inode->i_sb, DELALLOC)) {
3182 /*
3183 * With delalloc we want to sync the file
3184 * so that we can make sure we allocate
3185 * blocks for file
3186 */
3187 filemap_write_and_wait(mapping);
3188 }
3189
Frank Mayhar03901312009-01-07 00:06:22 -05003190 if (EXT4_JOURNAL(inode) && EXT4_I(inode)->i_state & EXT4_STATE_JDATA) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003191 /*
3192 * This is a REALLY heavyweight approach, but the use of
3193 * bmap on dirty files is expected to be extremely rare:
3194 * only if we run lilo or swapon on a freshly made file
3195 * do we expect this to happen.
3196 *
3197 * (bmap requires CAP_SYS_RAWIO so this does not
3198 * represent an unprivileged user DOS attack --- we'd be
3199 * in trouble if mortal users could trigger this path at
3200 * will.)
3201 *
Mingming Cao617ba132006-10-11 01:20:53 -07003202 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003203 * regular files. If somebody wants to bmap a directory
3204 * or symlink and gets confused because the buffer
3205 * hasn't yet been flushed to disk, they deserve
3206 * everything they get.
3207 */
3208
Mingming Cao617ba132006-10-11 01:20:53 -07003209 EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA;
3210 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07003211 jbd2_journal_lock_updates(journal);
3212 err = jbd2_journal_flush(journal);
3213 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003214
3215 if (err)
3216 return 0;
3217 }
3218
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003219 return generic_block_bmap(mapping, block, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003220}
3221
Mingming Cao617ba132006-10-11 01:20:53 -07003222static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003223{
Mingming Cao617ba132006-10-11 01:20:53 -07003224 return mpage_readpage(page, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003225}
3226
3227static int
Mingming Cao617ba132006-10-11 01:20:53 -07003228ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003229 struct list_head *pages, unsigned nr_pages)
3230{
Mingming Cao617ba132006-10-11 01:20:53 -07003231 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003232}
3233
Mingming Cao617ba132006-10-11 01:20:53 -07003234static void ext4_invalidatepage(struct page *page, unsigned long offset)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003235{
Mingming Cao617ba132006-10-11 01:20:53 -07003236 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003237
3238 /*
3239 * If it's a full truncate we just forget about the pending dirtying
3240 */
3241 if (offset == 0)
3242 ClearPageChecked(page);
3243
Frank Mayhar03901312009-01-07 00:06:22 -05003244 if (journal)
3245 jbd2_journal_invalidatepage(journal, page, offset);
3246 else
3247 block_invalidatepage(page, offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003248}
3249
Mingming Cao617ba132006-10-11 01:20:53 -07003250static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003251{
Mingming Cao617ba132006-10-11 01:20:53 -07003252 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003253
3254 WARN_ON(PageChecked(page));
3255 if (!page_has_buffers(page))
3256 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003257 if (journal)
3258 return jbd2_journal_try_to_free_buffers(journal, page, wait);
3259 else
3260 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003261}
3262
3263/*
3264 * If the O_DIRECT write will extend the file then add this inode to the
3265 * orphan list. So recovery will truncate it back to the original size
3266 * if the machine crashes during the write.
3267 *
3268 * If the O_DIRECT write is intantiating holes inside i_size and the machine
Jan Kara7fb54092008-02-10 01:08:38 -05003269 * crashes then stale disk data _may_ be exposed inside the file. But current
3270 * VFS code falls back into buffered path in that case so we are safe.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003271 */
Mingming Cao617ba132006-10-11 01:20:53 -07003272static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003273 const struct iovec *iov, loff_t offset,
3274 unsigned long nr_segs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003275{
3276 struct file *file = iocb->ki_filp;
3277 struct inode *inode = file->f_mapping->host;
Mingming Cao617ba132006-10-11 01:20:53 -07003278 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003279 handle_t *handle;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003280 ssize_t ret;
3281 int orphan = 0;
3282 size_t count = iov_length(iov, nr_segs);
3283
3284 if (rw == WRITE) {
3285 loff_t final_size = offset + count;
3286
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003287 if (final_size > inode->i_size) {
Jan Kara7fb54092008-02-10 01:08:38 -05003288 /* Credits for sb + inode write */
3289 handle = ext4_journal_start(inode, 2);
3290 if (IS_ERR(handle)) {
3291 ret = PTR_ERR(handle);
3292 goto out;
3293 }
Mingming Cao617ba132006-10-11 01:20:53 -07003294 ret = ext4_orphan_add(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003295 if (ret) {
3296 ext4_journal_stop(handle);
3297 goto out;
3298 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003299 orphan = 1;
3300 ei->i_disksize = inode->i_size;
Jan Kara7fb54092008-02-10 01:08:38 -05003301 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003302 }
3303 }
3304
3305 ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
3306 offset, nr_segs,
Mingming Cao617ba132006-10-11 01:20:53 -07003307 ext4_get_block, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003308
Jan Kara7fb54092008-02-10 01:08:38 -05003309 if (orphan) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003310 int err;
3311
Jan Kara7fb54092008-02-10 01:08:38 -05003312 /* Credits for sb + inode write */
3313 handle = ext4_journal_start(inode, 2);
3314 if (IS_ERR(handle)) {
3315 /* This is really bad luck. We've written the data
3316 * but cannot extend i_size. Bail out and pretend
3317 * the write failed... */
3318 ret = PTR_ERR(handle);
3319 goto out;
3320 }
3321 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003322 ext4_orphan_del(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003323 if (ret > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003324 loff_t end = offset + ret;
3325 if (end > inode->i_size) {
3326 ei->i_disksize = end;
3327 i_size_write(inode, end);
3328 /*
3329 * We're going to return a positive `ret'
3330 * here due to non-zero-length I/O, so there's
3331 * no way of reporting error returns from
Mingming Cao617ba132006-10-11 01:20:53 -07003332 * ext4_mark_inode_dirty() to userspace. So
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003333 * ignore it.
3334 */
Mingming Cao617ba132006-10-11 01:20:53 -07003335 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003336 }
3337 }
Mingming Cao617ba132006-10-11 01:20:53 -07003338 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003339 if (ret == 0)
3340 ret = err;
3341 }
3342out:
3343 return ret;
3344}
3345
3346/*
Mingming Cao617ba132006-10-11 01:20:53 -07003347 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003348 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3349 * much here because ->set_page_dirty is called under VFS locks. The page is
3350 * not necessarily locked.
3351 *
3352 * We cannot just dirty the page and leave attached buffers clean, because the
3353 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3354 * or jbddirty because all the journalling code will explode.
3355 *
3356 * So what we do is to mark the page "pending dirty" and next time writepage
3357 * is called, propagate that into the buffers appropriately.
3358 */
Mingming Cao617ba132006-10-11 01:20:53 -07003359static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003360{
3361 SetPageChecked(page);
3362 return __set_page_dirty_nobuffers(page);
3363}
3364
Mingming Cao617ba132006-10-11 01:20:53 -07003365static const struct address_space_operations ext4_ordered_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003366 .readpage = ext4_readpage,
3367 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003368 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003369 .sync_page = block_sync_page,
3370 .write_begin = ext4_write_begin,
3371 .write_end = ext4_ordered_write_end,
3372 .bmap = ext4_bmap,
3373 .invalidatepage = ext4_invalidatepage,
3374 .releasepage = ext4_releasepage,
3375 .direct_IO = ext4_direct_IO,
3376 .migratepage = buffer_migrate_page,
3377 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003378};
3379
Mingming Cao617ba132006-10-11 01:20:53 -07003380static const struct address_space_operations ext4_writeback_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003381 .readpage = ext4_readpage,
3382 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003383 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003384 .sync_page = block_sync_page,
3385 .write_begin = ext4_write_begin,
3386 .write_end = ext4_writeback_write_end,
3387 .bmap = ext4_bmap,
3388 .invalidatepage = ext4_invalidatepage,
3389 .releasepage = ext4_releasepage,
3390 .direct_IO = ext4_direct_IO,
3391 .migratepage = buffer_migrate_page,
3392 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003393};
3394
Mingming Cao617ba132006-10-11 01:20:53 -07003395static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003396 .readpage = ext4_readpage,
3397 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003398 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003399 .sync_page = block_sync_page,
3400 .write_begin = ext4_write_begin,
3401 .write_end = ext4_journalled_write_end,
3402 .set_page_dirty = ext4_journalled_set_page_dirty,
3403 .bmap = ext4_bmap,
3404 .invalidatepage = ext4_invalidatepage,
3405 .releasepage = ext4_releasepage,
3406 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003407};
3408
Alex Tomas64769242008-07-11 19:27:31 -04003409static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003410 .readpage = ext4_readpage,
3411 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003412 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003413 .writepages = ext4_da_writepages,
3414 .sync_page = block_sync_page,
3415 .write_begin = ext4_da_write_begin,
3416 .write_end = ext4_da_write_end,
3417 .bmap = ext4_bmap,
3418 .invalidatepage = ext4_da_invalidatepage,
3419 .releasepage = ext4_releasepage,
3420 .direct_IO = ext4_direct_IO,
3421 .migratepage = buffer_migrate_page,
3422 .is_partially_uptodate = block_is_partially_uptodate,
Alex Tomas64769242008-07-11 19:27:31 -04003423};
3424
Mingming Cao617ba132006-10-11 01:20:53 -07003425void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003426{
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04003427 if (ext4_should_order_data(inode) &&
3428 test_opt(inode->i_sb, DELALLOC))
3429 inode->i_mapping->a_ops = &ext4_da_aops;
3430 else if (ext4_should_order_data(inode))
Mingming Cao617ba132006-10-11 01:20:53 -07003431 inode->i_mapping->a_ops = &ext4_ordered_aops;
Alex Tomas64769242008-07-11 19:27:31 -04003432 else if (ext4_should_writeback_data(inode) &&
3433 test_opt(inode->i_sb, DELALLOC))
3434 inode->i_mapping->a_ops = &ext4_da_aops;
Mingming Cao617ba132006-10-11 01:20:53 -07003435 else if (ext4_should_writeback_data(inode))
3436 inode->i_mapping->a_ops = &ext4_writeback_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003437 else
Mingming Cao617ba132006-10-11 01:20:53 -07003438 inode->i_mapping->a_ops = &ext4_journalled_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003439}
3440
3441/*
Mingming Cao617ba132006-10-11 01:20:53 -07003442 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003443 * up to the end of the block which corresponds to `from'.
3444 * This required during truncate. We need to physically zero the tail end
3445 * of that block so it doesn't yield old data if the file is later grown.
3446 */
Jan Karacf108bc2008-07-11 19:27:31 -04003447int ext4_block_truncate_page(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003448 struct address_space *mapping, loff_t from)
3449{
Mingming Cao617ba132006-10-11 01:20:53 -07003450 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003451 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003452 unsigned blocksize, length, pos;
3453 ext4_lblk_t iblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003454 struct inode *inode = mapping->host;
3455 struct buffer_head *bh;
Jan Karacf108bc2008-07-11 19:27:31 -04003456 struct page *page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003457 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003458
Theodore Ts'of4a01012009-07-05 22:08:16 -04003459 page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
3460 mapping_gfp_mask(mapping) & ~__GFP_FS);
Jan Karacf108bc2008-07-11 19:27:31 -04003461 if (!page)
3462 return -EINVAL;
3463
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003464 blocksize = inode->i_sb->s_blocksize;
3465 length = blocksize - (offset & (blocksize - 1));
3466 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
3467
3468 /*
3469 * For "nobh" option, we can only work if we don't need to
3470 * read-in the page - otherwise we create buffers to do the IO.
3471 */
3472 if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) &&
Mingming Cao617ba132006-10-11 01:20:53 -07003473 ext4_should_writeback_data(inode) && PageUptodate(page)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003474 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003475 set_page_dirty(page);
3476 goto unlock;
3477 }
3478
3479 if (!page_has_buffers(page))
3480 create_empty_buffers(page, blocksize, 0);
3481
3482 /* Find the buffer that contains "offset" */
3483 bh = page_buffers(page);
3484 pos = blocksize;
3485 while (offset >= pos) {
3486 bh = bh->b_this_page;
3487 iblock++;
3488 pos += blocksize;
3489 }
3490
3491 err = 0;
3492 if (buffer_freed(bh)) {
3493 BUFFER_TRACE(bh, "freed: skip");
3494 goto unlock;
3495 }
3496
3497 if (!buffer_mapped(bh)) {
3498 BUFFER_TRACE(bh, "unmapped");
Mingming Cao617ba132006-10-11 01:20:53 -07003499 ext4_get_block(inode, iblock, bh, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003500 /* unmapped? It's a hole - nothing to do */
3501 if (!buffer_mapped(bh)) {
3502 BUFFER_TRACE(bh, "still unmapped");
3503 goto unlock;
3504 }
3505 }
3506
3507 /* Ok, it's mapped. Make sure it's up-to-date */
3508 if (PageUptodate(page))
3509 set_buffer_uptodate(bh);
3510
3511 if (!buffer_uptodate(bh)) {
3512 err = -EIO;
3513 ll_rw_block(READ, 1, &bh);
3514 wait_on_buffer(bh);
3515 /* Uhhuh. Read error. Complain and punt. */
3516 if (!buffer_uptodate(bh))
3517 goto unlock;
3518 }
3519
Mingming Cao617ba132006-10-11 01:20:53 -07003520 if (ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003521 BUFFER_TRACE(bh, "get write access");
Mingming Cao617ba132006-10-11 01:20:53 -07003522 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003523 if (err)
3524 goto unlock;
3525 }
3526
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003527 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003528
3529 BUFFER_TRACE(bh, "zeroed end of block");
3530
3531 err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003532 if (ext4_should_journal_data(inode)) {
Frank Mayhar03901312009-01-07 00:06:22 -05003533 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003534 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003535 if (ext4_should_order_data(inode))
Jan Kara678aaf42008-07-11 19:27:31 -04003536 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003537 mark_buffer_dirty(bh);
3538 }
3539
3540unlock:
3541 unlock_page(page);
3542 page_cache_release(page);
3543 return err;
3544}
3545
3546/*
3547 * Probably it should be a library function... search for first non-zero word
3548 * or memcmp with zero_page, whatever is better for particular architecture.
3549 * Linus?
3550 */
3551static inline int all_zeroes(__le32 *p, __le32 *q)
3552{
3553 while (p < q)
3554 if (*p++)
3555 return 0;
3556 return 1;
3557}
3558
3559/**
Mingming Cao617ba132006-10-11 01:20:53 -07003560 * ext4_find_shared - find the indirect blocks for partial truncation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003561 * @inode: inode in question
3562 * @depth: depth of the affected branch
Mingming Cao617ba132006-10-11 01:20:53 -07003563 * @offsets: offsets of pointers in that branch (see ext4_block_to_path)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003564 * @chain: place to store the pointers to partial indirect blocks
3565 * @top: place to the (detached) top of branch
3566 *
Mingming Cao617ba132006-10-11 01:20:53 -07003567 * This is a helper function used by ext4_truncate().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003568 *
3569 * When we do truncate() we may have to clean the ends of several
3570 * indirect blocks but leave the blocks themselves alive. Block is
3571 * partially truncated if some data below the new i_size is refered
3572 * from it (and it is on the path to the first completely truncated
3573 * data block, indeed). We have to free the top of that path along
3574 * with everything to the right of the path. Since no allocation
Mingming Cao617ba132006-10-11 01:20:53 -07003575 * past the truncation point is possible until ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003576 * finishes, we may safely do the latter, but top of branch may
3577 * require special attention - pageout below the truncation point
3578 * might try to populate it.
3579 *
3580 * We atomically detach the top of branch from the tree, store the
3581 * block number of its root in *@top, pointers to buffer_heads of
3582 * partially truncated blocks - in @chain[].bh and pointers to
3583 * their last elements that should not be removed - in
3584 * @chain[].p. Return value is the pointer to last filled element
3585 * of @chain.
3586 *
3587 * The work left to caller to do the actual freeing of subtrees:
3588 * a) free the subtree starting from *@top
3589 * b) free the subtrees whose roots are stored in
3590 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
3591 * c) free the subtrees growing from the inode past the @chain[0].
3592 * (no partially truncated stuff there). */
3593
Mingming Cao617ba132006-10-11 01:20:53 -07003594static Indirect *ext4_find_shared(struct inode *inode, int depth,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003595 ext4_lblk_t offsets[4], Indirect chain[4],
3596 __le32 *top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003597{
3598 Indirect *partial, *p;
3599 int k, err;
3600
3601 *top = 0;
3602 /* Make k index the deepest non-null offest + 1 */
3603 for (k = depth; k > 1 && !offsets[k-1]; k--)
3604 ;
Mingming Cao617ba132006-10-11 01:20:53 -07003605 partial = ext4_get_branch(inode, k, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003606 /* Writer: pointers */
3607 if (!partial)
3608 partial = chain + k-1;
3609 /*
3610 * If the branch acquired continuation since we've looked at it -
3611 * fine, it should all survive and (new) top doesn't belong to us.
3612 */
3613 if (!partial->key && *partial->p)
3614 /* Writer: end */
3615 goto no_top;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003616 for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003617 ;
3618 /*
3619 * OK, we've found the last block that must survive. The rest of our
3620 * branch should be detached before unlocking. However, if that rest
3621 * of branch is all ours and does not grow immediately from the inode
3622 * it's easier to cheat and just decrement partial->p.
3623 */
3624 if (p == chain + k - 1 && p > chain) {
3625 p->p--;
3626 } else {
3627 *top = *p->p;
Mingming Cao617ba132006-10-11 01:20:53 -07003628 /* Nope, don't do this in ext4. Must leave the tree intact */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003629#if 0
3630 *p->p = 0;
3631#endif
3632 }
3633 /* Writer: end */
3634
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003635 while (partial > p) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003636 brelse(partial->bh);
3637 partial--;
3638 }
3639no_top:
3640 return partial;
3641}
3642
3643/*
3644 * Zero a number of block pointers in either an inode or an indirect block.
3645 * If we restart the transaction we must again get write access to the
3646 * indirect block for further modification.
3647 *
3648 * We release `count' blocks on disk, but (last - first) may be greater
3649 * than `count' because there can be holes in there.
3650 */
Mingming Cao617ba132006-10-11 01:20:53 -07003651static void ext4_clear_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003652 struct buffer_head *bh,
3653 ext4_fsblk_t block_to_free,
3654 unsigned long count, __le32 *first,
3655 __le32 *last)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003656{
3657 __le32 *p;
3658 if (try_to_extend_transaction(handle, inode)) {
3659 if (bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05003660 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
3661 ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003662 }
Mingming Cao617ba132006-10-11 01:20:53 -07003663 ext4_mark_inode_dirty(handle, inode);
3664 ext4_journal_test_restart(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003665 if (bh) {
3666 BUFFER_TRACE(bh, "retaking write access");
Mingming Cao617ba132006-10-11 01:20:53 -07003667 ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003668 }
3669 }
3670
3671 /*
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003672 * Any buffers which are on the journal will be in memory. We
3673 * find them on the hash table so jbd2_journal_revoke() will
3674 * run jbd2_journal_forget() on them. We've already detached
3675 * each block from the file, so bforget() in
3676 * jbd2_journal_forget() should be safe.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003677 *
Mingming Caodab291a2006-10-11 01:21:01 -07003678 * AKPM: turn on bforget in jbd2_journal_forget()!!!
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003679 */
3680 for (p = first; p < last; p++) {
3681 u32 nr = le32_to_cpu(*p);
3682 if (nr) {
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003683 struct buffer_head *tbh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003684
3685 *p = 0;
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003686 tbh = sb_find_get_block(inode->i_sb, nr);
3687 ext4_forget(handle, 0, inode, tbh, nr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003688 }
3689 }
3690
Alex Tomasc9de5602008-01-29 00:19:52 -05003691 ext4_free_blocks(handle, inode, block_to_free, count, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003692}
3693
3694/**
Mingming Cao617ba132006-10-11 01:20:53 -07003695 * ext4_free_data - free a list of data blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003696 * @handle: handle for this transaction
3697 * @inode: inode we are dealing with
3698 * @this_bh: indirect buffer_head which contains *@first and *@last
3699 * @first: array of block numbers
3700 * @last: points immediately past the end of array
3701 *
3702 * We are freeing all blocks refered from that array (numbers are stored as
3703 * little-endian 32-bit) and updating @inode->i_blocks appropriately.
3704 *
3705 * We accumulate contiguous runs of blocks to free. Conveniently, if these
3706 * blocks are contiguous then releasing them at one time will only affect one
3707 * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
3708 * actually use a lot of journal space.
3709 *
3710 * @this_bh will be %NULL if @first and @last point into the inode's direct
3711 * block pointers.
3712 */
Mingming Cao617ba132006-10-11 01:20:53 -07003713static void ext4_free_data(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003714 struct buffer_head *this_bh,
3715 __le32 *first, __le32 *last)
3716{
Mingming Cao617ba132006-10-11 01:20:53 -07003717 ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003718 unsigned long count = 0; /* Number of blocks in the run */
3719 __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
3720 corresponding to
3721 block_to_free */
Mingming Cao617ba132006-10-11 01:20:53 -07003722 ext4_fsblk_t nr; /* Current block # */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003723 __le32 *p; /* Pointer into inode/ind
3724 for current block */
3725 int err;
3726
3727 if (this_bh) { /* For indirect block */
3728 BUFFER_TRACE(this_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003729 err = ext4_journal_get_write_access(handle, this_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003730 /* Important: if we can't update the indirect pointers
3731 * to the blocks, we can't free them. */
3732 if (err)
3733 return;
3734 }
3735
3736 for (p = first; p < last; p++) {
3737 nr = le32_to_cpu(*p);
3738 if (nr) {
3739 /* accumulate blocks to free if they're contiguous */
3740 if (count == 0) {
3741 block_to_free = nr;
3742 block_to_free_p = p;
3743 count = 1;
3744 } else if (nr == block_to_free + count) {
3745 count++;
3746 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003747 ext4_clear_blocks(handle, inode, this_bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003748 block_to_free,
3749 count, block_to_free_p, p);
3750 block_to_free = nr;
3751 block_to_free_p = p;
3752 count = 1;
3753 }
3754 }
3755 }
3756
3757 if (count > 0)
Mingming Cao617ba132006-10-11 01:20:53 -07003758 ext4_clear_blocks(handle, inode, this_bh, block_to_free,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003759 count, block_to_free_p, p);
3760
3761 if (this_bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05003762 BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata");
Duane Griffin71dc8fb2008-07-11 19:27:31 -04003763
3764 /*
3765 * The buffer head should have an attached journal head at this
3766 * point. However, if the data is corrupted and an indirect
3767 * block pointed to itself, it would have been detached when
3768 * the block was cleared. Check for this instead of OOPSing.
3769 */
Theodore Ts'oe7f07962009-01-20 09:50:19 -05003770 if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
Frank Mayhar03901312009-01-07 00:06:22 -05003771 ext4_handle_dirty_metadata(handle, inode, this_bh);
Duane Griffin71dc8fb2008-07-11 19:27:31 -04003772 else
3773 ext4_error(inode->i_sb, __func__,
3774 "circular indirect block detected, "
3775 "inode=%lu, block=%llu",
3776 inode->i_ino,
3777 (unsigned long long) this_bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003778 }
3779}
3780
3781/**
Mingming Cao617ba132006-10-11 01:20:53 -07003782 * ext4_free_branches - free an array of branches
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003783 * @handle: JBD handle for this transaction
3784 * @inode: inode we are dealing with
3785 * @parent_bh: the buffer_head which contains *@first and *@last
3786 * @first: array of block numbers
3787 * @last: pointer immediately past the end of array
3788 * @depth: depth of the branches to free
3789 *
3790 * We are freeing all blocks refered from these branches (numbers are
3791 * stored as little-endian 32-bit) and updating @inode->i_blocks
3792 * appropriately.
3793 */
Mingming Cao617ba132006-10-11 01:20:53 -07003794static void ext4_free_branches(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003795 struct buffer_head *parent_bh,
3796 __le32 *first, __le32 *last, int depth)
3797{
Mingming Cao617ba132006-10-11 01:20:53 -07003798 ext4_fsblk_t nr;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003799 __le32 *p;
3800
Frank Mayhar03901312009-01-07 00:06:22 -05003801 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003802 return;
3803
3804 if (depth--) {
3805 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07003806 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003807 p = last;
3808 while (--p >= first) {
3809 nr = le32_to_cpu(*p);
3810 if (!nr)
3811 continue; /* A hole */
3812
3813 /* Go read the buffer for the next level down */
3814 bh = sb_bread(inode->i_sb, nr);
3815
3816 /*
3817 * A read failure? Report error and clear slot
3818 * (should be rare).
3819 */
3820 if (!bh) {
Mingming Cao617ba132006-10-11 01:20:53 -07003821 ext4_error(inode->i_sb, "ext4_free_branches",
Mingming Cao2ae02102006-10-11 01:21:11 -07003822 "Read failure, inode=%lu, block=%llu",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003823 inode->i_ino, nr);
3824 continue;
3825 }
3826
3827 /* This zaps the entire block. Bottom up. */
3828 BUFFER_TRACE(bh, "free child branches");
Mingming Cao617ba132006-10-11 01:20:53 -07003829 ext4_free_branches(handle, inode, bh,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003830 (__le32 *) bh->b_data,
3831 (__le32 *) bh->b_data + addr_per_block,
3832 depth);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003833
3834 /*
3835 * We've probably journalled the indirect block several
3836 * times during the truncate. But it's no longer
3837 * needed and we now drop it from the transaction via
Mingming Caodab291a2006-10-11 01:21:01 -07003838 * jbd2_journal_revoke().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003839 *
3840 * That's easy if it's exclusively part of this
3841 * transaction. But if it's part of the committing
Mingming Caodab291a2006-10-11 01:21:01 -07003842 * transaction then jbd2_journal_forget() will simply
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003843 * brelse() it. That means that if the underlying
Mingming Cao617ba132006-10-11 01:20:53 -07003844 * block is reallocated in ext4_get_block(),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003845 * unmap_underlying_metadata() will find this block
3846 * and will try to get rid of it. damn, damn.
3847 *
3848 * If this block has already been committed to the
3849 * journal, a revoke record will be written. And
3850 * revoke records must be emitted *before* clearing
3851 * this block's bit in the bitmaps.
3852 */
Mingming Cao617ba132006-10-11 01:20:53 -07003853 ext4_forget(handle, 1, inode, bh, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003854
3855 /*
3856 * Everything below this this pointer has been
3857 * released. Now let this top-of-subtree go.
3858 *
3859 * We want the freeing of this indirect block to be
3860 * atomic in the journal with the updating of the
3861 * bitmap block which owns it. So make some room in
3862 * the journal.
3863 *
3864 * We zero the parent pointer *after* freeing its
3865 * pointee in the bitmaps, so if extend_transaction()
3866 * for some reason fails to put the bitmap changes and
3867 * the release into the same transaction, recovery
3868 * will merely complain about releasing a free block,
3869 * rather than leaking blocks.
3870 */
Frank Mayhar03901312009-01-07 00:06:22 -05003871 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003872 return;
3873 if (try_to_extend_transaction(handle, inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003874 ext4_mark_inode_dirty(handle, inode);
3875 ext4_journal_test_restart(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003876 }
3877
Alex Tomasc9de5602008-01-29 00:19:52 -05003878 ext4_free_blocks(handle, inode, nr, 1, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003879
3880 if (parent_bh) {
3881 /*
3882 * The block which we have just freed is
3883 * pointed to by an indirect block: journal it
3884 */
3885 BUFFER_TRACE(parent_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003886 if (!ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003887 parent_bh)){
3888 *p = 0;
3889 BUFFER_TRACE(parent_bh,
Frank Mayhar03901312009-01-07 00:06:22 -05003890 "call ext4_handle_dirty_metadata");
3891 ext4_handle_dirty_metadata(handle,
3892 inode,
3893 parent_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003894 }
3895 }
3896 }
3897 } else {
3898 /* We have reached the bottom of the tree. */
3899 BUFFER_TRACE(parent_bh, "free data blocks");
Mingming Cao617ba132006-10-11 01:20:53 -07003900 ext4_free_data(handle, inode, parent_bh, first, last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003901 }
3902}
3903
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003904int ext4_can_truncate(struct inode *inode)
3905{
3906 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3907 return 0;
3908 if (S_ISREG(inode->i_mode))
3909 return 1;
3910 if (S_ISDIR(inode->i_mode))
3911 return 1;
3912 if (S_ISLNK(inode->i_mode))
3913 return !ext4_inode_is_fast_symlink(inode);
3914 return 0;
3915}
3916
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003917/*
Mingming Cao617ba132006-10-11 01:20:53 -07003918 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003919 *
Mingming Cao617ba132006-10-11 01:20:53 -07003920 * We block out ext4_get_block() block instantiations across the entire
3921 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003922 * simultaneously on behalf of the same inode.
3923 *
3924 * As we work through the truncate and commmit bits of it to the journal there
3925 * is one core, guiding principle: the file's tree must always be consistent on
3926 * disk. We must be able to restart the truncate after a crash.
3927 *
3928 * The file's tree may be transiently inconsistent in memory (although it
3929 * probably isn't), but whenever we close off and commit a journal transaction,
3930 * the contents of (the filesystem + the journal) must be consistent and
3931 * restartable. It's pretty simple, really: bottom up, right to left (although
3932 * left-to-right works OK too).
3933 *
3934 * Note that at recovery time, journal replay occurs *before* the restart of
3935 * truncate against the orphan inode list.
3936 *
3937 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07003938 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003939 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07003940 * ext4_truncate() to have another go. So there will be instantiated blocks
3941 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003942 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07003943 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003944 */
Mingming Cao617ba132006-10-11 01:20:53 -07003945void ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003946{
3947 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -07003948 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003949 __le32 *i_data = ei->i_data;
Mingming Cao617ba132006-10-11 01:20:53 -07003950 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003951 struct address_space *mapping = inode->i_mapping;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003952 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003953 Indirect chain[4];
3954 Indirect *partial;
3955 __le32 nr = 0;
3956 int n;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003957 ext4_lblk_t last_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003958 unsigned blocksize = inode->i_sb->s_blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003959
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003960 if (!ext4_can_truncate(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003961 return;
3962
Theodore Ts'o0eab9282009-06-09 09:54:40 -04003963 if (ei->i_disksize && inode->i_size == 0 &&
3964 !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o7d8f9f72009-02-24 08:21:14 -05003965 ei->i_state |= EXT4_STATE_DA_ALLOC_CLOSE;
3966
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003967 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
Jan Karacf108bc2008-07-11 19:27:31 -04003968 ext4_ext_truncate(inode);
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003969 return;
3970 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003971
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003972 handle = start_transaction(inode);
Jan Karacf108bc2008-07-11 19:27:31 -04003973 if (IS_ERR(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003974 return; /* AKPM: return what? */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003975
3976 last_block = (inode->i_size + blocksize-1)
Mingming Cao617ba132006-10-11 01:20:53 -07003977 >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003978
Jan Karacf108bc2008-07-11 19:27:31 -04003979 if (inode->i_size & (blocksize - 1))
3980 if (ext4_block_truncate_page(handle, mapping, inode->i_size))
3981 goto out_stop;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003982
Mingming Cao617ba132006-10-11 01:20:53 -07003983 n = ext4_block_to_path(inode, last_block, offsets, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003984 if (n == 0)
3985 goto out_stop; /* error */
3986
3987 /*
3988 * OK. This truncate is going to happen. We add the inode to the
3989 * orphan list, so that if this truncate spans multiple transactions,
3990 * and we crash, we will resume the truncate when the filesystem
3991 * recovers. It also marks the inode dirty, to catch the new size.
3992 *
3993 * Implication: the file must always be in a sane, consistent
3994 * truncatable state while each transaction commits.
3995 */
Mingming Cao617ba132006-10-11 01:20:53 -07003996 if (ext4_orphan_add(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003997 goto out_stop;
3998
3999 /*
Mingming Cao632eaea2008-07-11 19:27:31 -04004000 * From here we block out all ext4_get_block() callers who want to
4001 * modify the block allocation tree.
4002 */
4003 down_write(&ei->i_data_sem);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04004004
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004005 ext4_discard_preallocations(inode);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04004006
Mingming Cao632eaea2008-07-11 19:27:31 -04004007 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004008 * The orphan list entry will now protect us from any crash which
4009 * occurs before the truncate completes, so it is now safe to propagate
4010 * the new, shorter inode size (held for now in i_size) into the
4011 * on-disk inode. We do this via i_disksize, which is the value which
Mingming Cao617ba132006-10-11 01:20:53 -07004012 * ext4 *really* writes onto the disk inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004013 */
4014 ei->i_disksize = inode->i_size;
4015
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004016 if (n == 1) { /* direct blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07004017 ext4_free_data(handle, inode, NULL, i_data+offsets[0],
4018 i_data + EXT4_NDIR_BLOCKS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004019 goto do_indirects;
4020 }
4021
Mingming Cao617ba132006-10-11 01:20:53 -07004022 partial = ext4_find_shared(inode, n, offsets, chain, &nr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004023 /* Kill the top of shared branch (not detached) */
4024 if (nr) {
4025 if (partial == chain) {
4026 /* Shared branch grows from the inode */
Mingming Cao617ba132006-10-11 01:20:53 -07004027 ext4_free_branches(handle, inode, NULL,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004028 &nr, &nr+1, (chain+n-1) - partial);
4029 *partial->p = 0;
4030 /*
4031 * We mark the inode dirty prior to restart,
4032 * and prior to stop. No need for it here.
4033 */
4034 } else {
4035 /* Shared branch grows from an indirect block */
4036 BUFFER_TRACE(partial->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07004037 ext4_free_branches(handle, inode, partial->bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004038 partial->p,
4039 partial->p+1, (chain+n-1) - partial);
4040 }
4041 }
4042 /* Clear the ends of indirect blocks on the shared branch */
4043 while (partial > chain) {
Mingming Cao617ba132006-10-11 01:20:53 -07004044 ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004045 (__le32*)partial->bh->b_data+addr_per_block,
4046 (chain+n-1) - partial);
4047 BUFFER_TRACE(partial->bh, "call brelse");
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004048 brelse(partial->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004049 partial--;
4050 }
4051do_indirects:
4052 /* Kill the remaining (whole) subtrees */
4053 switch (offsets[0]) {
4054 default:
Mingming Cao617ba132006-10-11 01:20:53 -07004055 nr = i_data[EXT4_IND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004056 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004057 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
4058 i_data[EXT4_IND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004059 }
Mingming Cao617ba132006-10-11 01:20:53 -07004060 case EXT4_IND_BLOCK:
4061 nr = i_data[EXT4_DIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004062 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004063 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
4064 i_data[EXT4_DIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004065 }
Mingming Cao617ba132006-10-11 01:20:53 -07004066 case EXT4_DIND_BLOCK:
4067 nr = i_data[EXT4_TIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004068 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004069 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
4070 i_data[EXT4_TIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004071 }
Mingming Cao617ba132006-10-11 01:20:53 -07004072 case EXT4_TIND_BLOCK:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004073 ;
4074 }
4075
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004076 up_write(&ei->i_data_sem);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004077 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07004078 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004079
4080 /*
4081 * In a multi-transaction truncate, we only make the final transaction
4082 * synchronous
4083 */
4084 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05004085 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004086out_stop:
4087 /*
4088 * If this was a simple ftruncate(), and the file will remain alive
4089 * then we need to clear up the orphan record which we created above.
4090 * However, if this was a real unlink then we were called by
Mingming Cao617ba132006-10-11 01:20:53 -07004091 * ext4_delete_inode(), and we allow that function to clean up the
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004092 * orphan info for us.
4093 */
4094 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07004095 ext4_orphan_del(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004096
Mingming Cao617ba132006-10-11 01:20:53 -07004097 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004098}
4099
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004100/*
Mingming Cao617ba132006-10-11 01:20:53 -07004101 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004102 * underlying buffer_head on success. If 'in_mem' is true, we have all
4103 * data in memory that is needed to recreate the on-disk version of this
4104 * inode.
4105 */
Mingming Cao617ba132006-10-11 01:20:53 -07004106static int __ext4_get_inode_loc(struct inode *inode,
4107 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004108{
Theodore Ts'o240799c2008-10-09 23:53:47 -04004109 struct ext4_group_desc *gdp;
4110 struct buffer_head *bh;
4111 struct super_block *sb = inode->i_sb;
4112 ext4_fsblk_t block;
4113 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004114
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004115 iloc->bh = NULL;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004116 if (!ext4_valid_inum(sb, inode->i_ino))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004117 return -EIO;
4118
Theodore Ts'o240799c2008-10-09 23:53:47 -04004119 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
4120 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
4121 if (!gdp)
4122 return -EIO;
4123
4124 /*
4125 * Figure out the offset within the block group inode table
4126 */
4127 inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb));
4128 inode_offset = ((inode->i_ino - 1) %
4129 EXT4_INODES_PER_GROUP(sb));
4130 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4131 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4132
4133 bh = sb_getblk(sb, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004134 if (!bh) {
Theodore Ts'o240799c2008-10-09 23:53:47 -04004135 ext4_error(sb, "ext4_get_inode_loc", "unable to read "
4136 "inode block - inode=%lu, block=%llu",
4137 inode->i_ino, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004138 return -EIO;
4139 }
4140 if (!buffer_uptodate(bh)) {
4141 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04004142
4143 /*
4144 * If the buffer has the write error flag, we have failed
4145 * to write out another inode in the same block. In this
4146 * case, we don't have to read the block because we may
4147 * read the old inode data successfully.
4148 */
4149 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
4150 set_buffer_uptodate(bh);
4151
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004152 if (buffer_uptodate(bh)) {
4153 /* someone brought it uptodate while we waited */
4154 unlock_buffer(bh);
4155 goto has_buffer;
4156 }
4157
4158 /*
4159 * If we have all information of the inode in memory and this
4160 * is the only valid inode in the block, we need not read the
4161 * block.
4162 */
4163 if (in_mem) {
4164 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004165 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004166
Theodore Ts'o240799c2008-10-09 23:53:47 -04004167 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004168
4169 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004170 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004171 if (!bitmap_bh)
4172 goto make_io;
4173
4174 /*
4175 * If the inode bitmap isn't in cache then the
4176 * optimisation may end up performing two reads instead
4177 * of one, so skip it.
4178 */
4179 if (!buffer_uptodate(bitmap_bh)) {
4180 brelse(bitmap_bh);
4181 goto make_io;
4182 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04004183 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004184 if (i == inode_offset)
4185 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07004186 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004187 break;
4188 }
4189 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004190 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004191 /* all other inodes are free, so skip I/O */
4192 memset(bh->b_data, 0, bh->b_size);
4193 set_buffer_uptodate(bh);
4194 unlock_buffer(bh);
4195 goto has_buffer;
4196 }
4197 }
4198
4199make_io:
4200 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04004201 * If we need to do any I/O, try to pre-readahead extra
4202 * blocks from the inode table.
4203 */
4204 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4205 ext4_fsblk_t b, end, table;
4206 unsigned num;
4207
4208 table = ext4_inode_table(sb, gdp);
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004209 /* s_inode_readahead_blks is always a power of 2 */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004210 b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
4211 if (table > b)
4212 b = table;
4213 end = b + EXT4_SB(sb)->s_inode_readahead_blks;
4214 num = EXT4_INODES_PER_GROUP(sb);
4215 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4216 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004217 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004218 table += num / inodes_per_block;
4219 if (end > table)
4220 end = table;
4221 while (b <= end)
4222 sb_breadahead(sb, b++);
4223 }
4224
4225 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004226 * There are other valid inodes in the buffer, this inode
4227 * has in-inode xattrs, or we don't have this inode in memory.
4228 * Read the block from disk.
4229 */
4230 get_bh(bh);
4231 bh->b_end_io = end_buffer_read_sync;
4232 submit_bh(READ_META, bh);
4233 wait_on_buffer(bh);
4234 if (!buffer_uptodate(bh)) {
Theodore Ts'o240799c2008-10-09 23:53:47 -04004235 ext4_error(sb, __func__,
4236 "unable to read inode block - inode=%lu, "
4237 "block=%llu", inode->i_ino, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004238 brelse(bh);
4239 return -EIO;
4240 }
4241 }
4242has_buffer:
4243 iloc->bh = bh;
4244 return 0;
4245}
4246
Mingming Cao617ba132006-10-11 01:20:53 -07004247int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004248{
4249 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07004250 return __ext4_get_inode_loc(inode, iloc,
4251 !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004252}
4253
Mingming Cao617ba132006-10-11 01:20:53 -07004254void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004255{
Mingming Cao617ba132006-10-11 01:20:53 -07004256 unsigned int flags = EXT4_I(inode)->i_flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004257
4258 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
Mingming Cao617ba132006-10-11 01:20:53 -07004259 if (flags & EXT4_SYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004260 inode->i_flags |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07004261 if (flags & EXT4_APPEND_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004262 inode->i_flags |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07004263 if (flags & EXT4_IMMUTABLE_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004264 inode->i_flags |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07004265 if (flags & EXT4_NOATIME_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004266 inode->i_flags |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004267 if (flags & EXT4_DIRSYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004268 inode->i_flags |= S_DIRSYNC;
4269}
4270
Jan Karaff9ddf72007-07-18 09:24:20 -04004271/* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4272void ext4_get_inode_flags(struct ext4_inode_info *ei)
4273{
4274 unsigned int flags = ei->vfs_inode.i_flags;
4275
4276 ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
4277 EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL);
4278 if (flags & S_SYNC)
4279 ei->i_flags |= EXT4_SYNC_FL;
4280 if (flags & S_APPEND)
4281 ei->i_flags |= EXT4_APPEND_FL;
4282 if (flags & S_IMMUTABLE)
4283 ei->i_flags |= EXT4_IMMUTABLE_FL;
4284 if (flags & S_NOATIME)
4285 ei->i_flags |= EXT4_NOATIME_FL;
4286 if (flags & S_DIRSYNC)
4287 ei->i_flags |= EXT4_DIRSYNC_FL;
4288}
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004289
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004290static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004291 struct ext4_inode_info *ei)
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004292{
4293 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004294 struct inode *inode = &(ei->vfs_inode);
4295 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004296
4297 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4298 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
4299 /* we are using combined 48 bit field */
4300 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4301 le32_to_cpu(raw_inode->i_blocks_lo);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004302 if (ei->i_flags & EXT4_HUGE_FILE_FL) {
4303 /* i_blocks represent file system block size */
4304 return i_blocks << (inode->i_blkbits - 9);
4305 } else {
4306 return i_blocks;
4307 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004308 } else {
4309 return le32_to_cpu(raw_inode->i_blocks_lo);
4310 }
4311}
Jan Karaff9ddf72007-07-18 09:24:20 -04004312
David Howells1d1fe1e2008-02-07 00:15:37 -08004313struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004314{
Mingming Cao617ba132006-10-11 01:20:53 -07004315 struct ext4_iloc iloc;
4316 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004317 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004318 struct buffer_head *bh;
David Howells1d1fe1e2008-02-07 00:15:37 -08004319 struct inode *inode;
4320 long ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004321 int block;
4322
David Howells1d1fe1e2008-02-07 00:15:37 -08004323 inode = iget_locked(sb, ino);
4324 if (!inode)
4325 return ERR_PTR(-ENOMEM);
4326 if (!(inode->i_state & I_NEW))
4327 return inode;
4328
4329 ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004330
David Howells1d1fe1e2008-02-07 00:15:37 -08004331 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4332 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004333 goto bad_inode;
4334 bh = iloc.bh;
Mingming Cao617ba132006-10-11 01:20:53 -07004335 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004336 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
4337 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4338 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004339 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004340 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4341 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
4342 }
4343 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004344
4345 ei->i_state = 0;
4346 ei->i_dir_start_lookup = 0;
4347 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4348 /* We now have enough fields to check if the inode was active or not.
4349 * This is needed because nfsd might try to access dead inodes
4350 * the test is that same one that e2fsck uses
4351 * NeilBrown 1999oct15
4352 */
4353 if (inode->i_nlink == 0) {
4354 if (inode->i_mode == 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07004355 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004356 /* this inode is deleted */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004357 brelse(bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004358 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004359 goto bad_inode;
4360 }
4361 /* The only unlinked inodes we let through here have
4362 * valid i_mode and are being read by the orphan
4363 * recovery code: that's fine, we're about to complete
4364 * the process of deleting those. */
4365 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004366 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004367 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004368 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Theodore Ts'oa9e81742009-04-24 16:11:18 -04004369 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004370 ei->i_file_acl |=
4371 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004372 inode->i_size = ext4_isize(raw_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004373 ei->i_disksize = inode->i_size;
4374 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4375 ei->i_block_group = iloc.block_group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04004376 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004377 /*
4378 * NOTE! The in-memory inode i_data array is in little-endian order
4379 * even on big-endian machines: we do NOT byteswap the block numbers!
4380 */
Mingming Cao617ba132006-10-11 01:20:53 -07004381 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004382 ei->i_data[block] = raw_inode->i_block[block];
4383 INIT_LIST_HEAD(&ei->i_orphan);
4384
Eric Sandeen0040d982008-02-05 22:36:43 -05004385 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004386 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
Mingming Cao617ba132006-10-11 01:20:53 -07004387 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004388 EXT4_INODE_SIZE(inode->i_sb)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004389 brelse(bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004390 ret = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004391 goto bad_inode;
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004392 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004393 if (ei->i_extra_isize == 0) {
4394 /* The extra space is currently unused. Use it. */
Mingming Cao617ba132006-10-11 01:20:53 -07004395 ei->i_extra_isize = sizeof(struct ext4_inode) -
4396 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004397 } else {
4398 __le32 *magic = (void *)raw_inode +
Mingming Cao617ba132006-10-11 01:20:53 -07004399 EXT4_GOOD_OLD_INODE_SIZE +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004400 ei->i_extra_isize;
Mingming Cao617ba132006-10-11 01:20:53 -07004401 if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC))
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004402 ei->i_state |= EXT4_STATE_XATTR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004403 }
4404 } else
4405 ei->i_extra_isize = 0;
4406
Kalpak Shahef7f3832007-07-18 09:15:20 -04004407 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4408 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4409 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4410 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4411
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004412 inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
4413 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4414 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4415 inode->i_version |=
4416 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4417 }
4418
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004419 ret = 0;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004420 if (ei->i_file_acl &&
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004421 ((ei->i_file_acl <
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004422 (le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) +
4423 EXT4_SB(sb)->s_gdb_count)) ||
4424 (ei->i_file_acl >= ext4_blocks_count(EXT4_SB(sb)->s_es)))) {
4425 ext4_error(sb, __func__,
4426 "bad extended attribute block %llu in inode #%lu",
4427 ei->i_file_acl, inode->i_ino);
4428 ret = -EIO;
4429 goto bad_inode;
4430 } else if (ei->i_flags & EXT4_EXTENTS_FL) {
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004431 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4432 (S_ISLNK(inode->i_mode) &&
4433 !ext4_inode_is_fast_symlink(inode)))
4434 /* Validate extent which is part of inode */
4435 ret = ext4_ext_check_inode(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004436 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004437 (S_ISLNK(inode->i_mode) &&
4438 !ext4_inode_is_fast_symlink(inode))) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004439 /* Validate block references which are part of inode */
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004440 ret = ext4_check_inode_blockref(inode);
4441 }
4442 if (ret) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004443 brelse(bh);
4444 goto bad_inode;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -04004445 }
4446
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004447 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004448 inode->i_op = &ext4_file_inode_operations;
4449 inode->i_fop = &ext4_file_operations;
4450 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004451 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004452 inode->i_op = &ext4_dir_inode_operations;
4453 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004454 } else if (S_ISLNK(inode->i_mode)) {
Duane Griffine83c1392008-12-19 20:47:15 +00004455 if (ext4_inode_is_fast_symlink(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004456 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00004457 nd_terminate_link(ei->i_data, inode->i_size,
4458 sizeof(ei->i_data) - 1);
4459 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004460 inode->i_op = &ext4_symlink_inode_operations;
4461 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004462 }
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004463 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4464 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004465 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004466 if (raw_inode->i_block[0])
4467 init_special_inode(inode, inode->i_mode,
4468 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4469 else
4470 init_special_inode(inode, inode->i_mode,
4471 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004472 } else {
4473 brelse(bh);
4474 ret = -EIO;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004475 ext4_error(inode->i_sb, __func__,
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004476 "bogus i_mode (%o) for inode=%lu",
4477 inode->i_mode, inode->i_ino);
4478 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004479 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004480 brelse(iloc.bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004481 ext4_set_inode_flags(inode);
David Howells1d1fe1e2008-02-07 00:15:37 -08004482 unlock_new_inode(inode);
4483 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004484
4485bad_inode:
David Howells1d1fe1e2008-02-07 00:15:37 -08004486 iget_failed(inode);
4487 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004488}
4489
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004490static int ext4_inode_blocks_set(handle_t *handle,
4491 struct ext4_inode *raw_inode,
4492 struct ext4_inode_info *ei)
4493{
4494 struct inode *inode = &(ei->vfs_inode);
4495 u64 i_blocks = inode->i_blocks;
4496 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004497
4498 if (i_blocks <= ~0U) {
4499 /*
4500 * i_blocks can be represnted in a 32 bit variable
4501 * as multiple of 512 bytes
4502 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004503 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004504 raw_inode->i_blocks_high = 0;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004505 ei->i_flags &= ~EXT4_HUGE_FILE_FL;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004506 return 0;
4507 }
4508 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
4509 return -EFBIG;
4510
4511 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004512 /*
4513 * i_blocks can be represented in a 48 bit variable
4514 * as multiple of 512 bytes
4515 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004516 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004517 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004518 ei->i_flags &= ~EXT4_HUGE_FILE_FL;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004519 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004520 ei->i_flags |= EXT4_HUGE_FILE_FL;
4521 /* i_block is stored in file system block size */
4522 i_blocks = i_blocks >> (inode->i_blkbits - 9);
4523 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4524 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004525 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004526 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004527}
4528
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004529/*
4530 * Post the struct inode info into an on-disk inode location in the
4531 * buffer-cache. This gobbles the caller's reference to the
4532 * buffer_head in the inode location struct.
4533 *
4534 * The caller must have write access to iloc->bh.
4535 */
Mingming Cao617ba132006-10-11 01:20:53 -07004536static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004537 struct inode *inode,
Mingming Cao617ba132006-10-11 01:20:53 -07004538 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004539{
Mingming Cao617ba132006-10-11 01:20:53 -07004540 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4541 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004542 struct buffer_head *bh = iloc->bh;
4543 int err = 0, rc, block;
4544
4545 /* For fields not not tracking in the in-memory inode,
4546 * initialise them to zero for new inodes. */
Mingming Cao617ba132006-10-11 01:20:53 -07004547 if (ei->i_state & EXT4_STATE_NEW)
4548 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004549
Jan Karaff9ddf72007-07-18 09:24:20 -04004550 ext4_get_inode_flags(ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004551 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004552 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004553 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
4554 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
4555/*
4556 * Fix up interoperability with old kernels. Otherwise, old inodes get
4557 * re-used with the upper 16 bits of the uid/gid intact
4558 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004559 if (!ei->i_dtime) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004560 raw_inode->i_uid_high =
4561 cpu_to_le16(high_16_bits(inode->i_uid));
4562 raw_inode->i_gid_high =
4563 cpu_to_le16(high_16_bits(inode->i_gid));
4564 } else {
4565 raw_inode->i_uid_high = 0;
4566 raw_inode->i_gid_high = 0;
4567 }
4568 } else {
4569 raw_inode->i_uid_low =
4570 cpu_to_le16(fs_high2lowuid(inode->i_uid));
4571 raw_inode->i_gid_low =
4572 cpu_to_le16(fs_high2lowgid(inode->i_gid));
4573 raw_inode->i_uid_high = 0;
4574 raw_inode->i_gid_high = 0;
4575 }
4576 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004577
4578 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4579 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4580 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4581 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4582
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004583 if (ext4_inode_blocks_set(handle, raw_inode, ei))
4584 goto out_brelse;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004585 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04004586 /* clear the migrate flag in the raw_inode */
4587 raw_inode->i_flags = cpu_to_le32(ei->i_flags & ~EXT4_EXT_MIGRATE);
Mingming Cao9b8f1f02006-10-11 01:21:13 -07004588 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
4589 cpu_to_le32(EXT4_OS_HURD))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004590 raw_inode->i_file_acl_high =
4591 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004592 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004593 ext4_isize_set(raw_inode, ei->i_disksize);
4594 if (ei->i_disksize > 0x7fffffffULL) {
4595 struct super_block *sb = inode->i_sb;
4596 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
4597 EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
4598 EXT4_SB(sb)->s_es->s_rev_level ==
4599 cpu_to_le32(EXT4_GOOD_OLD_REV)) {
4600 /* If this is the first large file
4601 * created, add a flag to the superblock.
4602 */
4603 err = ext4_journal_get_write_access(handle,
4604 EXT4_SB(sb)->s_sbh);
4605 if (err)
4606 goto out_brelse;
4607 ext4_update_dynamic_rev(sb);
4608 EXT4_SET_RO_COMPAT_FEATURE(sb,
Mingming Cao617ba132006-10-11 01:20:53 -07004609 EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004610 sb->s_dirt = 1;
Frank Mayhar03901312009-01-07 00:06:22 -05004611 ext4_handle_sync(handle);
4612 err = ext4_handle_dirty_metadata(handle, inode,
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004613 EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004614 }
4615 }
4616 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
4617 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4618 if (old_valid_dev(inode->i_rdev)) {
4619 raw_inode->i_block[0] =
4620 cpu_to_le32(old_encode_dev(inode->i_rdev));
4621 raw_inode->i_block[1] = 0;
4622 } else {
4623 raw_inode->i_block[0] = 0;
4624 raw_inode->i_block[1] =
4625 cpu_to_le32(new_encode_dev(inode->i_rdev));
4626 raw_inode->i_block[2] = 0;
4627 }
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004628 } else
4629 for (block = 0; block < EXT4_N_BLOCKS; block++)
4630 raw_inode->i_block[block] = ei->i_data[block];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004631
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004632 raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
4633 if (ei->i_extra_isize) {
4634 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4635 raw_inode->i_version_hi =
4636 cpu_to_le32(inode->i_version >> 32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004637 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004638 }
4639
Frank Mayhar03901312009-01-07 00:06:22 -05004640 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
4641 rc = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004642 if (!err)
4643 err = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07004644 ei->i_state &= ~EXT4_STATE_NEW;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004645
4646out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004647 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004648 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004649 return err;
4650}
4651
4652/*
Mingming Cao617ba132006-10-11 01:20:53 -07004653 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004654 *
4655 * We are called from a few places:
4656 *
4657 * - Within generic_file_write() for O_SYNC files.
4658 * Here, there will be no transaction running. We wait for any running
4659 * trasnaction to commit.
4660 *
4661 * - Within sys_sync(), kupdate and such.
4662 * We wait on commit, if tol to.
4663 *
4664 * - Within prune_icache() (PF_MEMALLOC == true)
4665 * Here we simply return. We can't afford to block kswapd on the
4666 * journal commit.
4667 *
4668 * In all cases it is actually safe for us to return without doing anything,
4669 * because the inode has been copied into a raw inode buffer in
Mingming Cao617ba132006-10-11 01:20:53 -07004670 * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004671 * knfsd.
4672 *
4673 * Note that we are absolutely dependent upon all inode dirtiers doing the
4674 * right thing: they *must* call mark_inode_dirty() after dirtying info in
4675 * which we are interested.
4676 *
4677 * It would be a bug for them to not do this. The code:
4678 *
4679 * mark_inode_dirty(inode)
4680 * stuff();
4681 * inode->i_size = expr;
4682 *
4683 * is in error because a kswapd-driven write_inode() could occur while
4684 * `stuff()' is running, and the new i_size will be lost. Plus the inode
4685 * will no longer be on the superblock's dirty inode list.
4686 */
Mingming Cao617ba132006-10-11 01:20:53 -07004687int ext4_write_inode(struct inode *inode, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004688{
4689 if (current->flags & PF_MEMALLOC)
4690 return 0;
4691
Mingming Cao617ba132006-10-11 01:20:53 -07004692 if (ext4_journal_current_handle()) {
Mingming Caob38bd332007-07-19 01:48:35 -07004693 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004694 dump_stack();
4695 return -EIO;
4696 }
4697
4698 if (!wait)
4699 return 0;
4700
Mingming Cao617ba132006-10-11 01:20:53 -07004701 return ext4_force_commit(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004702}
4703
4704/*
Mingming Cao617ba132006-10-11 01:20:53 -07004705 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004706 *
4707 * Called from notify_change.
4708 *
4709 * We want to trap VFS attempts to truncate the file as soon as
4710 * possible. In particular, we want to make sure that when the VFS
4711 * shrinks i_size, we put the inode on the orphan list and modify
4712 * i_disksize immediately, so that during the subsequent flushing of
4713 * dirty pages and freeing of disk blocks, we can guarantee that any
4714 * commit will leave the blocks being flushed in an unused state on
4715 * disk. (On recovery, the inode will get truncated and the blocks will
4716 * be freed, so we have a strong guarantee that no future commit will
4717 * leave these blocks visible to the user.)
4718 *
Jan Kara678aaf42008-07-11 19:27:31 -04004719 * Another thing we have to assure is that if we are in ordered mode
4720 * and inode is still attached to the committing transaction, we must
4721 * we start writeout of all the dirty pages which are being truncated.
4722 * This way we are sure that all the data written in the previous
4723 * transaction are already on disk (truncate waits for pages under
4724 * writeback).
4725 *
4726 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004727 */
Mingming Cao617ba132006-10-11 01:20:53 -07004728int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004729{
4730 struct inode *inode = dentry->d_inode;
4731 int error, rc = 0;
4732 const unsigned int ia_valid = attr->ia_valid;
4733
4734 error = inode_change_ok(inode, attr);
4735 if (error)
4736 return error;
4737
4738 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
4739 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
4740 handle_t *handle;
4741
4742 /* (user+group)*(old+new) structure, inode write (sb,
4743 * inode block, ? - but truncate inode update has it) */
Mingming Cao617ba132006-10-11 01:20:53 -07004744 handle = ext4_journal_start(inode, 2*(EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)+
4745 EXT4_QUOTA_DEL_BLOCKS(inode->i_sb))+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004746 if (IS_ERR(handle)) {
4747 error = PTR_ERR(handle);
4748 goto err_out;
4749 }
Jan Karaa269eb12009-01-26 17:04:39 +01004750 error = vfs_dq_transfer(inode, attr) ? -EDQUOT : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004751 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07004752 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004753 return error;
4754 }
4755 /* Update corresponding info in inode so that everything is in
4756 * one transaction */
4757 if (attr->ia_valid & ATTR_UID)
4758 inode->i_uid = attr->ia_uid;
4759 if (attr->ia_valid & ATTR_GID)
4760 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07004761 error = ext4_mark_inode_dirty(handle, inode);
4762 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004763 }
4764
Eric Sandeene2b46572008-01-28 23:58:27 -05004765 if (attr->ia_valid & ATTR_SIZE) {
4766 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) {
4767 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4768
4769 if (attr->ia_size > sbi->s_bitmap_maxbytes) {
4770 error = -EFBIG;
4771 goto err_out;
4772 }
4773 }
4774 }
4775
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004776 if (S_ISREG(inode->i_mode) &&
4777 attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
4778 handle_t *handle;
4779
Mingming Cao617ba132006-10-11 01:20:53 -07004780 handle = ext4_journal_start(inode, 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004781 if (IS_ERR(handle)) {
4782 error = PTR_ERR(handle);
4783 goto err_out;
4784 }
4785
Mingming Cao617ba132006-10-11 01:20:53 -07004786 error = ext4_orphan_add(handle, inode);
4787 EXT4_I(inode)->i_disksize = attr->ia_size;
4788 rc = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004789 if (!error)
4790 error = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07004791 ext4_journal_stop(handle);
Jan Kara678aaf42008-07-11 19:27:31 -04004792
4793 if (ext4_should_order_data(inode)) {
4794 error = ext4_begin_ordered_truncate(inode,
4795 attr->ia_size);
4796 if (error) {
4797 /* Do as much error cleanup as possible */
4798 handle = ext4_journal_start(inode, 3);
4799 if (IS_ERR(handle)) {
4800 ext4_orphan_del(NULL, inode);
4801 goto err_out;
4802 }
4803 ext4_orphan_del(handle, inode);
4804 ext4_journal_stop(handle);
4805 goto err_out;
4806 }
4807 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004808 }
4809
4810 rc = inode_setattr(inode, attr);
4811
Mingming Cao617ba132006-10-11 01:20:53 -07004812 /* If inode_setattr's call to ext4_truncate failed to get a
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004813 * transaction handle at all, we need to clean up the in-core
4814 * orphan list manually. */
4815 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07004816 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004817
4818 if (!rc && (ia_valid & ATTR_MODE))
Mingming Cao617ba132006-10-11 01:20:53 -07004819 rc = ext4_acl_chmod(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004820
4821err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07004822 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004823 if (!error)
4824 error = rc;
4825 return error;
4826}
4827
Mingming Cao3e3398a2008-07-11 19:27:31 -04004828int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
4829 struct kstat *stat)
4830{
4831 struct inode *inode;
4832 unsigned long delalloc_blocks;
4833
4834 inode = dentry->d_inode;
4835 generic_fillattr(inode, stat);
4836
4837 /*
4838 * We can't update i_blocks if the block allocation is delayed
4839 * otherwise in the case of system crash before the real block
4840 * allocation is done, we will have i_blocks inconsistent with
4841 * on-disk file blocks.
4842 * We always keep i_blocks updated together with real
4843 * allocation. But to not confuse with user, stat
4844 * will return the blocks that include the delayed allocation
4845 * blocks for this file.
4846 */
4847 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
4848 delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks;
4849 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
4850
4851 stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
4852 return 0;
4853}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004854
Mingming Caoa02908f2008-08-19 22:16:07 -04004855static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks,
4856 int chunk)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004857{
Mingming Caoa02908f2008-08-19 22:16:07 -04004858 int indirects;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004859
Mingming Caoa02908f2008-08-19 22:16:07 -04004860 /* if nrblocks are contiguous */
4861 if (chunk) {
4862 /*
4863 * With N contiguous data blocks, it need at most
4864 * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks
4865 * 2 dindirect blocks
4866 * 1 tindirect block
4867 */
4868 indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb);
4869 return indirects + 3;
4870 }
4871 /*
4872 * if nrblocks are not contiguous, worse case, each block touch
4873 * a indirect block, and each indirect block touch a double indirect
4874 * block, plus a triple indirect block
4875 */
4876 indirects = nrblocks * 2 + 1;
4877 return indirects;
4878}
Alex Tomasa86c6182006-10-11 01:21:03 -07004879
Mingming Caoa02908f2008-08-19 22:16:07 -04004880static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4881{
4882 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
Theodore Ts'oac51d832008-11-06 16:49:36 -05004883 return ext4_indirect_trans_blocks(inode, nrblocks, chunk);
4884 return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
Mingming Caoa02908f2008-08-19 22:16:07 -04004885}
Theodore Ts'oac51d832008-11-06 16:49:36 -05004886
Mingming Caoa02908f2008-08-19 22:16:07 -04004887/*
4888 * Account for index blocks, block groups bitmaps and block group
4889 * descriptor blocks if modify datablocks and index blocks
4890 * worse case, the indexs blocks spread over different block groups
4891 *
4892 * If datablocks are discontiguous, they are possible to spread over
4893 * different block groups too. If they are contiugous, with flexbg,
4894 * they could still across block group boundary.
4895 *
4896 * Also account for superblock, inode, quota and xattr blocks
4897 */
4898int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4899{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004900 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
4901 int gdpblocks;
Mingming Caoa02908f2008-08-19 22:16:07 -04004902 int idxblocks;
4903 int ret = 0;
4904
4905 /*
4906 * How many index blocks need to touch to modify nrblocks?
4907 * The "Chunk" flag indicating whether the nrblocks is
4908 * physically contiguous on disk
4909 *
4910 * For Direct IO and fallocate, they calls get_block to allocate
4911 * one single extent at a time, so they could set the "Chunk" flag
4912 */
4913 idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
4914
4915 ret = idxblocks;
4916
4917 /*
4918 * Now let's see how many group bitmaps and group descriptors need
4919 * to account
4920 */
4921 groups = idxblocks;
4922 if (chunk)
4923 groups += 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004924 else
Mingming Caoa02908f2008-08-19 22:16:07 -04004925 groups += nrblocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004926
Mingming Caoa02908f2008-08-19 22:16:07 -04004927 gdpblocks = groups;
Theodore Ts'o8df96752009-05-01 08:50:38 -04004928 if (groups > ngroups)
4929 groups = ngroups;
Mingming Caoa02908f2008-08-19 22:16:07 -04004930 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
4931 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
4932
4933 /* bitmaps and block group descriptor blocks */
4934 ret += groups + gdpblocks;
4935
4936 /* Blocks for super block, inode, quota and xattr blocks */
4937 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004938
4939 return ret;
4940}
4941
4942/*
Mingming Caoa02908f2008-08-19 22:16:07 -04004943 * Calulate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04004944 * the modification of a single pages into a single transaction,
4945 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04004946 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004947 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04004948 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004949 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04004950 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04004951 */
4952int ext4_writepage_trans_blocks(struct inode *inode)
4953{
4954 int bpp = ext4_journal_blocks_per_page(inode);
4955 int ret;
4956
4957 ret = ext4_meta_trans_blocks(inode, bpp, 0);
4958
4959 /* Account for data blocks for journalled mode */
4960 if (ext4_should_journal_data(inode))
4961 ret += bpp;
4962 return ret;
4963}
Mingming Caof3bd1f32008-08-19 22:16:03 -04004964
4965/*
4966 * Calculate the journal credits for a chunk of data modification.
4967 *
4968 * This is called from DIO, fallocate or whoever calling
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04004969 * ext4_get_blocks() to map/allocate a chunk of contigous disk blocks.
Mingming Caof3bd1f32008-08-19 22:16:03 -04004970 *
4971 * journal buffers for data blocks are not included here, as DIO
4972 * and fallocate do no need to journal data buffers.
4973 */
4974int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
4975{
4976 return ext4_meta_trans_blocks(inode, nrblocks, 1);
4977}
4978
Mingming Caoa02908f2008-08-19 22:16:07 -04004979/*
Mingming Cao617ba132006-10-11 01:20:53 -07004980 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004981 * Give this, we know that the caller already has write access to iloc->bh.
4982 */
Mingming Cao617ba132006-10-11 01:20:53 -07004983int ext4_mark_iloc_dirty(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004984 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004985{
4986 int err = 0;
4987
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004988 if (test_opt(inode->i_sb, I_VERSION))
4989 inode_inc_iversion(inode);
4990
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004991 /* the do_update_inode consumes one bh->b_count */
4992 get_bh(iloc->bh);
4993
Mingming Caodab291a2006-10-11 01:21:01 -07004994 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Mingming Cao617ba132006-10-11 01:20:53 -07004995 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004996 put_bh(iloc->bh);
4997 return err;
4998}
4999
5000/*
5001 * On success, We end up with an outstanding reference count against
5002 * iloc->bh. This _must_ be cleaned up later.
5003 */
5004
5005int
Mingming Cao617ba132006-10-11 01:20:53 -07005006ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
5007 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005008{
Frank Mayhar03901312009-01-07 00:06:22 -05005009 int err;
5010
5011 err = ext4_get_inode_loc(inode, iloc);
5012 if (!err) {
5013 BUFFER_TRACE(iloc->bh, "get_write_access");
5014 err = ext4_journal_get_write_access(handle, iloc->bh);
5015 if (err) {
5016 brelse(iloc->bh);
5017 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005018 }
5019 }
Mingming Cao617ba132006-10-11 01:20:53 -07005020 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005021 return err;
5022}
5023
5024/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005025 * Expand an inode by new_extra_isize bytes.
5026 * Returns 0 on success or negative error number on failure.
5027 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05005028static int ext4_expand_extra_isize(struct inode *inode,
5029 unsigned int new_extra_isize,
5030 struct ext4_iloc iloc,
5031 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005032{
5033 struct ext4_inode *raw_inode;
5034 struct ext4_xattr_ibody_header *header;
5035 struct ext4_xattr_entry *entry;
5036
5037 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
5038 return 0;
5039
5040 raw_inode = ext4_raw_inode(&iloc);
5041
5042 header = IHDR(inode, raw_inode);
5043 entry = IFIRST(header);
5044
5045 /* No extended attributes present */
5046 if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) ||
5047 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
5048 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
5049 new_extra_isize);
5050 EXT4_I(inode)->i_extra_isize = new_extra_isize;
5051 return 0;
5052 }
5053
5054 /* try to expand with EAs present */
5055 return ext4_expand_extra_isize_ea(inode, new_extra_isize,
5056 raw_inode, handle);
5057}
5058
5059/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005060 * What we do here is to mark the in-core inode as clean with respect to inode
5061 * dirtiness (it may still be data-dirty).
5062 * This means that the in-core inode may be reaped by prune_icache
5063 * without having to perform any I/O. This is a very good thing,
5064 * because *any* task may call prune_icache - even ones which
5065 * have a transaction open against a different journal.
5066 *
5067 * Is this cheating? Not really. Sure, we haven't written the
5068 * inode out, but prune_icache isn't a user-visible syncing function.
5069 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5070 * we start and wait on commits.
5071 *
5072 * Is this efficient/effective? Well, we're being nice to the system
5073 * by cleaning up our inodes proactively so they can be reaped
5074 * without I/O. But we are potentially leaving up to five seconds'
5075 * worth of inodes floating about which prune_icache wants us to
5076 * write out. One way to fix that would be to get prune_icache()
5077 * to do a write_super() to free up some memory. It has the desired
5078 * effect.
5079 */
Mingming Cao617ba132006-10-11 01:20:53 -07005080int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005081{
Mingming Cao617ba132006-10-11 01:20:53 -07005082 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005083 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5084 static unsigned int mnt_count;
5085 int err, ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005086
5087 might_sleep();
Mingming Cao617ba132006-10-11 01:20:53 -07005088 err = ext4_reserve_inode_write(handle, inode, &iloc);
Frank Mayhar03901312009-01-07 00:06:22 -05005089 if (ext4_handle_valid(handle) &&
5090 EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005091 !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) {
5092 /*
5093 * We need extra buffer credits since we may write into EA block
5094 * with this same handle. If journal_extend fails, then it will
5095 * only result in a minor loss of functionality for that inode.
5096 * If this is felt to be critical, then e2fsck should be run to
5097 * force a large enough s_min_extra_isize.
5098 */
5099 if ((jbd2_journal_extend(handle,
5100 EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
5101 ret = ext4_expand_extra_isize(inode,
5102 sbi->s_want_extra_isize,
5103 iloc, handle);
5104 if (ret) {
5105 EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND;
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005106 if (mnt_count !=
5107 le16_to_cpu(sbi->s_es->s_mnt_count)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04005108 ext4_warning(inode->i_sb, __func__,
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005109 "Unable to expand inode %lu. Delete"
5110 " some EAs or run e2fsck.",
5111 inode->i_ino);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005112 mnt_count =
5113 le16_to_cpu(sbi->s_es->s_mnt_count);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005114 }
5115 }
5116 }
5117 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005118 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07005119 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005120 return err;
5121}
5122
5123/*
Mingming Cao617ba132006-10-11 01:20:53 -07005124 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005125 *
5126 * We're really interested in the case where a file is being extended.
5127 * i_size has been changed by generic_commit_write() and we thus need
5128 * to include the updated inode in the current transaction.
5129 *
Jan Karaa269eb12009-01-26 17:04:39 +01005130 * Also, vfs_dq_alloc_block() will always dirty the inode when blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005131 * are allocated to the file.
5132 *
5133 * If the inode is marked synchronous, we don't honour that here - doing
5134 * so would cause a commit on atime updates, which we don't bother doing.
5135 * We handle synchronous inodes at the highest possible level.
5136 */
Mingming Cao617ba132006-10-11 01:20:53 -07005137void ext4_dirty_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005138{
Mingming Cao617ba132006-10-11 01:20:53 -07005139 handle_t *current_handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005140 handle_t *handle;
5141
Frank Mayhar03901312009-01-07 00:06:22 -05005142 if (!ext4_handle_valid(current_handle)) {
5143 ext4_mark_inode_dirty(current_handle, inode);
5144 return;
5145 }
5146
Mingming Cao617ba132006-10-11 01:20:53 -07005147 handle = ext4_journal_start(inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005148 if (IS_ERR(handle))
5149 goto out;
5150 if (current_handle &&
5151 current_handle->h_transaction != handle->h_transaction) {
5152 /* This task has a transaction open against a different fs */
5153 printk(KERN_EMERG "%s: transactions do not match!\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04005154 __func__);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005155 } else {
5156 jbd_debug(5, "marking dirty. outer handle=%p\n",
5157 current_handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005158 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005159 }
Mingming Cao617ba132006-10-11 01:20:53 -07005160 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005161out:
5162 return;
5163}
5164
5165#if 0
5166/*
5167 * Bind an inode's backing buffer_head into this transaction, to prevent
5168 * it from being flushed to disk early. Unlike
Mingming Cao617ba132006-10-11 01:20:53 -07005169 * ext4_reserve_inode_write, this leaves behind no bh reference and
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005170 * returns no iloc structure, so the caller needs to repeat the iloc
5171 * lookup to mark the inode dirty later.
5172 */
Mingming Cao617ba132006-10-11 01:20:53 -07005173static int ext4_pin_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005174{
Mingming Cao617ba132006-10-11 01:20:53 -07005175 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005176
5177 int err = 0;
5178 if (handle) {
Mingming Cao617ba132006-10-11 01:20:53 -07005179 err = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005180 if (!err) {
5181 BUFFER_TRACE(iloc.bh, "get_write_access");
Mingming Caodab291a2006-10-11 01:21:01 -07005182 err = jbd2_journal_get_write_access(handle, iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005183 if (!err)
Frank Mayhar03901312009-01-07 00:06:22 -05005184 err = ext4_handle_dirty_metadata(handle,
5185 inode,
5186 iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005187 brelse(iloc.bh);
5188 }
5189 }
Mingming Cao617ba132006-10-11 01:20:53 -07005190 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005191 return err;
5192}
5193#endif
5194
Mingming Cao617ba132006-10-11 01:20:53 -07005195int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005196{
5197 journal_t *journal;
5198 handle_t *handle;
5199 int err;
5200
5201 /*
5202 * We have to be very careful here: changing a data block's
5203 * journaling status dynamically is dangerous. If we write a
5204 * data block to the journal, change the status and then delete
5205 * that block, we risk forgetting to revoke the old log record
5206 * from the journal and so a subsequent replay can corrupt data.
5207 * So, first we make sure that the journal is empty and that
5208 * nobody is changing anything.
5209 */
5210
Mingming Cao617ba132006-10-11 01:20:53 -07005211 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005212 if (!journal)
5213 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04005214 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005215 return -EROFS;
5216
Mingming Caodab291a2006-10-11 01:21:01 -07005217 jbd2_journal_lock_updates(journal);
5218 jbd2_journal_flush(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005219
5220 /*
5221 * OK, there are no updates running now, and all cached data is
5222 * synced to disk. We are now in a completely consistent state
5223 * which doesn't have anything in the journal, and we know that
5224 * no filesystem updates are running, so it is safe to modify
5225 * the inode's in-core data-journaling state flag now.
5226 */
5227
5228 if (val)
Mingming Cao617ba132006-10-11 01:20:53 -07005229 EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005230 else
Mingming Cao617ba132006-10-11 01:20:53 -07005231 EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL;
5232 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005233
Mingming Caodab291a2006-10-11 01:21:01 -07005234 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005235
5236 /* Finally we can mark the inode as dirty. */
5237
Mingming Cao617ba132006-10-11 01:20:53 -07005238 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005239 if (IS_ERR(handle))
5240 return PTR_ERR(handle);
5241
Mingming Cao617ba132006-10-11 01:20:53 -07005242 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005243 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005244 ext4_journal_stop(handle);
5245 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005246
5247 return err;
5248}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005249
5250static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5251{
5252 return !buffer_mapped(bh);
5253}
5254
Nick Pigginc2ec1752009-03-31 15:23:21 -07005255int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005256{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005257 struct page *page = vmf->page;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005258 loff_t size;
5259 unsigned long len;
5260 int ret = -EINVAL;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005261 void *fsdata;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005262 struct file *file = vma->vm_file;
5263 struct inode *inode = file->f_path.dentry->d_inode;
5264 struct address_space *mapping = inode->i_mapping;
5265
5266 /*
5267 * Get i_alloc_sem to stop truncates messing with the inode. We cannot
5268 * get i_mutex because we are already holding mmap_sem.
5269 */
5270 down_read(&inode->i_alloc_sem);
5271 size = i_size_read(inode);
5272 if (page->mapping != mapping || size <= page_offset(page)
5273 || !PageUptodate(page)) {
5274 /* page got truncated from under us? */
5275 goto out_unlock;
5276 }
5277 ret = 0;
5278 if (PageMappedToDisk(page))
5279 goto out_unlock;
5280
5281 if (page->index == size >> PAGE_CACHE_SHIFT)
5282 len = size & ~PAGE_CACHE_MASK;
5283 else
5284 len = PAGE_CACHE_SIZE;
5285
5286 if (page_has_buffers(page)) {
5287 /* return if we have all the buffers mapped */
5288 if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
5289 ext4_bh_unmapped))
5290 goto out_unlock;
5291 }
5292 /*
5293 * OK, we need to fill the hole... Do write_begin write_end
5294 * to do block allocation/reservation.We are not holding
5295 * inode.i__mutex here. That allow * parallel write_begin,
5296 * write_end call. lock_page prevent this from happening
5297 * on the same page though
5298 */
5299 ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005300 len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005301 if (ret < 0)
5302 goto out_unlock;
5303 ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005304 len, len, page, fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005305 if (ret < 0)
5306 goto out_unlock;
5307 ret = 0;
5308out_unlock:
Nick Pigginc2ec1752009-03-31 15:23:21 -07005309 if (ret)
5310 ret = VM_FAULT_SIGBUS;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005311 up_read(&inode->i_alloc_sem);
5312 return ret;
5313}