blob: d61fb523308fd2caa89e020e9d4afbfc4f201444 [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/inode.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *
9 * from
10 *
11 * linux/fs/minix/inode.c
12 *
13 * Copyright (C) 1991, 1992 Linus Torvalds
14 *
15 * Goal-directed block allocation by Stephen Tweedie
16 * (sct@redhat.com), 1993, 1998
17 * Big-endian to little-endian byte-swapping/bitmaps by
18 * David S. Miller (davem@caip.rutgers.edu), 1995
19 * 64-bit file support on 64-bit platforms by Jakub Jelinek
20 * (jj@sunsite.ms.mff.cuni.cz)
21 *
Mingming Cao617ba132006-10-11 01:20:53 -070022 * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
Dave Kleikampac27a0e2006-10-11 01:20:50 -070023 */
24
25#include <linux/module.h>
26#include <linux/fs.h>
27#include <linux/time.h>
Mingming Caodab291a2006-10-11 01:21:01 -070028#include <linux/jbd2.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070029#include <linux/highuid.h>
30#include <linux/pagemap.h>
31#include <linux/quotaops.h>
32#include <linux/string.h>
33#include <linux/buffer_head.h>
34#include <linux/writeback.h>
Alex Tomas64769242008-07-11 19:27:31 -040035#include <linux/pagevec.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070036#include <linux/mpage.h>
Duane Griffine83c1392008-12-19 20:47:15 +000037#include <linux/namei.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070038#include <linux/uio.h>
39#include <linux/bio.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040040
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040041#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070042#include "xattr.h"
43#include "acl.h"
Mingming Caod2a17632008-07-14 17:52:37 -040044#include "ext4_extents.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070045
Theodore Ts'o9bffad12009-06-17 11:48:11 -040046#include <trace/events/ext4.h>
47
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -040048#define MPAGE_DA_EXTENT_TAIL 0x01
49
Jan Kara678aaf42008-07-11 19:27:31 -040050static inline int ext4_begin_ordered_truncate(struct inode *inode,
51 loff_t new_size)
52{
Jan Kara7f5aa212009-02-10 11:15:34 -050053 return jbd2_journal_begin_ordered_truncate(
54 EXT4_SB(inode->i_sb)->s_journal,
55 &EXT4_I(inode)->jinode,
56 new_size);
Jan Kara678aaf42008-07-11 19:27:31 -040057}
58
Alex Tomas64769242008-07-11 19:27:31 -040059static void ext4_invalidatepage(struct page *page, unsigned long offset);
60
Dave Kleikampac27a0e2006-10-11 01:20:50 -070061/*
62 * Test whether an inode is a fast symlink.
63 */
Mingming Cao617ba132006-10-11 01:20:53 -070064static int ext4_inode_is_fast_symlink(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070065{
Mingming Cao617ba132006-10-11 01:20:53 -070066 int ea_blocks = EXT4_I(inode)->i_file_acl ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -070067 (inode->i_sb->s_blocksize >> 9) : 0;
68
69 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
70}
71
72/*
Mingming Cao617ba132006-10-11 01:20:53 -070073 * The ext4 forget function must perform a revoke if we are freeing data
Dave Kleikampac27a0e2006-10-11 01:20:50 -070074 * which has been journaled. Metadata (eg. indirect blocks) must be
75 * revoked in all cases.
76 *
77 * "bh" may be NULL: a metadata block may have been freed from memory
78 * but there may still be a record of it in the journal, and that record
79 * still needs to be revoked.
Frank Mayhar03901312009-01-07 00:06:22 -050080 *
Curt Wohlgemuthe6b5d302009-07-13 09:07:20 -040081 * If the handle isn't valid we're not journaling, but we still need to
82 * call into ext4_journal_revoke() to put the buffer head.
Dave Kleikampac27a0e2006-10-11 01:20:50 -070083 */
Mingming Cao617ba132006-10-11 01:20:53 -070084int ext4_forget(handle_t *handle, int is_metadata, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -040085 struct buffer_head *bh, ext4_fsblk_t blocknr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070086{
87 int err;
88
89 might_sleep();
90
91 BUFFER_TRACE(bh, "enter");
92
93 jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
Theodore Ts'o7f4520c2009-06-13 10:09:41 -040094 "data mode %x\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -070095 bh, is_metadata, inode->i_mode,
96 test_opt(inode->i_sb, DATA_FLAGS));
97
98 /* Never use the revoke function if we are doing full data
99 * journaling: there is no need to, and a V1 superblock won't
100 * support it. Otherwise, only skip the revoke on un-journaled
101 * data blocks. */
102
Mingming Cao617ba132006-10-11 01:20:53 -0700103 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
104 (!is_metadata && !ext4_should_journal_data(inode))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700105 if (bh) {
Mingming Caodab291a2006-10-11 01:21:01 -0700106 BUFFER_TRACE(bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700107 return ext4_journal_forget(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700108 }
109 return 0;
110 }
111
112 /*
113 * data!=journal && (is_metadata || should_journal_data(inode))
114 */
Mingming Cao617ba132006-10-11 01:20:53 -0700115 BUFFER_TRACE(bh, "call ext4_journal_revoke");
116 err = ext4_journal_revoke(handle, blocknr, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700117 if (err)
Harvey Harrison46e665e2008-04-17 10:38:59 -0400118 ext4_abort(inode->i_sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700119 "error %d when attempting revoke", err);
120 BUFFER_TRACE(bh, "exit");
121 return err;
122}
123
124/*
125 * Work out how many blocks we need to proceed with the next chunk of a
126 * truncate transaction.
127 */
128static unsigned long blocks_for_truncate(struct inode *inode)
129{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500130 ext4_lblk_t needed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700131
132 needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
133
134 /* Give ourselves just enough room to cope with inodes in which
135 * i_blocks is corrupt: we've seen disk corruptions in the past
136 * which resulted in random data in an inode which looked enough
Mingming Cao617ba132006-10-11 01:20:53 -0700137 * like a regular file for ext4 to try to delete it. Things
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700138 * will go a bit crazy if that happens, but at least we should
139 * try not to panic the whole kernel. */
140 if (needed < 2)
141 needed = 2;
142
143 /* But we need to bound the transaction so we don't overflow the
144 * journal. */
Mingming Cao617ba132006-10-11 01:20:53 -0700145 if (needed > EXT4_MAX_TRANS_DATA)
146 needed = EXT4_MAX_TRANS_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700147
Mingming Cao617ba132006-10-11 01:20:53 -0700148 return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700149}
150
151/*
152 * Truncate transactions can be complex and absolutely huge. So we need to
153 * be able to restart the transaction at a conventient checkpoint to make
154 * sure we don't overflow the journal.
155 *
156 * start_transaction gets us a new handle for a truncate transaction,
157 * and extend_transaction tries to extend the existing one a bit. If
158 * extend fails, we need to propagate the failure up and restart the
159 * transaction in the top-level truncate loop. --sct
160 */
161static handle_t *start_transaction(struct inode *inode)
162{
163 handle_t *result;
164
Mingming Cao617ba132006-10-11 01:20:53 -0700165 result = ext4_journal_start(inode, blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700166 if (!IS_ERR(result))
167 return result;
168
Mingming Cao617ba132006-10-11 01:20:53 -0700169 ext4_std_error(inode->i_sb, PTR_ERR(result));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700170 return result;
171}
172
173/*
174 * Try to extend this transaction for the purposes of truncation.
175 *
176 * Returns 0 if we managed to create more room. If we can't create more
177 * room, and the transaction must be restarted we return 1.
178 */
179static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
180{
Frank Mayhar03901312009-01-07 00:06:22 -0500181 if (!ext4_handle_valid(handle))
182 return 0;
183 if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700184 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700185 if (!ext4_journal_extend(handle, blocks_for_truncate(inode)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700186 return 0;
187 return 1;
188}
189
190/*
191 * Restart the transaction associated with *handle. This does a commit,
192 * so before we call here everything must be consistently dirtied against
193 * this transaction.
194 */
Jan Kara487caee2009-08-17 22:17:20 -0400195 int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
196 int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700197{
Jan Kara487caee2009-08-17 22:17:20 -0400198 int ret;
199
200 /*
201 * Drop i_data_sem to avoid deadlock with ext4_get_blocks At this
202 * moment, get_block can be called only for blocks inside i_size since
203 * page cache has been already dropped and writes are blocked by
204 * i_mutex. So we can safely drop the i_data_sem here.
205 */
Frank Mayhar03901312009-01-07 00:06:22 -0500206 BUG_ON(EXT4_JOURNAL(inode) == NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700207 jbd_debug(2, "restarting handle %p\n", handle);
Jan Kara487caee2009-08-17 22:17:20 -0400208 up_write(&EXT4_I(inode)->i_data_sem);
209 ret = ext4_journal_restart(handle, blocks_for_truncate(inode));
210 down_write(&EXT4_I(inode)->i_data_sem);
211
212 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700213}
214
215/*
216 * Called at the last iput() if i_nlink is zero.
217 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400218void ext4_delete_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700219{
220 handle_t *handle;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400221 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700222
Jan Kara678aaf42008-07-11 19:27:31 -0400223 if (ext4_should_order_data(inode))
224 ext4_begin_ordered_truncate(inode, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700225 truncate_inode_pages(&inode->i_data, 0);
226
227 if (is_bad_inode(inode))
228 goto no_delete;
229
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400230 handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400232 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700233 /*
234 * If we're going to skip the normal cleanup, we still need to
235 * make sure that the in-core orphan linked list is properly
236 * cleaned up.
237 */
Mingming Cao617ba132006-10-11 01:20:53 -0700238 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700239 goto no_delete;
240 }
241
242 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500243 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700244 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400245 err = ext4_mark_inode_dirty(handle, inode);
246 if (err) {
247 ext4_warning(inode->i_sb, __func__,
248 "couldn't mark inode dirty (err %d)", err);
249 goto stop_handle;
250 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700251 if (inode->i_blocks)
Mingming Cao617ba132006-10-11 01:20:53 -0700252 ext4_truncate(inode);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400253
254 /*
255 * ext4_ext_truncate() doesn't reserve any slop when it
256 * restarts journal transactions; therefore there may not be
257 * enough credits left in the handle to remove the inode from
258 * the orphan list and set the dtime field.
259 */
Frank Mayhar03901312009-01-07 00:06:22 -0500260 if (!ext4_handle_has_enough_credits(handle, 3)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400261 err = ext4_journal_extend(handle, 3);
262 if (err > 0)
263 err = ext4_journal_restart(handle, 3);
264 if (err != 0) {
265 ext4_warning(inode->i_sb, __func__,
266 "couldn't extend journal (err %d)", err);
267 stop_handle:
268 ext4_journal_stop(handle);
269 goto no_delete;
270 }
271 }
272
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700273 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700274 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700275 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700276 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700277 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700278 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700279 * (Well, we could do this if we need to, but heck - it works)
280 */
Mingming Cao617ba132006-10-11 01:20:53 -0700281 ext4_orphan_del(handle, inode);
282 EXT4_I(inode)->i_dtime = get_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700283
284 /*
285 * One subtle ordering requirement: if anything has gone wrong
286 * (transaction abort, IO errors, whatever), then we can still
287 * do these next steps (the fs will already have been marked as
288 * having errors), but we can't free the inode if the mark_dirty
289 * fails.
290 */
Mingming Cao617ba132006-10-11 01:20:53 -0700291 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700292 /* If that failed, just do the required in-core inode clear. */
293 clear_inode(inode);
294 else
Mingming Cao617ba132006-10-11 01:20:53 -0700295 ext4_free_inode(handle, inode);
296 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700297 return;
298no_delete:
299 clear_inode(inode); /* We must guarantee clearing of inode... */
300}
301
302typedef struct {
303 __le32 *p;
304 __le32 key;
305 struct buffer_head *bh;
306} Indirect;
307
308static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
309{
310 p->key = *(p->p = v);
311 p->bh = bh;
312}
313
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700314/**
Mingming Cao617ba132006-10-11 01:20:53 -0700315 * ext4_block_to_path - parse the block number into array of offsets
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700316 * @inode: inode in question (we are only interested in its superblock)
317 * @i_block: block number to be parsed
318 * @offsets: array to store the offsets in
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400319 * @boundary: set this non-zero if the referred-to block is likely to be
320 * followed (on disk) by an indirect block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700321 *
Mingming Cao617ba132006-10-11 01:20:53 -0700322 * To store the locations of file's data ext4 uses a data structure common
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700323 * for UNIX filesystems - tree of pointers anchored in the inode, with
324 * data blocks at leaves and indirect blocks in intermediate nodes.
325 * This function translates the block number into path in that tree -
326 * return value is the path length and @offsets[n] is the offset of
327 * pointer to (n+1)th node in the nth one. If @block is out of range
328 * (negative or too large) warning is printed and zero returned.
329 *
330 * Note: function doesn't find node addresses, so no IO is needed. All
331 * we need to know is the capacity of indirect blocks (taken from the
332 * inode->i_sb).
333 */
334
335/*
336 * Portability note: the last comparison (check that we fit into triple
337 * indirect block) is spelled differently, because otherwise on an
338 * architecture with 32-bit longs and 8Kb pages we might get into trouble
339 * if our filesystem had 8Kb blocks. We might use long long, but that would
340 * kill us on x86. Oh, well, at least the sign propagation does not matter -
341 * i_block would have to be negative in the very beginning, so we would not
342 * get there at all.
343 */
344
Mingming Cao617ba132006-10-11 01:20:53 -0700345static int ext4_block_to_path(struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400346 ext4_lblk_t i_block,
347 ext4_lblk_t offsets[4], int *boundary)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700348{
Mingming Cao617ba132006-10-11 01:20:53 -0700349 int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb);
350 int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb);
351 const long direct_blocks = EXT4_NDIR_BLOCKS,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700352 indirect_blocks = ptrs,
353 double_blocks = (1 << (ptrs_bits * 2));
354 int n = 0;
355 int final = 0;
356
Roel Kluinc333e072009-08-10 22:47:22 -0400357 if (i_block < direct_blocks) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700358 offsets[n++] = i_block;
359 final = direct_blocks;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400360 } else if ((i_block -= direct_blocks) < indirect_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700361 offsets[n++] = EXT4_IND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700362 offsets[n++] = i_block;
363 final = ptrs;
364 } else if ((i_block -= indirect_blocks) < double_blocks) {
Mingming Cao617ba132006-10-11 01:20:53 -0700365 offsets[n++] = EXT4_DIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700366 offsets[n++] = i_block >> ptrs_bits;
367 offsets[n++] = i_block & (ptrs - 1);
368 final = ptrs;
369 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
Mingming Cao617ba132006-10-11 01:20:53 -0700370 offsets[n++] = EXT4_TIND_BLOCK;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700371 offsets[n++] = i_block >> (ptrs_bits * 2);
372 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
373 offsets[n++] = i_block & (ptrs - 1);
374 final = ptrs;
375 } else {
Eric Sandeene2b46572008-01-28 23:58:27 -0500376 ext4_warning(inode->i_sb, "ext4_block_to_path",
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400377 "block %lu > max in inode %lu",
378 i_block + direct_blocks +
379 indirect_blocks + double_blocks, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700380 }
381 if (boundary)
382 *boundary = final - 1 - (i_block & (ptrs - 1));
383 return n;
384}
385
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400386static int __ext4_check_blockref(const char *function, struct inode *inode,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400387 __le32 *p, unsigned int max)
388{
Thiemo Nagelf73953c2009-04-07 18:46:47 -0400389 __le32 *bref = p;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400390 unsigned int blk;
391
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400392 while (bref < p+max) {
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400393 blk = le32_to_cpu(*bref++);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400394 if (blk &&
395 unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb),
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400396 blk, 1))) {
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400397 ext4_error(inode->i_sb, function,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400398 "invalid block reference %u "
399 "in inode #%lu", blk, inode->i_ino);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400400 return -EIO;
401 }
402 }
403 return 0;
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400404}
405
406
407#define ext4_check_indirect_blockref(inode, bh) \
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400408 __ext4_check_blockref(__func__, inode, (__le32 *)(bh)->b_data, \
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400409 EXT4_ADDR_PER_BLOCK((inode)->i_sb))
410
411#define ext4_check_inode_blockref(inode) \
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400412 __ext4_check_blockref(__func__, inode, EXT4_I(inode)->i_data, \
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400413 EXT4_NDIR_BLOCKS)
414
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700415/**
Mingming Cao617ba132006-10-11 01:20:53 -0700416 * ext4_get_branch - read the chain of indirect blocks leading to data
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700417 * @inode: inode in question
418 * @depth: depth of the chain (1 - direct pointer, etc.)
419 * @offsets: offsets of pointers in inode/indirect blocks
420 * @chain: place to store the result
421 * @err: here we store the error value
422 *
423 * Function fills the array of triples <key, p, bh> and returns %NULL
424 * if everything went OK or the pointer to the last filled triple
425 * (incomplete one) otherwise. Upon the return chain[i].key contains
426 * the number of (i+1)-th block in the chain (as it is stored in memory,
427 * i.e. little-endian 32-bit), chain[i].p contains the address of that
428 * number (it points into struct inode for i==0 and into the bh->b_data
429 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
430 * block for i>0 and NULL for i==0. In other words, it holds the block
431 * numbers of the chain, addresses they were taken from (and where we can
432 * verify that chain did not change) and buffer_heads hosting these
433 * numbers.
434 *
435 * Function stops when it stumbles upon zero pointer (absent block)
436 * (pointer to last triple returned, *@err == 0)
437 * or when it gets an IO error reading an indirect block
438 * (ditto, *@err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700439 * or when it reads all @depth-1 indirect blocks successfully and finds
440 * the whole chain, all way to the data (returns %NULL, *err == 0).
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500441 *
442 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500443 * down_read(&EXT4_I(inode)->i_data_sem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700444 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500445static Indirect *ext4_get_branch(struct inode *inode, int depth,
446 ext4_lblk_t *offsets,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700447 Indirect chain[4], int *err)
448{
449 struct super_block *sb = inode->i_sb;
450 Indirect *p = chain;
451 struct buffer_head *bh;
452
453 *err = 0;
454 /* i_data is not going away, no lock needed */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400455 add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700456 if (!p->key)
457 goto no_block;
458 while (--depth) {
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400459 bh = sb_getblk(sb, le32_to_cpu(p->key));
460 if (unlikely(!bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700461 goto failure;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400462
Thiemo Nagelfe2c8192009-03-31 08:36:10 -0400463 if (!bh_uptodate_or_lock(bh)) {
464 if (bh_submit_read(bh) < 0) {
465 put_bh(bh);
466 goto failure;
467 }
468 /* validate block references */
469 if (ext4_check_indirect_blockref(inode, bh)) {
470 put_bh(bh);
471 goto failure;
472 }
473 }
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400474
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400475 add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700476 /* Reader: end */
477 if (!p->key)
478 goto no_block;
479 }
480 return NULL;
481
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700482failure:
483 *err = -EIO;
484no_block:
485 return p;
486}
487
488/**
Mingming Cao617ba132006-10-11 01:20:53 -0700489 * ext4_find_near - find a place for allocation with sufficient locality
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700490 * @inode: owner
491 * @ind: descriptor of indirect block.
492 *
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000493 * This function returns the preferred place for block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700494 * It is used when heuristic for sequential allocation fails.
495 * Rules are:
496 * + if there is a block to the left of our position - allocate near it.
497 * + if pointer will live in indirect block - allocate near that block.
498 * + if pointer will live in inode - allocate in the same
499 * cylinder group.
500 *
501 * In the latter case we colour the starting block by the callers PID to
502 * prevent it from clashing with concurrent allocations for a different inode
503 * in the same block group. The PID is used here so that functionally related
504 * files will be close-by on-disk.
505 *
506 * Caller must make sure that @ind is valid and will stay that way.
507 */
Mingming Cao617ba132006-10-11 01:20:53 -0700508static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700509{
Mingming Cao617ba132006-10-11 01:20:53 -0700510 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400511 __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700512 __le32 *p;
Mingming Cao617ba132006-10-11 01:20:53 -0700513 ext4_fsblk_t bg_start;
Valerie Clement74d34872008-02-15 13:43:07 -0500514 ext4_fsblk_t last_block;
Mingming Cao617ba132006-10-11 01:20:53 -0700515 ext4_grpblk_t colour;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400516 ext4_group_t block_group;
517 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700518
519 /* Try to find previous block */
520 for (p = ind->p - 1; p >= start; p--) {
521 if (*p)
522 return le32_to_cpu(*p);
523 }
524
525 /* No such thing, so let's try location of indirect block */
526 if (ind->bh)
527 return ind->bh->b_blocknr;
528
529 /*
530 * It is going to be referred to from the inode itself? OK, just put it
531 * into the same cylinder group then.
532 */
Theodore Ts'oa4912122009-03-12 12:18:34 -0400533 block_group = ei->i_block_group;
534 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
535 block_group &= ~(flex_size-1);
536 if (S_ISREG(inode->i_mode))
537 block_group++;
538 }
539 bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
Valerie Clement74d34872008-02-15 13:43:07 -0500540 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
541
Theodore Ts'oa4912122009-03-12 12:18:34 -0400542 /*
543 * If we are doing delayed allocation, we don't need take
544 * colour into account.
545 */
546 if (test_opt(inode->i_sb, DELALLOC))
547 return bg_start;
548
Valerie Clement74d34872008-02-15 13:43:07 -0500549 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
550 colour = (current->pid % 16) *
Mingming Cao617ba132006-10-11 01:20:53 -0700551 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
Valerie Clement74d34872008-02-15 13:43:07 -0500552 else
553 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700554 return bg_start + colour;
555}
556
557/**
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000558 * ext4_find_goal - find a preferred place for allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700559 * @inode: owner
560 * @block: block we want
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700561 * @partial: pointer to the last triple within a chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700562 *
Benoit Boissinot1cc8dcf2008-04-21 22:45:55 +0000563 * Normally this function find the preferred place for block allocation,
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800564 * returns it.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700565 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500566static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400567 Indirect *partial)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700568{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700569 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400570 * XXX need to get goal block from mballoc's data structures
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700571 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700572
Mingming Cao617ba132006-10-11 01:20:53 -0700573 return ext4_find_near(inode, partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700574}
575
576/**
Mingming Cao617ba132006-10-11 01:20:53 -0700577 * ext4_blks_to_allocate: Look up the block map and count the number
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700578 * of direct blocks need to be allocated for the given branch.
579 *
580 * @branch: chain of indirect blocks
581 * @k: number of blocks need for indirect blocks
582 * @blks: number of data blocks to be mapped.
583 * @blocks_to_boundary: the offset in the indirect block
584 *
585 * return the total number of blocks to be allocate, including the
586 * direct and indirect blocks.
587 */
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500588static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400589 int blocks_to_boundary)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700590{
Theodore Ts'o498e5f22008-11-05 00:14:04 -0500591 unsigned int count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700592
593 /*
594 * Simple case, [t,d]Indirect block(s) has not allocated yet
595 * then it's clear blocks on that path have not allocated
596 */
597 if (k > 0) {
598 /* right now we don't handle cross boundary allocation */
599 if (blks < blocks_to_boundary + 1)
600 count += blks;
601 else
602 count += blocks_to_boundary + 1;
603 return count;
604 }
605
606 count++;
607 while (count < blks && count <= blocks_to_boundary &&
608 le32_to_cpu(*(branch[0].p + count)) == 0) {
609 count++;
610 }
611 return count;
612}
613
614/**
Mingming Cao617ba132006-10-11 01:20:53 -0700615 * ext4_alloc_blocks: multiple allocate blocks needed for a branch
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700616 * @indirect_blks: the number of blocks need to allocate for indirect
617 * blocks
618 *
619 * @new_blocks: on return it will store the new block numbers for
620 * the indirect blocks(if needed) and the first direct block,
621 * @blks: on return it will store the total number of allocated
622 * direct blocks
623 */
Mingming Cao617ba132006-10-11 01:20:53 -0700624static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400625 ext4_lblk_t iblock, ext4_fsblk_t goal,
626 int indirect_blks, int blks,
627 ext4_fsblk_t new_blocks[4], int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700628{
Theodore Ts'o815a1132009-01-01 23:59:43 -0500629 struct ext4_allocation_request ar;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700630 int target, i;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400631 unsigned long count = 0, blk_allocated = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700632 int index = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700633 ext4_fsblk_t current_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700634 int ret = 0;
635
636 /*
637 * Here we try to allocate the requested multiple blocks at once,
638 * on a best-effort basis.
639 * To build a branch, we should allocate blocks for
640 * the indirect blocks(if not allocated yet), and at least
641 * the first direct block of this branch. That's the
642 * minimum number of blocks need to allocate(required)
643 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400644 /* first we try to allocate the indirect blocks */
645 target = indirect_blks;
646 while (target > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700647 count = target;
648 /* allocating blocks for indirect blocks and direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400649 current_block = ext4_new_meta_blocks(handle, inode,
650 goal, &count, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700651 if (*err)
652 goto failed_out;
653
654 target -= count;
655 /* allocate blocks for indirect blocks */
656 while (index < indirect_blks && count) {
657 new_blocks[index++] = current_block++;
658 count--;
659 }
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400660 if (count > 0) {
661 /*
662 * save the new block number
663 * for the first direct block
664 */
665 new_blocks[index] = current_block;
666 printk(KERN_INFO "%s returned more blocks than "
667 "requested\n", __func__);
668 WARN_ON(1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700669 break;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400670 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700671 }
672
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400673 target = blks - count ;
674 blk_allocated = count;
675 if (!target)
676 goto allocated;
677 /* Now allocate data blocks */
Theodore Ts'o815a1132009-01-01 23:59:43 -0500678 memset(&ar, 0, sizeof(ar));
679 ar.inode = inode;
680 ar.goal = goal;
681 ar.len = target;
682 ar.logical = iblock;
683 if (S_ISREG(inode->i_mode))
684 /* enable in-core preallocation only for regular files */
685 ar.flags = EXT4_MB_HINT_DATA;
686
687 current_block = ext4_mb_new_blocks(handle, &ar, err);
688
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400689 if (*err && (target == blks)) {
690 /*
691 * if the allocation failed and we didn't allocate
692 * any blocks before
693 */
694 goto failed_out;
695 }
696 if (!*err) {
697 if (target == blks) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400698 /*
699 * save the new block number
700 * for the first direct block
701 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400702 new_blocks[index] = current_block;
703 }
Theodore Ts'o815a1132009-01-01 23:59:43 -0500704 blk_allocated += ar.len;
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400705 }
706allocated:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700707 /* total number of blocks allocated for direct blocks */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400708 ret = blk_allocated;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700709 *err = 0;
710 return ret;
711failed_out:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400712 for (i = 0; i < index; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500713 ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700714 return ret;
715}
716
717/**
Mingming Cao617ba132006-10-11 01:20:53 -0700718 * ext4_alloc_branch - allocate and set up a chain of blocks.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700719 * @inode: owner
720 * @indirect_blks: number of allocated indirect blocks
721 * @blks: number of allocated direct blocks
722 * @offsets: offsets (in the blocks) to store the pointers to next.
723 * @branch: place to store the chain in.
724 *
725 * This function allocates blocks, zeroes out all but the last one,
726 * links them into chain and (if we are synchronous) writes them to disk.
727 * In other words, it prepares a branch that can be spliced onto the
728 * inode. It stores the information about that chain in the branch[], in
Mingming Cao617ba132006-10-11 01:20:53 -0700729 * the same format as ext4_get_branch() would do. We are calling it after
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700730 * we had read the existing part of chain and partial points to the last
731 * triple of that (one with zero ->key). Upon the exit we have the same
Mingming Cao617ba132006-10-11 01:20:53 -0700732 * picture as after the successful ext4_get_block(), except that in one
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700733 * place chain is disconnected - *branch->p is still zero (we did not
734 * set the last link), but branch->key contains the number that should
735 * be placed into *branch->p to fill that gap.
736 *
737 * If allocation fails we free all blocks we've allocated (and forget
738 * their buffer_heads) and return the error value the from failed
Mingming Cao617ba132006-10-11 01:20:53 -0700739 * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700740 * as described above and return 0.
741 */
Mingming Cao617ba132006-10-11 01:20:53 -0700742static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400743 ext4_lblk_t iblock, int indirect_blks,
744 int *blks, ext4_fsblk_t goal,
745 ext4_lblk_t *offsets, Indirect *branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700746{
747 int blocksize = inode->i_sb->s_blocksize;
748 int i, n = 0;
749 int err = 0;
750 struct buffer_head *bh;
751 int num;
Mingming Cao617ba132006-10-11 01:20:53 -0700752 ext4_fsblk_t new_blocks[4];
753 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700754
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400755 num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700756 *blks, new_blocks, &err);
757 if (err)
758 return err;
759
760 branch[0].key = cpu_to_le32(new_blocks[0]);
761 /*
762 * metadata blocks and data blocks are allocated.
763 */
764 for (n = 1; n <= indirect_blks; n++) {
765 /*
766 * Get buffer_head for parent block, zero it out
767 * and set the pointer to new one, then send
768 * parent to disk.
769 */
770 bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
771 branch[n].bh = bh;
772 lock_buffer(bh);
773 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700774 err = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700775 if (err) {
Curt Wohlgemuth6487a9d2009-07-17 10:54:08 -0400776 /* Don't brelse(bh) here; it's done in
777 * ext4_journal_forget() below */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700778 unlock_buffer(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700779 goto failed;
780 }
781
782 memset(bh->b_data, 0, blocksize);
783 branch[n].p = (__le32 *) bh->b_data + offsets[n];
784 branch[n].key = cpu_to_le32(new_blocks[n]);
785 *branch[n].p = branch[n].key;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400786 if (n == indirect_blks) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700787 current_block = new_blocks[n];
788 /*
789 * End of chain, update the last new metablock of
790 * the chain to point to the new allocated
791 * data blocks numbers
792 */
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400793 for (i = 1; i < num; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700794 *(branch[n].p + i) = cpu_to_le32(++current_block);
795 }
796 BUFFER_TRACE(bh, "marking uptodate");
797 set_buffer_uptodate(bh);
798 unlock_buffer(bh);
799
Frank Mayhar03901312009-01-07 00:06:22 -0500800 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
801 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700802 if (err)
803 goto failed;
804 }
805 *blks = num;
806 return err;
807failed:
808 /* Allocation failed, free what we already allocated */
809 for (i = 1; i <= n ; i++) {
Mingming Caodab291a2006-10-11 01:21:01 -0700810 BUFFER_TRACE(branch[i].bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700811 ext4_journal_forget(handle, branch[i].bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700812 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400813 for (i = 0; i < indirect_blks; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500814 ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700815
Alex Tomasc9de5602008-01-29 00:19:52 -0500816 ext4_free_blocks(handle, inode, new_blocks[i], num, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700817
818 return err;
819}
820
821/**
Mingming Cao617ba132006-10-11 01:20:53 -0700822 * ext4_splice_branch - splice the allocated branch onto inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700823 * @inode: owner
824 * @block: (logical) number of block we are adding
825 * @chain: chain of indirect blocks (with a missing link - see
Mingming Cao617ba132006-10-11 01:20:53 -0700826 * ext4_alloc_branch)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700827 * @where: location of missing link
828 * @num: number of indirect blocks we are adding
829 * @blks: number of direct blocks we are adding
830 *
831 * This function fills the missing link and does all housekeeping needed in
832 * inode (->i_blocks, etc.). In case of success we end up with the full
833 * chain to new block and return 0.
834 */
Mingming Cao617ba132006-10-11 01:20:53 -0700835static int ext4_splice_branch(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400836 ext4_lblk_t block, Indirect *where, int num,
837 int blks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700838{
839 int i;
840 int err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700841 ext4_fsblk_t current_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700842
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700843 /*
844 * If we're splicing into a [td]indirect block (as opposed to the
845 * inode) then we need to get write access to the [td]indirect block
846 * before the splice.
847 */
848 if (where->bh) {
849 BUFFER_TRACE(where->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -0700850 err = ext4_journal_get_write_access(handle, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700851 if (err)
852 goto err_out;
853 }
854 /* That's it */
855
856 *where->p = where->key;
857
858 /*
859 * Update the host buffer_head or inode to point to more just allocated
860 * direct blocks blocks
861 */
862 if (num == 0 && blks > 1) {
863 current_block = le32_to_cpu(where->key) + 1;
864 for (i = 1; i < blks; i++)
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400865 *(where->p + i) = cpu_to_le32(current_block++);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700866 }
867
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700868 /* We are done with atomic stuff, now do the rest of housekeeping */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700869 /* had we spliced it onto indirect block? */
870 if (where->bh) {
871 /*
872 * If we spliced it onto an indirect block, we haven't
873 * altered the inode. Note however that if it is being spliced
874 * onto an indirect block at the very end of the file (the
875 * file is growing) then we *will* alter the inode to reflect
876 * the new i_size. But that is not done here - it is done in
Mingming Cao617ba132006-10-11 01:20:53 -0700877 * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700878 */
879 jbd_debug(5, "splicing indirect only\n");
Frank Mayhar03901312009-01-07 00:06:22 -0500880 BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata");
881 err = ext4_handle_dirty_metadata(handle, inode, where->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700882 if (err)
883 goto err_out;
884 } else {
885 /*
886 * OK, we spliced it into the inode itself on a direct block.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700887 */
Theodore Ts'o41591752009-06-15 03:41:23 -0400888 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700889 jbd_debug(5, "splicing direct\n");
890 }
891 return err;
892
893err_out:
894 for (i = 1; i <= num; i++) {
Mingming Caodab291a2006-10-11 01:21:01 -0700895 BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget");
Mingming Cao617ba132006-10-11 01:20:53 -0700896 ext4_journal_forget(handle, where[i].bh);
Alex Tomasc9de5602008-01-29 00:19:52 -0500897 ext4_free_blocks(handle, inode,
898 le32_to_cpu(where[i-1].key), 1, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700899 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500900 ext4_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700901
902 return err;
903}
904
905/*
Theodore Ts'ob920c752009-05-14 00:54:29 -0400906 * The ext4_ind_get_blocks() function handles non-extents inodes
907 * (i.e., using the traditional indirect/double-indirect i_blocks
908 * scheme) for ext4_get_blocks().
909 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700910 * Allocation strategy is simple: if we have to allocate something, we will
911 * have to go the whole way to leaf. So let's do it before attaching anything
912 * to tree, set linkage between the newborn blocks, write them if sync is
913 * required, recheck the path, free and repeat if check fails, otherwise
914 * set the last missing link (that will protect us from any truncate-generated
915 * removals - all blocks on the path are immune now) and possibly force the
916 * write on the parent block.
917 * That has a nice additional property: no special recovery from the failed
918 * allocations is needed - we simply release blocks and do not touch anything
919 * reachable from inode.
920 *
921 * `handle' can be NULL if create == 0.
922 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700923 * return > 0, # of blocks mapped or allocated.
924 * return = 0, if plain lookup failed.
925 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500926 *
Theodore Ts'ob920c752009-05-14 00:54:29 -0400927 * The ext4_ind_get_blocks() function should be called with
928 * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem
929 * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or
930 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system
931 * blocks.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700932 */
Theodore Ts'oe4d996c2009-05-12 00:25:28 -0400933static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400934 ext4_lblk_t iblock, unsigned int maxblocks,
935 struct buffer_head *bh_result,
936 int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700937{
938 int err = -EIO;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500939 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700940 Indirect chain[4];
941 Indirect *partial;
Mingming Cao617ba132006-10-11 01:20:53 -0700942 ext4_fsblk_t goal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700943 int indirect_blks;
944 int blocks_to_boundary = 0;
945 int depth;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700946 int count = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700947 ext4_fsblk_t first_block = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700948
Alex Tomasa86c6182006-10-11 01:21:03 -0700949 J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL));
Theodore Ts'oc2177052009-05-14 00:58:52 -0400950 J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500951 depth = ext4_block_to_path(inode, iblock, offsets,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400952 &blocks_to_boundary);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700953
954 if (depth == 0)
955 goto out;
956
Mingming Cao617ba132006-10-11 01:20:53 -0700957 partial = ext4_get_branch(inode, depth, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700958
959 /* Simplest case - block found, no allocation needed */
960 if (!partial) {
961 first_block = le32_to_cpu(chain[depth - 1].key);
962 clear_buffer_new(bh_result);
963 count++;
964 /*map more blocks*/
965 while (count < maxblocks && count <= blocks_to_boundary) {
Mingming Cao617ba132006-10-11 01:20:53 -0700966 ext4_fsblk_t blk;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700967
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700968 blk = le32_to_cpu(*(chain[depth-1].p + count));
969
970 if (blk == first_block + count)
971 count++;
972 else
973 break;
974 }
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -0500975 goto got_it;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700976 }
977
978 /* Next simple case - plain lookup or failed read of indirect block */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400979 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700980 goto cleanup;
981
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700982 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400983 * Okay, we need to do block allocation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700984 */
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800985 goal = ext4_find_goal(inode, iblock, partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700986
987 /* the number of blocks need to allocate for [d,t]indirect blocks */
988 indirect_blks = (chain + depth) - partial - 1;
989
990 /*
991 * Next look up the indirect map to count the totoal number of
992 * direct blocks to allocate for this branch.
993 */
Mingming Cao617ba132006-10-11 01:20:53 -0700994 count = ext4_blks_to_allocate(partial, indirect_blks,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700995 maxblocks, blocks_to_boundary);
996 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700997 * Block out ext4_truncate while we alter the tree
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700998 */
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400999 err = ext4_alloc_branch(handle, inode, iblock, indirect_blks,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001000 &count, goal,
1001 offsets + (partial - chain), partial);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001002
1003 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001004 * The ext4_splice_branch call will free and forget any buffers
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001005 * on the new chain if there is a failure, but that risks using
1006 * up transaction credits, especially for bitmaps where the
1007 * credits cannot be returned. Can we handle this somehow? We
1008 * may need to return -EAGAIN upwards in the worst case. --sct
1009 */
1010 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07001011 err = ext4_splice_branch(handle, inode, iblock,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001012 partial, indirect_blks, count);
1013 else
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001014 goto cleanup;
1015
1016 set_buffer_new(bh_result);
1017got_it:
1018 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
1019 if (count > blocks_to_boundary)
1020 set_buffer_boundary(bh_result);
1021 err = count;
1022 /* Clean up and exit */
1023 partial = chain + depth - 1; /* the whole chain */
1024cleanup:
1025 while (partial > chain) {
1026 BUFFER_TRACE(partial->bh, "call brelse");
1027 brelse(partial->bh);
1028 partial--;
1029 }
1030 BUFFER_TRACE(bh_result, "returned");
1031out:
1032 return err;
1033}
1034
Mingming Cao60e58e02009-01-22 18:13:05 +01001035qsize_t ext4_get_reserved_space(struct inode *inode)
1036{
1037 unsigned long long total;
1038
1039 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1040 total = EXT4_I(inode)->i_reserved_data_blocks +
1041 EXT4_I(inode)->i_reserved_meta_blocks;
1042 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1043
1044 return total;
1045}
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001046/*
1047 * Calculate the number of metadata blocks need to reserve
1048 * to allocate @blocks for non extent file based file
1049 */
1050static int ext4_indirect_calc_metadata_amount(struct inode *inode, int blocks)
1051{
1052 int icap = EXT4_ADDR_PER_BLOCK(inode->i_sb);
1053 int ind_blks, dind_blks, tind_blks;
1054
1055 /* number of new indirect blocks needed */
1056 ind_blks = (blocks + icap - 1) / icap;
1057
1058 dind_blks = (ind_blks + icap - 1) / icap;
1059
1060 tind_blks = 1;
1061
1062 return ind_blks + dind_blks + tind_blks;
1063}
1064
1065/*
1066 * Calculate the number of metadata blocks need to reserve
1067 * to allocate given number of blocks
1068 */
1069static int ext4_calc_metadata_amount(struct inode *inode, int blocks)
1070{
Mingming Caocd213222008-08-19 22:16:59 -04001071 if (!blocks)
1072 return 0;
1073
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001074 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)
1075 return ext4_ext_calc_metadata_amount(inode, blocks);
1076
1077 return ext4_indirect_calc_metadata_amount(inode, blocks);
1078}
1079
1080static void ext4_da_update_reserve_space(struct inode *inode, int used)
1081{
1082 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1083 int total, mdb, mdb_free;
1084
1085 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1086 /* recalculate the number of metablocks still need to be reserved */
1087 total = EXT4_I(inode)->i_reserved_data_blocks - used;
1088 mdb = ext4_calc_metadata_amount(inode, total);
1089
1090 /* figure out how many metablocks to release */
1091 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1092 mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
1093
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001094 if (mdb_free) {
1095 /* Account for allocated meta_blocks */
1096 mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001097
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001098 /* update fs dirty blocks counter */
1099 percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free);
1100 EXT4_I(inode)->i_allocated_meta_blocks = 0;
1101 EXT4_I(inode)->i_reserved_meta_blocks = mdb;
1102 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001103
1104 /* update per-inode reservations */
1105 BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks);
1106 EXT4_I(inode)->i_reserved_data_blocks -= used;
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001107 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001108
1109 /*
1110 * free those over-booking quota for metadata blocks
1111 */
Mingming Cao60e58e02009-01-22 18:13:05 +01001112 if (mdb_free)
1113 vfs_dq_release_reservation_block(inode, mdb_free);
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -04001114
1115 /*
1116 * If we have done all the pending block allocations and if
1117 * there aren't any writers on the inode, we can discard the
1118 * inode's preallocations.
1119 */
1120 if (!total && (atomic_read(&inode->i_writecount) == 0))
1121 ext4_discard_preallocations(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001122}
1123
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001124static int check_block_validity(struct inode *inode, sector_t logical,
1125 sector_t phys, int len)
1126{
1127 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), phys, len)) {
1128 ext4_error(inode->i_sb, "check_block_validity",
1129 "inode #%lu logical block %llu mapped to %llu "
1130 "(size %d)", inode->i_ino,
1131 (unsigned long long) logical,
1132 (unsigned long long) phys, len);
1133 WARN_ON(1);
1134 return -EIO;
1135 }
1136 return 0;
1137}
1138
Mingming Caof5ab0d12008-02-25 15:29:55 -05001139/*
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001140 * The ext4_get_blocks() function tries to look up the requested blocks,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001141 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -05001142 *
Mingming Caof5ab0d12008-02-25 15:29:55 -05001143 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
1144 * and store the allocated blocks in the result buffer head and mark it
1145 * mapped.
1146 *
1147 * If file type is extents based, it will call ext4_ext_get_blocks(),
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001148 * Otherwise, call with ext4_ind_get_blocks() to handle indirect mapping
Mingming Caof5ab0d12008-02-25 15:29:55 -05001149 * based files
1150 *
1151 * On success, it returns the number of blocks being mapped or allocate.
1152 * if create==0 and the blocks are pre-allocated and uninitialized block,
1153 * the result buffer head is unmapped. If the create ==1, it will make sure
1154 * the buffer head is mapped.
1155 *
1156 * It returns 0 if plain look up failed (blocks have not been allocated), in
1157 * that casem, buffer head is unmapped
1158 *
1159 * It returns the error in case of allocation failure.
1160 */
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001161int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block,
1162 unsigned int max_blocks, struct buffer_head *bh,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001163 int flags)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001164{
1165 int retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -05001166
1167 clear_buffer_mapped(bh);
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -04001168 clear_buffer_unwritten(bh);
Mingming Caof5ab0d12008-02-25 15:29:55 -05001169
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001170 /*
Theodore Ts'ob920c752009-05-14 00:54:29 -04001171 * Try to see if we can get the block without requesting a new
1172 * file system block.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001173 */
1174 down_read((&EXT4_I(inode)->i_data_sem));
1175 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1176 retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001177 bh, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001178 } else {
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001179 retval = ext4_ind_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001180 bh, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001181 }
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001182 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -05001183
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001184 if (retval > 0 && buffer_mapped(bh)) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001185 int ret = check_block_validity(inode, block,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001186 bh->b_blocknr, retval);
1187 if (ret != 0)
1188 return ret;
1189 }
1190
Mingming Caof5ab0d12008-02-25 15:29:55 -05001191 /* If it is only a block(s) look up */
Theodore Ts'oc2177052009-05-14 00:58:52 -04001192 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001193 return retval;
1194
1195 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001196 * Returns if the blocks have already allocated
1197 *
1198 * Note that if blocks have been preallocated
1199 * ext4_ext_get_block() returns th create = 0
1200 * with buffer head unmapped.
1201 */
1202 if (retval > 0 && buffer_mapped(bh))
1203 return retval;
1204
1205 /*
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -04001206 * When we call get_blocks without the create flag, the
1207 * BH_Unwritten flag could have gotten set if the blocks
1208 * requested were part of a uninitialized extent. We need to
1209 * clear this flag now that we are committed to convert all or
1210 * part of the uninitialized extent to be an initialized
1211 * extent. This is because we need to avoid the combination
1212 * of BH_Unwritten and BH_Mapped flags being simultaneously
1213 * set on the buffer_head.
1214 */
1215 clear_buffer_unwritten(bh);
1216
1217 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -05001218 * New blocks allocate and/or writing to uninitialized extent
1219 * will possibly result in updating i_data, so we take
1220 * the write lock of i_data_sem, and call get_blocks()
1221 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001222 */
1223 down_write((&EXT4_I(inode)->i_data_sem));
Mingming Caod2a17632008-07-14 17:52:37 -04001224
1225 /*
1226 * if the caller is from delayed allocation writeout path
1227 * we have already reserved fs blocks for allocation
1228 * let the underlying get_block() function know to
1229 * avoid double accounting
1230 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04001231 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Mingming Caod2a17632008-07-14 17:52:37 -04001232 EXT4_I(inode)->i_delalloc_reserved_flag = 1;
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001233 /*
1234 * We need to check for EXT4 here because migrate
1235 * could have changed the inode type in between
1236 */
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001237 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
1238 retval = ext4_ext_get_blocks(handle, inode, block, max_blocks,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001239 bh, flags);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001240 } else {
Theodore Ts'oe4d996c2009-05-12 00:25:28 -04001241 retval = ext4_ind_get_blocks(handle, inode, block,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001242 max_blocks, bh, flags);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04001243
1244 if (retval > 0 && buffer_new(bh)) {
1245 /*
1246 * We allocated new blocks which will result in
1247 * i_data's format changing. Force the migrate
1248 * to fail by clearing migrate flags
1249 */
1250 EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags &
1251 ~EXT4_EXT_MIGRATE;
1252 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001253 }
Mingming Caod2a17632008-07-14 17:52:37 -04001254
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001255 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Mingming Caod2a17632008-07-14 17:52:37 -04001256 EXT4_I(inode)->i_delalloc_reserved_flag = 0;
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001257
1258 /*
1259 * Update reserved blocks/metadata blocks after successful
1260 * block allocation which had been deferred till now.
1261 */
1262 if ((retval > 0) && (flags & EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE))
1263 ext4_da_update_reserve_space(inode, retval);
Mingming Caod2a17632008-07-14 17:52:37 -04001264
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -05001265 up_write((&EXT4_I(inode)->i_data_sem));
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001266 if (retval > 0 && buffer_mapped(bh)) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001267 int ret = check_block_validity(inode, block,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001268 bh->b_blocknr, retval);
1269 if (ret != 0)
1270 return ret;
1271 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001272 return retval;
1273}
1274
Mingming Caof3bd1f32008-08-19 22:16:03 -04001275/* Maximum number of blocks we map for direct IO at once. */
1276#define DIO_MAX_BLOCKS 4096
1277
Eric Sandeen6873fa02008-10-07 00:46:36 -04001278int ext4_get_block(struct inode *inode, sector_t iblock,
1279 struct buffer_head *bh_result, int create)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001280{
Dmitriy Monakhov3e4fdaf2007-02-10 01:46:35 -08001281 handle_t *handle = ext4_journal_current_handle();
Jan Kara7fb54092008-02-10 01:08:38 -05001282 int ret = 0, started = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001283 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001284 int dio_credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001285
Jan Kara7fb54092008-02-10 01:08:38 -05001286 if (create && !handle) {
1287 /* Direct IO write... */
1288 if (max_blocks > DIO_MAX_BLOCKS)
1289 max_blocks = DIO_MAX_BLOCKS;
Mingming Caof3bd1f32008-08-19 22:16:03 -04001290 dio_credits = ext4_chunk_trans_blocks(inode, max_blocks);
1291 handle = ext4_journal_start(inode, dio_credits);
Jan Kara7fb54092008-02-10 01:08:38 -05001292 if (IS_ERR(handle)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001293 ret = PTR_ERR(handle);
Jan Kara7fb54092008-02-10 01:08:38 -05001294 goto out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001295 }
Jan Kara7fb54092008-02-10 01:08:38 -05001296 started = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001297 }
1298
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04001299 ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result,
Theodore Ts'oc2177052009-05-14 00:58:52 -04001300 create ? EXT4_GET_BLOCKS_CREATE : 0);
Jan Kara7fb54092008-02-10 01:08:38 -05001301 if (ret > 0) {
1302 bh_result->b_size = (ret << inode->i_blkbits);
1303 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001304 }
Jan Kara7fb54092008-02-10 01:08:38 -05001305 if (started)
1306 ext4_journal_stop(handle);
1307out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001308 return ret;
1309}
1310
1311/*
1312 * `handle' can be NULL if create is zero
1313 */
Mingming Cao617ba132006-10-11 01:20:53 -07001314struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001315 ext4_lblk_t block, int create, int *errp)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001316{
1317 struct buffer_head dummy;
1318 int fatal = 0, err;
Jan Kara03f5d8b2009-06-09 00:17:05 -04001319 int flags = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001320
1321 J_ASSERT(handle != NULL || create == 0);
1322
1323 dummy.b_state = 0;
1324 dummy.b_blocknr = -1000;
1325 buffer_trace_init(&dummy.b_history);
Theodore Ts'oc2177052009-05-14 00:58:52 -04001326 if (create)
1327 flags |= EXT4_GET_BLOCKS_CREATE;
1328 err = ext4_get_blocks(handle, inode, block, 1, &dummy, flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001329 /*
Theodore Ts'oc2177052009-05-14 00:58:52 -04001330 * ext4_get_blocks() returns number of blocks mapped. 0 in
1331 * case of a HOLE.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001332 */
1333 if (err > 0) {
1334 if (err > 1)
1335 WARN_ON(1);
1336 err = 0;
1337 }
1338 *errp = err;
1339 if (!err && buffer_mapped(&dummy)) {
1340 struct buffer_head *bh;
1341 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
1342 if (!bh) {
1343 *errp = -EIO;
1344 goto err;
1345 }
1346 if (buffer_new(&dummy)) {
1347 J_ASSERT(create != 0);
Aneesh Kumar K.Vac398492007-10-16 18:38:25 -04001348 J_ASSERT(handle != NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001349
1350 /*
1351 * Now that we do not always journal data, we should
1352 * keep in mind whether this should always journal the
1353 * new buffer as metadata. For now, regular file
Mingming Cao617ba132006-10-11 01:20:53 -07001354 * writes use ext4_get_block instead, so it's not a
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001355 * problem.
1356 */
1357 lock_buffer(bh);
1358 BUFFER_TRACE(bh, "call get_create_access");
Mingming Cao617ba132006-10-11 01:20:53 -07001359 fatal = ext4_journal_get_create_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001360 if (!fatal && !buffer_uptodate(bh)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001361 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001362 set_buffer_uptodate(bh);
1363 }
1364 unlock_buffer(bh);
Frank Mayhar03901312009-01-07 00:06:22 -05001365 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
1366 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001367 if (!fatal)
1368 fatal = err;
1369 } else {
1370 BUFFER_TRACE(bh, "not a new buffer");
1371 }
1372 if (fatal) {
1373 *errp = fatal;
1374 brelse(bh);
1375 bh = NULL;
1376 }
1377 return bh;
1378 }
1379err:
1380 return NULL;
1381}
1382
Mingming Cao617ba132006-10-11 01:20:53 -07001383struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001384 ext4_lblk_t block, int create, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001385{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001386 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001387
Mingming Cao617ba132006-10-11 01:20:53 -07001388 bh = ext4_getblk(handle, inode, block, create, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001389 if (!bh)
1390 return bh;
1391 if (buffer_uptodate(bh))
1392 return bh;
1393 ll_rw_block(READ_META, 1, &bh);
1394 wait_on_buffer(bh);
1395 if (buffer_uptodate(bh))
1396 return bh;
1397 put_bh(bh);
1398 *err = -EIO;
1399 return NULL;
1400}
1401
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001402static int walk_page_buffers(handle_t *handle,
1403 struct buffer_head *head,
1404 unsigned from,
1405 unsigned to,
1406 int *partial,
1407 int (*fn)(handle_t *handle,
1408 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001409{
1410 struct buffer_head *bh;
1411 unsigned block_start, block_end;
1412 unsigned blocksize = head->b_size;
1413 int err, ret = 0;
1414 struct buffer_head *next;
1415
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001416 for (bh = head, block_start = 0;
1417 ret == 0 && (bh != head || !block_start);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001418 block_start = block_end, bh = next) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001419 next = bh->b_this_page;
1420 block_end = block_start + blocksize;
1421 if (block_end <= from || block_start >= to) {
1422 if (partial && !buffer_uptodate(bh))
1423 *partial = 1;
1424 continue;
1425 }
1426 err = (*fn)(handle, bh);
1427 if (!ret)
1428 ret = err;
1429 }
1430 return ret;
1431}
1432
1433/*
1434 * To preserve ordering, it is essential that the hole instantiation and
1435 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -07001436 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -07001437 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001438 * prepare_write() is the right place.
1439 *
Mingming Cao617ba132006-10-11 01:20:53 -07001440 * Also, this function can nest inside ext4_writepage() ->
1441 * block_write_full_page(). In that case, we *know* that ext4_writepage()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001442 * has generated enough buffer credits to do the whole page. So we won't
1443 * block on the journal in that case, which is good, because the caller may
1444 * be PF_MEMALLOC.
1445 *
Mingming Cao617ba132006-10-11 01:20:53 -07001446 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001447 * quota file writes. If we were to commit the transaction while thus
1448 * reentered, there can be a deadlock - we would be holding a quota
1449 * lock, and the commit would never complete if another thread had a
1450 * transaction open and was blocking on the quota lock - a ranking
1451 * violation.
1452 *
Mingming Caodab291a2006-10-11 01:21:01 -07001453 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001454 * will _not_ run commit under these circumstances because handle->h_ref
1455 * is elevated. We'll still have enough credits for the tiny quotafile
1456 * write.
1457 */
1458static int do_journal_get_write_access(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001459 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001460{
1461 if (!buffer_mapped(bh) || buffer_freed(bh))
1462 return 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001463 return ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001464}
1465
Nick Pigginbfc1af62007-10-16 01:25:05 -07001466static int ext4_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001467 loff_t pos, unsigned len, unsigned flags,
1468 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001469{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001470 struct inode *inode = mapping->host;
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001471 int ret, needed_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001472 handle_t *handle;
1473 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001474 struct page *page;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001475 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001476 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001477
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001478 trace_ext4_write_begin(inode, pos, len, flags);
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001479 /*
1480 * Reserve one block more for addition to orphan list in case
1481 * we allocate blocks but write fails for some reason
1482 */
1483 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001484 index = pos >> PAGE_CACHE_SHIFT;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001485 from = pos & (PAGE_CACHE_SIZE - 1);
1486 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001487
1488retry:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001489 handle = ext4_journal_start(inode, needed_blocks);
1490 if (IS_ERR(handle)) {
1491 ret = PTR_ERR(handle);
1492 goto out;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001493 }
1494
Jan Karaebd36102009-02-22 21:09:59 -05001495 /* We cannot recurse into the filesystem as the transaction is already
1496 * started */
1497 flags |= AOP_FLAG_NOFS;
1498
Nick Piggin54566b22009-01-04 12:00:53 -08001499 page = grab_cache_page_write_begin(mapping, index, flags);
Jan Karacf108bc2008-07-11 19:27:31 -04001500 if (!page) {
1501 ext4_journal_stop(handle);
1502 ret = -ENOMEM;
1503 goto out;
1504 }
1505 *pagep = page;
1506
Nick Pigginbfc1af62007-10-16 01:25:05 -07001507 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
Jan Karaebd36102009-02-22 21:09:59 -05001508 ext4_get_block);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001509
1510 if (!ret && ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001511 ret = walk_page_buffers(handle, page_buffers(page),
1512 from, to, NULL, do_journal_get_write_access);
1513 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001514
1515 if (ret) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001516 unlock_page(page);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001517 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001518 /*
1519 * block_write_begin may have instantiated a few blocks
1520 * outside i_size. Trim these off again. Don't need
1521 * i_size_read because we hold i_mutex.
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001522 *
1523 * Add inode to orphan list in case we crash before
1524 * truncate finishes
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001525 */
Jan Karaffacfa72009-07-13 16:22:22 -04001526 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001527 ext4_orphan_add(handle, inode);
1528
1529 ext4_journal_stop(handle);
1530 if (pos + len > inode->i_size) {
Jan Karaffacfa72009-07-13 16:22:22 -04001531 ext4_truncate(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001532 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001533 * If truncate failed early the inode might
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001534 * still be on the orphan list; we need to
1535 * make sure the inode is removed from the
1536 * orphan list in that case.
1537 */
1538 if (inode->i_nlink)
1539 ext4_orphan_del(NULL, inode);
1540 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001541 }
1542
Mingming Cao617ba132006-10-11 01:20:53 -07001543 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001544 goto retry;
Andrew Morton7479d2b2007-04-01 23:49:44 -07001545out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001546 return ret;
1547}
1548
Nick Pigginbfc1af62007-10-16 01:25:05 -07001549/* For write_end() in data=journal mode */
1550static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001551{
1552 if (!buffer_mapped(bh) || buffer_freed(bh))
1553 return 0;
1554 set_buffer_uptodate(bh);
Frank Mayhar03901312009-01-07 00:06:22 -05001555 return ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001556}
1557
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001558static int ext4_generic_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001559 struct address_space *mapping,
1560 loff_t pos, unsigned len, unsigned copied,
1561 struct page *page, void *fsdata)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001562{
1563 int i_size_changed = 0;
1564 struct inode *inode = mapping->host;
1565 handle_t *handle = ext4_journal_current_handle();
1566
1567 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
1568
1569 /*
1570 * No need to use i_size_read() here, the i_size
1571 * cannot change under us because we hold i_mutex.
1572 *
1573 * But it's important to update i_size while still holding page lock:
1574 * page writeout could otherwise come in and zero beyond i_size.
1575 */
1576 if (pos + copied > inode->i_size) {
1577 i_size_write(inode, pos + copied);
1578 i_size_changed = 1;
1579 }
1580
1581 if (pos + copied > EXT4_I(inode)->i_disksize) {
1582 /* We need to mark inode dirty even if
1583 * new_i_size is less that inode->i_size
1584 * bu greater than i_disksize.(hint delalloc)
1585 */
1586 ext4_update_i_disksize(inode, (pos + copied));
1587 i_size_changed = 1;
1588 }
1589 unlock_page(page);
1590 page_cache_release(page);
1591
1592 /*
1593 * Don't mark the inode dirty under page lock. First, it unnecessarily
1594 * makes the holding time of page lock longer. Second, it forces lock
1595 * ordering of page lock and transaction start for journaling
1596 * filesystems.
1597 */
1598 if (i_size_changed)
1599 ext4_mark_inode_dirty(handle, inode);
1600
1601 return copied;
1602}
1603
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001604/*
1605 * We need to pick up the new inode size which generic_commit_write gave us
1606 * `file' can be NULL - eg, when called from page_symlink().
1607 *
Mingming Cao617ba132006-10-11 01:20:53 -07001608 * ext4 never places buffers on inode->i_mapping->private_list. metadata
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001609 * buffers are managed internally.
1610 */
Nick Pigginbfc1af62007-10-16 01:25:05 -07001611static int ext4_ordered_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001612 struct address_space *mapping,
1613 loff_t pos, unsigned len, unsigned copied,
1614 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001615{
Mingming Cao617ba132006-10-11 01:20:53 -07001616 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001617 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001618 int ret = 0, ret2;
1619
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001620 trace_ext4_ordered_write_end(inode, pos, len, copied);
Jan Kara678aaf42008-07-11 19:27:31 -04001621 ret = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001622
1623 if (ret == 0) {
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001624 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001625 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001626 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -04001627 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001628 /* if we have allocated more blocks and copied
1629 * less. We will have blocks allocated outside
1630 * inode->i_size. So truncate them
1631 */
1632 ext4_orphan_add(handle, inode);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001633 if (ret2 < 0)
1634 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001635 }
Mingming Cao617ba132006-10-11 01:20:53 -07001636 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001637 if (!ret)
1638 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001639
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001640 if (pos + len > inode->i_size) {
Jan Karaffacfa72009-07-13 16:22:22 -04001641 ext4_truncate(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001642 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001643 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001644 * on the orphan list; we need to make sure the inode
1645 * is removed from the orphan list in that case.
1646 */
1647 if (inode->i_nlink)
1648 ext4_orphan_del(NULL, inode);
1649 }
1650
1651
Nick Pigginbfc1af62007-10-16 01:25:05 -07001652 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001653}
1654
Nick Pigginbfc1af62007-10-16 01:25:05 -07001655static int ext4_writeback_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001656 struct address_space *mapping,
1657 loff_t pos, unsigned len, unsigned copied,
1658 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001659{
Mingming Cao617ba132006-10-11 01:20:53 -07001660 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001661 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001662 int ret = 0, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001663
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001664 trace_ext4_writeback_write_end(inode, pos, len, copied);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001665 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001666 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001667 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -04001668 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001669 /* if we have allocated more blocks and copied
1670 * less. We will have blocks allocated outside
1671 * inode->i_size. So truncate them
1672 */
1673 ext4_orphan_add(handle, inode);
1674
Roel Kluinf8a87d82008-04-29 22:01:18 -04001675 if (ret2 < 0)
1676 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001677
Mingming Cao617ba132006-10-11 01:20:53 -07001678 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001679 if (!ret)
1680 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001681
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001682 if (pos + len > inode->i_size) {
Jan Karaffacfa72009-07-13 16:22:22 -04001683 ext4_truncate(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001684 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001685 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001686 * on the orphan list; we need to make sure the inode
1687 * is removed from the orphan list in that case.
1688 */
1689 if (inode->i_nlink)
1690 ext4_orphan_del(NULL, inode);
1691 }
1692
Nick Pigginbfc1af62007-10-16 01:25:05 -07001693 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001694}
1695
Nick Pigginbfc1af62007-10-16 01:25:05 -07001696static int ext4_journalled_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001697 struct address_space *mapping,
1698 loff_t pos, unsigned len, unsigned copied,
1699 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001700{
Mingming Cao617ba132006-10-11 01:20:53 -07001701 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001702 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001703 int ret = 0, ret2;
1704 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001705 unsigned from, to;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001706 loff_t new_i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001707
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001708 trace_ext4_journalled_write_end(inode, pos, len, copied);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001709 from = pos & (PAGE_CACHE_SIZE - 1);
1710 to = from + len;
1711
1712 if (copied < len) {
1713 if (!PageUptodate(page))
1714 copied = 0;
1715 page_zero_new_buffers(page, from+copied, to);
1716 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001717
1718 ret = walk_page_buffers(handle, page_buffers(page), from,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001719 to, &partial, write_end_fn);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001720 if (!partial)
1721 SetPageUptodate(page);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001722 new_i_size = pos + copied;
1723 if (new_i_size > inode->i_size)
Nick Pigginbfc1af62007-10-16 01:25:05 -07001724 i_size_write(inode, pos+copied);
Mingming Cao617ba132006-10-11 01:20:53 -07001725 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001726 if (new_i_size > EXT4_I(inode)->i_disksize) {
1727 ext4_update_i_disksize(inode, new_i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001728 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001729 if (!ret)
1730 ret = ret2;
1731 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001732
Jan Karacf108bc2008-07-11 19:27:31 -04001733 unlock_page(page);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001734 page_cache_release(page);
Jan Karaffacfa72009-07-13 16:22:22 -04001735 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001736 /* if we have allocated more blocks and copied
1737 * less. We will have blocks allocated outside
1738 * inode->i_size. So truncate them
1739 */
1740 ext4_orphan_add(handle, inode);
1741
Mingming Cao617ba132006-10-11 01:20:53 -07001742 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001743 if (!ret)
1744 ret = ret2;
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001745 if (pos + len > inode->i_size) {
Jan Karaffacfa72009-07-13 16:22:22 -04001746 ext4_truncate(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001747 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001748 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001749 * on the orphan list; we need to make sure the inode
1750 * is removed from the orphan list in that case.
1751 */
1752 if (inode->i_nlink)
1753 ext4_orphan_del(NULL, inode);
1754 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001755
1756 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001757}
Mingming Caod2a17632008-07-14 17:52:37 -04001758
1759static int ext4_da_reserve_space(struct inode *inode, int nrblocks)
1760{
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001761 int retries = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01001762 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1763 unsigned long md_needed, mdblocks, total = 0;
Mingming Caod2a17632008-07-14 17:52:37 -04001764
1765 /*
1766 * recalculate the amount of metadata blocks to reserve
1767 * in order to allocate nrblocks
1768 * worse case is one extent per block
1769 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001770repeat:
Mingming Caod2a17632008-07-14 17:52:37 -04001771 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1772 total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks;
1773 mdblocks = ext4_calc_metadata_amount(inode, total);
1774 BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks);
1775
1776 md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks;
1777 total = md_needed + nrblocks;
1778
Mingming Cao60e58e02009-01-22 18:13:05 +01001779 /*
1780 * Make quota reservation here to prevent quota overflow
1781 * later. Real quota accounting is done at pages writeout
1782 * time.
1783 */
1784 if (vfs_dq_reserve_block(inode, total)) {
1785 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1786 return -EDQUOT;
1787 }
1788
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04001789 if (ext4_claim_free_blocks(sbi, total)) {
Mingming Caod2a17632008-07-14 17:52:37 -04001790 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001791 if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1792 yield();
1793 goto repeat;
1794 }
Mingming Cao60e58e02009-01-22 18:13:05 +01001795 vfs_dq_release_reservation_block(inode, total);
Mingming Caod2a17632008-07-14 17:52:37 -04001796 return -ENOSPC;
1797 }
Mingming Caod2a17632008-07-14 17:52:37 -04001798 EXT4_I(inode)->i_reserved_data_blocks += nrblocks;
1799 EXT4_I(inode)->i_reserved_meta_blocks = mdblocks;
1800
1801 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1802 return 0; /* success */
1803}
1804
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001805static void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001806{
1807 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1808 int total, mdb, mdb_free, release;
1809
Mingming Caocd213222008-08-19 22:16:59 -04001810 if (!to_free)
1811 return; /* Nothing to release, exit */
1812
Mingming Caod2a17632008-07-14 17:52:37 -04001813 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001814
1815 if (!EXT4_I(inode)->i_reserved_data_blocks) {
1816 /*
1817 * if there is no reserved blocks, but we try to free some
1818 * then the counter is messed up somewhere.
1819 * but since this function is called from invalidate
1820 * page, it's harmless to return without any action
1821 */
1822 printk(KERN_INFO "ext4 delalloc try to release %d reserved "
1823 "blocks for inode %lu, but there is no reserved "
1824 "data blocks\n", to_free, inode->i_ino);
1825 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1826 return;
1827 }
1828
Mingming Caod2a17632008-07-14 17:52:37 -04001829 /* recalculate the number of metablocks still need to be reserved */
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001830 total = EXT4_I(inode)->i_reserved_data_blocks - to_free;
Mingming Caod2a17632008-07-14 17:52:37 -04001831 mdb = ext4_calc_metadata_amount(inode, total);
1832
1833 /* figure out how many metablocks to release */
1834 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1835 mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb;
1836
Mingming Caod2a17632008-07-14 17:52:37 -04001837 release = to_free + mdb_free;
1838
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04001839 /* update fs dirty blocks counter for truncate case */
1840 percpu_counter_sub(&sbi->s_dirtyblocks_counter, release);
Mingming Caod2a17632008-07-14 17:52:37 -04001841
1842 /* update per-inode reservations */
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001843 BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks);
1844 EXT4_I(inode)->i_reserved_data_blocks -= to_free;
Mingming Caod2a17632008-07-14 17:52:37 -04001845
1846 BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks);
1847 EXT4_I(inode)->i_reserved_meta_blocks = mdb;
Mingming Caod2a17632008-07-14 17:52:37 -04001848 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001849
1850 vfs_dq_release_reservation_block(inode, release);
Mingming Caod2a17632008-07-14 17:52:37 -04001851}
1852
1853static void ext4_da_page_release_reservation(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001854 unsigned long offset)
Mingming Caod2a17632008-07-14 17:52:37 -04001855{
1856 int to_release = 0;
1857 struct buffer_head *head, *bh;
1858 unsigned int curr_off = 0;
1859
1860 head = page_buffers(page);
1861 bh = head;
1862 do {
1863 unsigned int next_off = curr_off + bh->b_size;
1864
1865 if ((offset <= curr_off) && (buffer_delay(bh))) {
1866 to_release++;
1867 clear_buffer_delay(bh);
1868 }
1869 curr_off = next_off;
1870 } while ((bh = bh->b_this_page) != head);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001871 ext4_da_release_space(page->mapping->host, to_release);
Mingming Caod2a17632008-07-14 17:52:37 -04001872}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001873
1874/*
Alex Tomas64769242008-07-11 19:27:31 -04001875 * Delayed allocation stuff
1876 */
1877
1878struct mpage_da_data {
1879 struct inode *inode;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001880 sector_t b_blocknr; /* start block number of extent */
1881 size_t b_size; /* size of extent */
1882 unsigned long b_state; /* state of the extent */
Alex Tomas64769242008-07-11 19:27:31 -04001883 unsigned long first_page, next_page; /* extent of pages */
Alex Tomas64769242008-07-11 19:27:31 -04001884 struct writeback_control *wbc;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001885 int io_done;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001886 int pages_written;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001887 int retval;
Alex Tomas64769242008-07-11 19:27:31 -04001888};
1889
1890/*
1891 * mpage_da_submit_io - walks through extent of pages and try to write
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001892 * them with writepage() call back
Alex Tomas64769242008-07-11 19:27:31 -04001893 *
1894 * @mpd->inode: inode
1895 * @mpd->first_page: first page of the extent
1896 * @mpd->next_page: page after the last page of the extent
Alex Tomas64769242008-07-11 19:27:31 -04001897 *
1898 * By the time mpage_da_submit_io() is called we expect all blocks
1899 * to be allocated. this may be wrong if allocation failed.
1900 *
1901 * As pages are already locked by write_cache_pages(), we can't use it
1902 */
1903static int mpage_da_submit_io(struct mpage_da_data *mpd)
1904{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001905 long pages_skipped;
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001906 struct pagevec pvec;
1907 unsigned long index, end;
1908 int ret = 0, err, nr_pages, i;
1909 struct inode *inode = mpd->inode;
1910 struct address_space *mapping = inode->i_mapping;
Alex Tomas64769242008-07-11 19:27:31 -04001911
1912 BUG_ON(mpd->next_page <= mpd->first_page);
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001913 /*
1914 * We need to start from the first_page to the next_page - 1
1915 * to make sure we also write the mapped dirty buffer_heads.
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001916 * If we look at mpd->b_blocknr we would only be looking
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001917 * at the currently mapped buffer_heads.
1918 */
Alex Tomas64769242008-07-11 19:27:31 -04001919 index = mpd->first_page;
1920 end = mpd->next_page - 1;
1921
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001922 pagevec_init(&pvec, 0);
Alex Tomas64769242008-07-11 19:27:31 -04001923 while (index <= end) {
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001924 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
Alex Tomas64769242008-07-11 19:27:31 -04001925 if (nr_pages == 0)
1926 break;
1927 for (i = 0; i < nr_pages; i++) {
1928 struct page *page = pvec.pages[i];
1929
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001930 index = page->index;
1931 if (index > end)
1932 break;
1933 index++;
1934
1935 BUG_ON(!PageLocked(page));
1936 BUG_ON(PageWriteback(page));
1937
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001938 pages_skipped = mpd->wbc->pages_skipped;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001939 err = mapping->a_ops->writepage(page, mpd->wbc);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001940 if (!err && (pages_skipped == mpd->wbc->pages_skipped))
1941 /*
1942 * have successfully written the page
1943 * without skipping the same
1944 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001945 mpd->pages_written++;
Alex Tomas64769242008-07-11 19:27:31 -04001946 /*
1947 * In error case, we have to continue because
1948 * remaining pages are still locked
1949 * XXX: unlock and re-dirty them?
1950 */
1951 if (ret == 0)
1952 ret = err;
1953 }
1954 pagevec_release(&pvec);
1955 }
Alex Tomas64769242008-07-11 19:27:31 -04001956 return ret;
1957}
1958
1959/*
1960 * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers
1961 *
1962 * @mpd->inode - inode to walk through
1963 * @exbh->b_blocknr - first block on a disk
1964 * @exbh->b_size - amount of space in bytes
1965 * @logical - first logical block to start assignment with
1966 *
1967 * the function goes through all passed space and put actual disk
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001968 * block numbers into buffer heads, dropping BH_Delay and BH_Unwritten
Alex Tomas64769242008-07-11 19:27:31 -04001969 */
1970static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical,
1971 struct buffer_head *exbh)
1972{
1973 struct inode *inode = mpd->inode;
1974 struct address_space *mapping = inode->i_mapping;
1975 int blocks = exbh->b_size >> inode->i_blkbits;
1976 sector_t pblock = exbh->b_blocknr, cur_logical;
1977 struct buffer_head *head, *bh;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001978 pgoff_t index, end;
Alex Tomas64769242008-07-11 19:27:31 -04001979 struct pagevec pvec;
1980 int nr_pages, i;
1981
1982 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
1983 end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
1984 cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1985
1986 pagevec_init(&pvec, 0);
1987
1988 while (index <= end) {
1989 /* XXX: optimize tail */
1990 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1991 if (nr_pages == 0)
1992 break;
1993 for (i = 0; i < nr_pages; i++) {
1994 struct page *page = pvec.pages[i];
1995
1996 index = page->index;
1997 if (index > end)
1998 break;
1999 index++;
2000
2001 BUG_ON(!PageLocked(page));
2002 BUG_ON(PageWriteback(page));
2003 BUG_ON(!page_has_buffers(page));
2004
2005 bh = page_buffers(page);
2006 head = bh;
2007
2008 /* skip blocks out of the range */
2009 do {
2010 if (cur_logical >= logical)
2011 break;
2012 cur_logical++;
2013 } while ((bh = bh->b_this_page) != head);
2014
2015 do {
2016 if (cur_logical >= logical + blocks)
2017 break;
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002018
2019 if (buffer_delay(bh) ||
2020 buffer_unwritten(bh)) {
2021
2022 BUG_ON(bh->b_bdev != inode->i_sb->s_bdev);
2023
2024 if (buffer_delay(bh)) {
2025 clear_buffer_delay(bh);
2026 bh->b_blocknr = pblock;
2027 } else {
2028 /*
2029 * unwritten already should have
2030 * blocknr assigned. Verify that
2031 */
2032 clear_buffer_unwritten(bh);
2033 BUG_ON(bh->b_blocknr != pblock);
2034 }
2035
Mingming Cao61628a32008-07-11 19:27:31 -04002036 } else if (buffer_mapped(bh))
Alex Tomas64769242008-07-11 19:27:31 -04002037 BUG_ON(bh->b_blocknr != pblock);
Alex Tomas64769242008-07-11 19:27:31 -04002038
2039 cur_logical++;
2040 pblock++;
2041 } while ((bh = bh->b_this_page) != head);
2042 }
2043 pagevec_release(&pvec);
2044 }
2045}
2046
2047
2048/*
2049 * __unmap_underlying_blocks - just a helper function to unmap
2050 * set of blocks described by @bh
2051 */
2052static inline void __unmap_underlying_blocks(struct inode *inode,
2053 struct buffer_head *bh)
2054{
2055 struct block_device *bdev = inode->i_sb->s_bdev;
2056 int blocks, i;
2057
2058 blocks = bh->b_size >> inode->i_blkbits;
2059 for (i = 0; i < blocks; i++)
2060 unmap_underlying_metadata(bdev, bh->b_blocknr + i);
2061}
2062
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002063static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd,
2064 sector_t logical, long blk_cnt)
2065{
2066 int nr_pages, i;
2067 pgoff_t index, end;
2068 struct pagevec pvec;
2069 struct inode *inode = mpd->inode;
2070 struct address_space *mapping = inode->i_mapping;
2071
2072 index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
2073 end = (logical + blk_cnt - 1) >>
2074 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2075 while (index <= end) {
2076 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
2077 if (nr_pages == 0)
2078 break;
2079 for (i = 0; i < nr_pages; i++) {
2080 struct page *page = pvec.pages[i];
2081 index = page->index;
2082 if (index > end)
2083 break;
2084 index++;
2085
2086 BUG_ON(!PageLocked(page));
2087 BUG_ON(PageWriteback(page));
2088 block_invalidatepage(page, 0);
2089 ClearPageUptodate(page);
2090 unlock_page(page);
2091 }
2092 }
2093 return;
2094}
2095
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002096static void ext4_print_free_blocks(struct inode *inode)
2097{
2098 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2099 printk(KERN_EMERG "Total free blocks count %lld\n",
2100 ext4_count_free_blocks(inode->i_sb));
2101 printk(KERN_EMERG "Free/Dirty block details\n");
2102 printk(KERN_EMERG "free_blocks=%lld\n",
Alexander Beregalov8f72fbd2008-10-29 17:13:08 -04002103 (long long)percpu_counter_sum(&sbi->s_freeblocks_counter));
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002104 printk(KERN_EMERG "dirty_blocks=%lld\n",
Alexander Beregalov8f72fbd2008-10-29 17:13:08 -04002105 (long long)percpu_counter_sum(&sbi->s_dirtyblocks_counter));
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002106 printk(KERN_EMERG "Block reservation details\n");
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002107 printk(KERN_EMERG "i_reserved_data_blocks=%u\n",
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002108 EXT4_I(inode)->i_reserved_data_blocks);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002109 printk(KERN_EMERG "i_reserved_meta_blocks=%u\n",
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002110 EXT4_I(inode)->i_reserved_meta_blocks);
2111 return;
2112}
2113
Theodore Ts'ob920c752009-05-14 00:54:29 -04002114/*
Alex Tomas64769242008-07-11 19:27:31 -04002115 * mpage_da_map_blocks - go through given space
2116 *
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002117 * @mpd - bh describing space
Alex Tomas64769242008-07-11 19:27:31 -04002118 *
2119 * The function skips space we know is already mapped to disk blocks.
2120 *
Alex Tomas64769242008-07-11 19:27:31 -04002121 */
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002122static int mpage_da_map_blocks(struct mpage_da_data *mpd)
Alex Tomas64769242008-07-11 19:27:31 -04002123{
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002124 int err, blks, get_blocks_flags;
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002125 struct buffer_head new;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002126 sector_t next = mpd->b_blocknr;
2127 unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
2128 loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
2129 handle_t *handle = NULL;
Alex Tomas64769242008-07-11 19:27:31 -04002130
2131 /*
2132 * We consider only non-mapped and non-allocated blocks
2133 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002134 if ((mpd->b_state & (1 << BH_Mapped)) &&
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002135 !(mpd->b_state & (1 << BH_Delay)) &&
2136 !(mpd->b_state & (1 << BH_Unwritten)))
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002137 return 0;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002138
2139 /*
2140 * If we didn't accumulate anything to write simply return
2141 */
2142 if (!mpd->b_size)
2143 return 0;
2144
2145 handle = ext4_journal_current_handle();
2146 BUG_ON(!handle);
2147
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04002148 /*
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002149 * Call ext4_get_blocks() to allocate any delayed allocation
2150 * blocks, or to convert an uninitialized extent to be
2151 * initialized (in the case where we have written into
2152 * one or more preallocated blocks).
2153 *
2154 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
2155 * indicate that we are on the delayed allocation path. This
2156 * affects functions in many different parts of the allocation
2157 * call path. This flag exists primarily because we don't
2158 * want to change *many* call functions, so ext4_get_blocks()
2159 * will set the magic i_delalloc_reserved_flag once the
2160 * inode's allocation semaphore is taken.
2161 *
2162 * If the blocks in questions were delalloc blocks, set
2163 * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
2164 * variables are updated after the blocks have been allocated.
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04002165 */
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002166 new.b_state = 0;
2167 get_blocks_flags = (EXT4_GET_BLOCKS_CREATE |
2168 EXT4_GET_BLOCKS_DELALLOC_RESERVE);
2169 if (mpd->b_state & (1 << BH_Delay))
2170 get_blocks_flags |= EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002171 blks = ext4_get_blocks(handle, mpd->inode, next, max_blocks,
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04002172 &new, get_blocks_flags);
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002173 if (blks < 0) {
2174 err = blks;
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002175 /*
2176 * If get block returns with error we simply
2177 * return. Later writepage will redirty the page and
2178 * writepages will find the dirty page again
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002179 */
2180 if (err == -EAGAIN)
2181 return 0;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002182
2183 if (err == -ENOSPC &&
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002184 ext4_count_free_blocks(mpd->inode->i_sb)) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002185 mpd->retval = err;
2186 return 0;
2187 }
2188
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002189 /*
Theodore Ts'oed5bde02009-02-23 10:48:07 -05002190 * get block failure will cause us to loop in
2191 * writepages, because a_ops->writepage won't be able
2192 * to make progress. The page will be redirtied by
2193 * writepage and writepages will again try to write
2194 * the same.
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002195 */
2196 printk(KERN_EMERG "%s block allocation failed for inode %lu "
2197 "at logical offset %llu with max blocks "
2198 "%zd with error %d\n",
2199 __func__, mpd->inode->i_ino,
2200 (unsigned long long)next,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002201 mpd->b_size >> mpd->inode->i_blkbits, err);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002202 printk(KERN_EMERG "This should not happen.!! "
2203 "Data will be lost\n");
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002204 if (err == -ENOSPC) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002205 ext4_print_free_blocks(mpd->inode);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04002206 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002207 /* invalidate all the pages */
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002208 ext4_da_block_invalidatepages(mpd, next,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002209 mpd->b_size >> mpd->inode->i_blkbits);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002210 return err;
2211 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002212 BUG_ON(blks == 0);
2213
2214 new.b_size = (blks << mpd->inode->i_blkbits);
Alex Tomas64769242008-07-11 19:27:31 -04002215
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002216 if (buffer_new(&new))
2217 __unmap_underlying_blocks(mpd->inode, &new);
Alex Tomas64769242008-07-11 19:27:31 -04002218
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002219 /*
2220 * If blocks are delayed marked, we need to
2221 * put actual blocknr and drop delayed bit
2222 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002223 if ((mpd->b_state & (1 << BH_Delay)) ||
2224 (mpd->b_state & (1 << BH_Unwritten)))
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002225 mpage_put_bnr_to_bhs(mpd, next, &new);
2226
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002227 if (ext4_should_order_data(mpd->inode)) {
2228 err = ext4_jbd2_file_inode(handle, mpd->inode);
2229 if (err)
2230 return err;
2231 }
2232
2233 /*
Jan Kara03f5d8b2009-06-09 00:17:05 -04002234 * Update on-disk size along with block allocation.
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04002235 */
2236 disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
2237 if (disksize > i_size_read(mpd->inode))
2238 disksize = i_size_read(mpd->inode);
2239 if (disksize > EXT4_I(mpd->inode)->i_disksize) {
2240 ext4_update_i_disksize(mpd->inode, disksize);
2241 return ext4_mark_inode_dirty(handle, mpd->inode);
2242 }
2243
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002244 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04002245}
2246
Aneesh Kumar K.Vbf068ee2008-08-19 22:16:43 -04002247#define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
2248 (1 << BH_Delay) | (1 << BH_Unwritten))
Alex Tomas64769242008-07-11 19:27:31 -04002249
2250/*
2251 * mpage_add_bh_to_extent - try to add one more block to extent of blocks
2252 *
2253 * @mpd->lbh - extent of blocks
2254 * @logical - logical number of the block in the file
2255 * @bh - bh of the block (used to access block's state)
2256 *
2257 * the function is used to collect contig. blocks in same state
2258 */
2259static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002260 sector_t logical, size_t b_size,
2261 unsigned long b_state)
Alex Tomas64769242008-07-11 19:27:31 -04002262{
Alex Tomas64769242008-07-11 19:27:31 -04002263 sector_t next;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002264 int nrblocks = mpd->b_size >> mpd->inode->i_blkbits;
Alex Tomas64769242008-07-11 19:27:31 -04002265
Mingming Cao525f4ed2008-08-19 22:15:58 -04002266 /* check if thereserved journal credits might overflow */
2267 if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) {
2268 if (nrblocks >= EXT4_MAX_TRANS_DATA) {
2269 /*
2270 * With non-extent format we are limited by the journal
2271 * credit available. Total credit needed to insert
2272 * nrblocks contiguous blocks is dependent on the
2273 * nrblocks. So limit nrblocks.
2274 */
2275 goto flush_it;
2276 } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
2277 EXT4_MAX_TRANS_DATA) {
2278 /*
2279 * Adding the new buffer_head would make it cross the
2280 * allowed limit for which we have journal credit
2281 * reserved. So limit the new bh->b_size
2282 */
2283 b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
2284 mpd->inode->i_blkbits;
2285 /* we will do mpage_da_submit_io in the next loop */
2286 }
2287 }
Alex Tomas64769242008-07-11 19:27:31 -04002288 /*
2289 * First block in the extent
2290 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002291 if (mpd->b_size == 0) {
2292 mpd->b_blocknr = logical;
2293 mpd->b_size = b_size;
2294 mpd->b_state = b_state & BH_FLAGS;
Alex Tomas64769242008-07-11 19:27:31 -04002295 return;
2296 }
2297
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002298 next = mpd->b_blocknr + nrblocks;
Alex Tomas64769242008-07-11 19:27:31 -04002299 /*
2300 * Can we merge the block to our big extent?
2301 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002302 if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
2303 mpd->b_size += b_size;
Alex Tomas64769242008-07-11 19:27:31 -04002304 return;
2305 }
2306
Mingming Cao525f4ed2008-08-19 22:15:58 -04002307flush_it:
Alex Tomas64769242008-07-11 19:27:31 -04002308 /*
2309 * We couldn't merge the block to our extent, so we
2310 * need to flush current extent and start new one
2311 */
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002312 if (mpage_da_map_blocks(mpd) == 0)
2313 mpage_da_submit_io(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002314 mpd->io_done = 1;
2315 return;
Alex Tomas64769242008-07-11 19:27:31 -04002316}
2317
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002318static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002319{
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002320 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002321}
2322
Alex Tomas64769242008-07-11 19:27:31 -04002323/*
2324 * __mpage_da_writepage - finds extent of pages and blocks
2325 *
2326 * @page: page to consider
2327 * @wbc: not used, we just follow rules
2328 * @data: context
2329 *
2330 * The function finds extents of pages and scan them for all blocks.
2331 */
2332static int __mpage_da_writepage(struct page *page,
2333 struct writeback_control *wbc, void *data)
2334{
2335 struct mpage_da_data *mpd = data;
2336 struct inode *inode = mpd->inode;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002337 struct buffer_head *bh, *head;
Alex Tomas64769242008-07-11 19:27:31 -04002338 sector_t logical;
2339
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002340 if (mpd->io_done) {
2341 /*
2342 * Rest of the page in the page_vec
2343 * redirty then and skip then. We will
2344 * try to to write them again after
2345 * starting a new transaction
2346 */
2347 redirty_page_for_writepage(wbc, page);
2348 unlock_page(page);
2349 return MPAGE_DA_EXTENT_TAIL;
2350 }
Alex Tomas64769242008-07-11 19:27:31 -04002351 /*
2352 * Can we merge this page to current extent?
2353 */
2354 if (mpd->next_page != page->index) {
2355 /*
2356 * Nope, we can't. So, we map non-allocated blocks
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002357 * and start IO on them using writepage()
Alex Tomas64769242008-07-11 19:27:31 -04002358 */
2359 if (mpd->next_page != mpd->first_page) {
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04002360 if (mpage_da_map_blocks(mpd) == 0)
2361 mpage_da_submit_io(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002362 /*
2363 * skip rest of the page in the page_vec
2364 */
2365 mpd->io_done = 1;
2366 redirty_page_for_writepage(wbc, page);
2367 unlock_page(page);
2368 return MPAGE_DA_EXTENT_TAIL;
Alex Tomas64769242008-07-11 19:27:31 -04002369 }
2370
2371 /*
2372 * Start next extent of pages ...
2373 */
2374 mpd->first_page = page->index;
2375
2376 /*
2377 * ... and blocks
2378 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002379 mpd->b_size = 0;
2380 mpd->b_state = 0;
2381 mpd->b_blocknr = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002382 }
2383
2384 mpd->next_page = page->index + 1;
2385 logical = (sector_t) page->index <<
2386 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2387
2388 if (!page_has_buffers(page)) {
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002389 mpage_add_bh_to_extent(mpd, logical, PAGE_CACHE_SIZE,
2390 (1 << BH_Dirty) | (1 << BH_Uptodate));
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002391 if (mpd->io_done)
2392 return MPAGE_DA_EXTENT_TAIL;
Alex Tomas64769242008-07-11 19:27:31 -04002393 } else {
2394 /*
2395 * Page with regular buffer heads, just add all dirty ones
2396 */
2397 head = page_buffers(page);
2398 bh = head;
2399 do {
2400 BUG_ON(buffer_locked(bh));
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002401 /*
2402 * We need to try to allocate
2403 * unmapped blocks in the same page.
2404 * Otherwise we won't make progress
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002405 * with the page in ext4_writepage
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002406 */
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002407 if (ext4_bh_delay_or_unwritten(NULL, bh)) {
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002408 mpage_add_bh_to_extent(mpd, logical,
2409 bh->b_size,
2410 bh->b_state);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002411 if (mpd->io_done)
2412 return MPAGE_DA_EXTENT_TAIL;
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05002413 } else if (buffer_dirty(bh) && (buffer_mapped(bh))) {
2414 /*
2415 * mapped dirty buffer. We need to update
2416 * the b_state because we look at
2417 * b_state in mpage_da_map_blocks. We don't
2418 * update b_size because if we find an
2419 * unmapped buffer_head later we need to
2420 * use the b_state flag of that buffer_head.
2421 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05002422 if (mpd->b_size == 0)
2423 mpd->b_state = bh->b_state & BH_FLAGS;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002424 }
Alex Tomas64769242008-07-11 19:27:31 -04002425 logical++;
2426 } while ((bh = bh->b_this_page) != head);
2427 }
2428
2429 return 0;
2430}
2431
2432/*
Theodore Ts'ob920c752009-05-14 00:54:29 -04002433 * This is a special get_blocks_t callback which is used by
2434 * ext4_da_write_begin(). It will either return mapped block or
2435 * reserve space for a single block.
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002436 *
2437 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
2438 * We also have b_blocknr = -1 and b_bdev initialized properly
2439 *
2440 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
2441 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
2442 * initialized properly.
Alex Tomas64769242008-07-11 19:27:31 -04002443 */
2444static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
2445 struct buffer_head *bh_result, int create)
2446{
2447 int ret = 0;
Aneesh Kumar K.V33b98172009-05-12 14:40:37 -04002448 sector_t invalid_block = ~((sector_t) 0xffff);
2449
2450 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
2451 invalid_block = ~0;
Alex Tomas64769242008-07-11 19:27:31 -04002452
2453 BUG_ON(create == 0);
2454 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
2455
2456 /*
2457 * first, we need to know whether the block is allocated already
2458 * preallocated blocks are unmapped but should treated
2459 * the same as allocated blocks.
2460 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04002461 ret = ext4_get_blocks(NULL, inode, iblock, 1, bh_result, 0);
Mingming Caod2a17632008-07-14 17:52:37 -04002462 if ((ret == 0) && !buffer_delay(bh_result)) {
2463 /* the block isn't (pre)allocated yet, let's reserve space */
Alex Tomas64769242008-07-11 19:27:31 -04002464 /*
2465 * XXX: __block_prepare_write() unmaps passed block,
2466 * is it OK?
2467 */
Mingming Caod2a17632008-07-14 17:52:37 -04002468 ret = ext4_da_reserve_space(inode, 1);
2469 if (ret)
2470 /* not enough space to reserve */
2471 return ret;
2472
Aneesh Kumar K.V33b98172009-05-12 14:40:37 -04002473 map_bh(bh_result, inode->i_sb, invalid_block);
Alex Tomas64769242008-07-11 19:27:31 -04002474 set_buffer_new(bh_result);
2475 set_buffer_delay(bh_result);
2476 } else if (ret > 0) {
2477 bh_result->b_size = (ret << inode->i_blkbits);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002478 if (buffer_unwritten(bh_result)) {
2479 /* A delayed write to unwritten bh should
2480 * be marked new and mapped. Mapped ensures
2481 * that we don't do get_block multiple times
2482 * when we write to the same offset and new
2483 * ensures that we do proper zero out for
2484 * partial write.
2485 */
Aneesh Kumar K.V9c1ee182009-05-13 18:36:58 -04002486 set_buffer_new(bh_result);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002487 set_buffer_mapped(bh_result);
2488 }
Alex Tomas64769242008-07-11 19:27:31 -04002489 ret = 0;
2490 }
2491
2492 return ret;
2493}
Mingming Cao61628a32008-07-11 19:27:31 -04002494
Theodore Ts'ob920c752009-05-14 00:54:29 -04002495/*
2496 * This function is used as a standard get_block_t calback function
2497 * when there is no desire to allocate any blocks. It is used as a
2498 * callback function for block_prepare_write(), nobh_writepage(), and
2499 * block_write_full_page(). These functions should only try to map a
2500 * single block at a time.
2501 *
2502 * Since this function doesn't do block allocations even if the caller
2503 * requests it by passing in create=1, it is critically important that
2504 * any caller checks to make sure that any buffer heads are returned
2505 * by this function are either all already mapped or marked for
2506 * delayed allocation before calling nobh_writepage() or
2507 * block_write_full_page(). Otherwise, b_blocknr could be left
2508 * unitialized, and the page write functions will be taken by
2509 * surprise.
2510 */
2511static int noalloc_get_block_write(struct inode *inode, sector_t iblock,
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002512 struct buffer_head *bh_result, int create)
2513{
2514 int ret = 0;
2515 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
2516
Theodore Ts'oa2dc52b2009-05-12 13:51:29 -04002517 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
2518
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002519 /*
2520 * we don't want to do block allocation in writepage
2521 * so call get_block_wrap with create = 0
2522 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04002523 ret = ext4_get_blocks(NULL, inode, iblock, max_blocks, bh_result, 0);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002524 if (ret > 0) {
2525 bh_result->b_size = (ret << inode->i_blkbits);
2526 ret = 0;
2527 }
2528 return ret;
Mingming Cao61628a32008-07-11 19:27:31 -04002529}
2530
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002531static int bget_one(handle_t *handle, struct buffer_head *bh)
2532{
2533 get_bh(bh);
2534 return 0;
2535}
2536
2537static int bput_one(handle_t *handle, struct buffer_head *bh)
2538{
2539 put_bh(bh);
2540 return 0;
2541}
2542
2543static int __ext4_journalled_writepage(struct page *page,
2544 struct writeback_control *wbc,
2545 unsigned int len)
2546{
2547 struct address_space *mapping = page->mapping;
2548 struct inode *inode = mapping->host;
2549 struct buffer_head *page_bufs;
2550 handle_t *handle = NULL;
2551 int ret = 0;
2552 int err;
2553
2554 page_bufs = page_buffers(page);
2555 BUG_ON(!page_bufs);
2556 walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one);
2557 /* As soon as we unlock the page, it can go away, but we have
2558 * references to buffers so we are safe */
2559 unlock_page(page);
2560
2561 handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
2562 if (IS_ERR(handle)) {
2563 ret = PTR_ERR(handle);
2564 goto out;
2565 }
2566
2567 ret = walk_page_buffers(handle, page_bufs, 0, len, NULL,
2568 do_journal_get_write_access);
2569
2570 err = walk_page_buffers(handle, page_bufs, 0, len, NULL,
2571 write_end_fn);
2572 if (ret == 0)
2573 ret = err;
2574 err = ext4_journal_stop(handle);
2575 if (!ret)
2576 ret = err;
2577
2578 walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one);
2579 EXT4_I(inode)->i_state |= EXT4_STATE_JDATA;
2580out:
2581 return ret;
2582}
2583
Mingming Cao61628a32008-07-11 19:27:31 -04002584/*
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002585 * Note that we don't need to start a transaction unless we're journaling data
2586 * because we should have holes filled from ext4_page_mkwrite(). We even don't
2587 * need to file the inode to the transaction's list in ordered mode because if
2588 * we are writing back data added by write(), the inode is already there and if
2589 * we are writing back data modified via mmap(), noone guarantees in which
2590 * transaction the data will hit the disk. In case we are journaling data, we
2591 * cannot start transaction directly because transaction start ranks above page
2592 * lock so we have to do some magic.
2593 *
Theodore Ts'ob920c752009-05-14 00:54:29 -04002594 * This function can get called via...
2595 * - ext4_da_writepages after taking page lock (have journal handle)
2596 * - journal_submit_inode_data_buffers (no journal handle)
2597 * - shrink_page_list via pdflush (no journal handle)
2598 * - grab_page_cache when doing write_begin (have journal handle)
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002599 *
2600 * We don't do any block allocation in this function. If we have page with
2601 * multiple blocks we need to write those buffer_heads that are mapped. This
2602 * is important for mmaped based write. So if we do with blocksize 1K
2603 * truncate(f, 1024);
2604 * a = mmap(f, 0, 4096);
2605 * a[0] = 'a';
2606 * truncate(f, 4096);
2607 * we have in the page first buffer_head mapped via page_mkwrite call back
2608 * but other bufer_heads would be unmapped but dirty(dirty done via the
2609 * do_wp_page). So writepage should write the first block. If we modify
2610 * the mmap area beyond 1024 we will again get a page_fault and the
2611 * page_mkwrite callback will do the block allocation and mark the
2612 * buffer_heads mapped.
2613 *
2614 * We redirty the page if we have any buffer_heads that is either delay or
2615 * unwritten in the page.
2616 *
2617 * We can get recursively called as show below.
2618 *
2619 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
2620 * ext4_writepage()
2621 *
2622 * But since we don't do any block allocation we should not deadlock.
2623 * Page also have the dirty flag cleared so we don't get recurive page_lock.
Mingming Cao61628a32008-07-11 19:27:31 -04002624 */
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002625static int ext4_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002626 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002627{
Alex Tomas64769242008-07-11 19:27:31 -04002628 int ret = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002629 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002630 unsigned int len;
Mingming Cao61628a32008-07-11 19:27:31 -04002631 struct buffer_head *page_bufs;
2632 struct inode *inode = page->mapping->host;
Alex Tomas64769242008-07-11 19:27:31 -04002633
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002634 trace_ext4_writepage(inode, page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002635 size = i_size_read(inode);
2636 if (page->index == size >> PAGE_CACHE_SHIFT)
2637 len = size & ~PAGE_CACHE_MASK;
2638 else
2639 len = PAGE_CACHE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04002640
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002641 if (page_has_buffers(page)) {
Mingming Cao61628a32008-07-11 19:27:31 -04002642 page_bufs = page_buffers(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002643 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002644 ext4_bh_delay_or_unwritten)) {
Mingming Cao61628a32008-07-11 19:27:31 -04002645 /*
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002646 * We don't want to do block allocation
2647 * So redirty the page and return
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04002648 * We may reach here when we do a journal commit
2649 * via journal_submit_inode_data_buffers.
2650 * If we don't have mapping block we just ignore
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002651 * them. We can also reach here via shrink_page_list
2652 */
2653 redirty_page_for_writepage(wbc, page);
2654 unlock_page(page);
2655 return 0;
2656 }
2657 } else {
2658 /*
2659 * The test for page_has_buffers() is subtle:
2660 * We know the page is dirty but it lost buffers. That means
2661 * that at some moment in time after write_begin()/write_end()
2662 * has been called all buffers have been clean and thus they
2663 * must have been written at least once. So they are all
2664 * mapped and we can happily proceed with mapping them
2665 * and writing the page.
2666 *
2667 * Try to initialize the buffer_heads and check whether
2668 * all are mapped and non delay. We don't want to
2669 * do block allocation here.
2670 */
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04002671 ret = block_prepare_write(page, 0, len,
Theodore Ts'ob920c752009-05-14 00:54:29 -04002672 noalloc_get_block_write);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002673 if (!ret) {
2674 page_bufs = page_buffers(page);
2675 /* check whether all are mapped and non delay */
2676 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002677 ext4_bh_delay_or_unwritten)) {
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002678 redirty_page_for_writepage(wbc, page);
2679 unlock_page(page);
2680 return 0;
2681 }
2682 } else {
2683 /*
2684 * We can't do block allocation here
2685 * so just redity the page and unlock
2686 * and return
Mingming Cao61628a32008-07-11 19:27:31 -04002687 */
Mingming Cao61628a32008-07-11 19:27:31 -04002688 redirty_page_for_writepage(wbc, page);
2689 unlock_page(page);
2690 return 0;
2691 }
Aneesh Kumar K.Ved9b3e32008-11-07 09:06:45 -05002692 /* now mark the buffer_heads as dirty and uptodate */
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04002693 block_commit_write(page, 0, len);
Alex Tomas64769242008-07-11 19:27:31 -04002694 }
2695
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002696 if (PageChecked(page) && ext4_should_journal_data(inode)) {
2697 /*
2698 * It's mmapped pagecache. Add buffers and journal it. There
2699 * doesn't seem much point in redirtying the page here.
2700 */
2701 ClearPageChecked(page);
2702 return __ext4_journalled_writepage(page, wbc, len);
2703 }
2704
Alex Tomas64769242008-07-11 19:27:31 -04002705 if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode))
Theodore Ts'ob920c752009-05-14 00:54:29 -04002706 ret = nobh_writepage(page, noalloc_get_block_write, wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002707 else
Theodore Ts'ob920c752009-05-14 00:54:29 -04002708 ret = block_write_full_page(page, noalloc_get_block_write,
2709 wbc);
Alex Tomas64769242008-07-11 19:27:31 -04002710
Alex Tomas64769242008-07-11 19:27:31 -04002711 return ret;
2712}
2713
Mingming Cao61628a32008-07-11 19:27:31 -04002714/*
Mingming Cao525f4ed2008-08-19 22:15:58 -04002715 * This is called via ext4_da_writepages() to
2716 * calulate the total number of credits to reserve to fit
2717 * a single extent allocation into a single transaction,
2718 * ext4_da_writpeages() will loop calling this before
2719 * the block allocation.
Mingming Cao61628a32008-07-11 19:27:31 -04002720 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002721
2722static int ext4_da_writepages_trans_blocks(struct inode *inode)
2723{
2724 int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
2725
2726 /*
2727 * With non-extent format the journal credit needed to
2728 * insert nrblocks contiguous block is dependent on
2729 * number of contiguous block. So we will limit
2730 * number of contiguous block to a sane value
2731 */
2732 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2733 (max_blocks > EXT4_MAX_TRANS_DATA))
2734 max_blocks = EXT4_MAX_TRANS_DATA;
2735
2736 return ext4_chunk_trans_blocks(inode, max_blocks);
2737}
Mingming Cao61628a32008-07-11 19:27:31 -04002738
Alex Tomas64769242008-07-11 19:27:31 -04002739static int ext4_da_writepages(struct address_space *mapping,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002740 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002741{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002742 pgoff_t index;
2743 int range_whole = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002744 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002745 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002746 struct inode *inode = mapping->host;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002747 int no_nrwrite_index_update;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002748 int pages_written = 0;
2749 long pages_skipped;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002750 int range_cyclic, cycled = 1, io_done = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002751 int needed_blocks, ret = 0, nr_to_writebump = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002752 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Mingming Cao61628a32008-07-11 19:27:31 -04002753
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002754 trace_ext4_da_writepages(inode, wbc);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002755
Mingming Cao61628a32008-07-11 19:27:31 -04002756 /*
2757 * No pages to write? This is mainly a kludge to avoid starting
2758 * a transaction for special inodes like journal inode on last iput()
2759 * because that could violate lock ordering on umount
2760 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002761 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Mingming Cao61628a32008-07-11 19:27:31 -04002762 return 0;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002763
2764 /*
2765 * If the filesystem has aborted, it is read-only, so return
2766 * right away instead of dumping stack traces later on that
2767 * will obscure the real source of the problem. We test
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002768 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002769 * the latter could be true if the filesystem is mounted
2770 * read-only, and in that case, ext4_da_writepages should
2771 * *never* be called, so if that ever happens, we would want
2772 * the stack trace.
2773 */
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002774 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002775 return -EROFS;
2776
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002777 /*
2778 * Make sure nr_to_write is >= sbi->s_mb_stream_request
2779 * This make sure small files blocks are allocated in
2780 * single attempt. This ensure that small files
2781 * get less fragmented.
2782 */
2783 if (wbc->nr_to_write < sbi->s_mb_stream_request) {
2784 nr_to_writebump = sbi->s_mb_stream_request - wbc->nr_to_write;
2785 wbc->nr_to_write = sbi->s_mb_stream_request;
2786 }
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002787 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2788 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002789
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002790 range_cyclic = wbc->range_cyclic;
2791 if (wbc->range_cyclic) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002792 index = mapping->writeback_index;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002793 if (index)
2794 cycled = 0;
2795 wbc->range_start = index << PAGE_CACHE_SHIFT;
2796 wbc->range_end = LLONG_MAX;
2797 wbc->range_cyclic = 0;
2798 } else
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002799 index = wbc->range_start >> PAGE_CACHE_SHIFT;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002800
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002801 mpd.wbc = wbc;
2802 mpd.inode = mapping->host;
2803
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002804 /*
2805 * we don't want write_cache_pages to update
2806 * nr_to_write and writeback_index
2807 */
2808 no_nrwrite_index_update = wbc->no_nrwrite_index_update;
2809 wbc->no_nrwrite_index_update = 1;
2810 pages_skipped = wbc->pages_skipped;
2811
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002812retry:
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002813 while (!ret && wbc->nr_to_write > 0) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002814
2815 /*
2816 * we insert one extent at a time. So we need
2817 * credit needed for single extent allocation.
2818 * journalled mode is currently not supported
2819 * by delalloc
2820 */
2821 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002822 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002823
Mingming Cao61628a32008-07-11 19:27:31 -04002824 /* start a new transaction*/
2825 handle = ext4_journal_start(inode, needed_blocks);
2826 if (IS_ERR(handle)) {
2827 ret = PTR_ERR(handle);
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002828 printk(KERN_CRIT "%s: jbd2_start: "
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002829 "%ld pages, ino %lu; err %d\n", __func__,
2830 wbc->nr_to_write, inode->i_ino, ret);
2831 dump_stack();
Mingming Cao61628a32008-07-11 19:27:31 -04002832 goto out_writepages;
2833 }
Theodore Ts'of63e6002009-02-23 16:42:39 -05002834
2835 /*
2836 * Now call __mpage_da_writepage to find the next
2837 * contiguous region of logical blocks that need
2838 * blocks to be allocated by ext4. We don't actually
2839 * submit the blocks for I/O here, even though
2840 * write_cache_pages thinks it will, and will set the
2841 * pages as clean for write before calling
2842 * __mpage_da_writepage().
2843 */
2844 mpd.b_size = 0;
2845 mpd.b_state = 0;
2846 mpd.b_blocknr = 0;
2847 mpd.first_page = 0;
2848 mpd.next_page = 0;
2849 mpd.io_done = 0;
2850 mpd.pages_written = 0;
2851 mpd.retval = 0;
2852 ret = write_cache_pages(mapping, wbc, __mpage_da_writepage,
2853 &mpd);
2854 /*
2855 * If we have a contigous extent of pages and we
2856 * haven't done the I/O yet, map the blocks and submit
2857 * them for I/O.
2858 */
2859 if (!mpd.io_done && mpd.next_page != mpd.first_page) {
2860 if (mpage_da_map_blocks(&mpd) == 0)
2861 mpage_da_submit_io(&mpd);
2862 mpd.io_done = 1;
2863 ret = MPAGE_DA_EXTENT_TAIL;
2864 }
2865 wbc->nr_to_write -= mpd.pages_written;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002866
Mingming Cao61628a32008-07-11 19:27:31 -04002867 ext4_journal_stop(handle);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002868
Eric Sandeen8f64b322009-02-26 00:57:35 -05002869 if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002870 /* commit the transaction which would
2871 * free blocks released in the transaction
2872 * and try again
2873 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002874 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002875 wbc->pages_skipped = pages_skipped;
2876 ret = 0;
2877 } else if (ret == MPAGE_DA_EXTENT_TAIL) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002878 /*
2879 * got one extent now try with
2880 * rest of the pages
2881 */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002882 pages_written += mpd.pages_written;
2883 wbc->pages_skipped = pages_skipped;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002884 ret = 0;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002885 io_done = 1;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002886 } else if (wbc->nr_to_write)
Mingming Cao61628a32008-07-11 19:27:31 -04002887 /*
2888 * There is no more writeout needed
2889 * or we requested for a noblocking writeout
2890 * and we found the device congested
2891 */
Mingming Cao61628a32008-07-11 19:27:31 -04002892 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002893 }
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002894 if (!io_done && !cycled) {
2895 cycled = 1;
2896 index = 0;
2897 wbc->range_start = index << PAGE_CACHE_SHIFT;
2898 wbc->range_end = mapping->writeback_index - 1;
2899 goto retry;
2900 }
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002901 if (pages_skipped != wbc->pages_skipped)
2902 printk(KERN_EMERG "This should not happen leaving %s "
2903 "with nr_to_write = %ld ret = %d\n",
2904 __func__, wbc->nr_to_write, ret);
Mingming Cao61628a32008-07-11 19:27:31 -04002905
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002906 /* Update index */
2907 index += pages_written;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002908 wbc->range_cyclic = range_cyclic;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002909 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2910 /*
2911 * set the writeback_index so that range_cyclic
2912 * mode will write it back later
2913 */
2914 mapping->writeback_index = index;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002915
Mingming Cao61628a32008-07-11 19:27:31 -04002916out_writepages:
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002917 if (!no_nrwrite_index_update)
2918 wbc->no_nrwrite_index_update = 0;
2919 wbc->nr_to_write -= nr_to_writebump;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002920 trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
Mingming Cao61628a32008-07-11 19:27:31 -04002921 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002922}
2923
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002924#define FALL_BACK_TO_NONDELALLOC 1
2925static int ext4_nonda_switch(struct super_block *sb)
2926{
2927 s64 free_blocks, dirty_blocks;
2928 struct ext4_sb_info *sbi = EXT4_SB(sb);
2929
2930 /*
2931 * switch to non delalloc mode if we are running low
2932 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08002933 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002934 * accumulated on each CPU without updating global counters
2935 * Delalloc need an accurate free block accounting. So switch
2936 * to non delalloc when we are near to error range.
2937 */
2938 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
2939 dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter);
2940 if (2 * free_blocks < 3 * dirty_blocks ||
2941 free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) {
2942 /*
2943 * free block count is less that 150% of dirty blocks
2944 * or free blocks is less that watermark
2945 */
2946 return 1;
2947 }
2948 return 0;
2949}
2950
Alex Tomas64769242008-07-11 19:27:31 -04002951static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002952 loff_t pos, unsigned len, unsigned flags,
2953 struct page **pagep, void **fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002954{
Mingming Caod2a17632008-07-14 17:52:37 -04002955 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002956 struct page *page;
2957 pgoff_t index;
2958 unsigned from, to;
2959 struct inode *inode = mapping->host;
2960 handle_t *handle;
2961
2962 index = pos >> PAGE_CACHE_SHIFT;
2963 from = pos & (PAGE_CACHE_SIZE - 1);
2964 to = from + len;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002965
2966 if (ext4_nonda_switch(inode->i_sb)) {
2967 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2968 return ext4_write_begin(file, mapping, pos,
2969 len, flags, pagep, fsdata);
2970 }
2971 *fsdata = (void *)0;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002972 trace_ext4_da_write_begin(inode, pos, len, flags);
Mingming Caod2a17632008-07-14 17:52:37 -04002973retry:
Alex Tomas64769242008-07-11 19:27:31 -04002974 /*
2975 * With delayed allocation, we don't log the i_disksize update
2976 * if there is delayed block allocation. But we still need
2977 * to journalling the i_disksize update if writes to the end
2978 * of file which has an already mapped buffer.
2979 */
2980 handle = ext4_journal_start(inode, 1);
2981 if (IS_ERR(handle)) {
2982 ret = PTR_ERR(handle);
2983 goto out;
2984 }
Jan Karaebd36102009-02-22 21:09:59 -05002985 /* We cannot recurse into the filesystem as the transaction is already
2986 * started */
2987 flags |= AOP_FLAG_NOFS;
Alex Tomas64769242008-07-11 19:27:31 -04002988
Nick Piggin54566b22009-01-04 12:00:53 -08002989 page = grab_cache_page_write_begin(mapping, index, flags);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002990 if (!page) {
2991 ext4_journal_stop(handle);
2992 ret = -ENOMEM;
2993 goto out;
2994 }
Alex Tomas64769242008-07-11 19:27:31 -04002995 *pagep = page;
2996
2997 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
Theodore Ts'ob920c752009-05-14 00:54:29 -04002998 ext4_da_get_block_prep);
Alex Tomas64769242008-07-11 19:27:31 -04002999 if (ret < 0) {
3000 unlock_page(page);
3001 ext4_journal_stop(handle);
3002 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04003003 /*
3004 * block_write_begin may have instantiated a few blocks
3005 * outside i_size. Trim these off again. Don't need
3006 * i_size_read because we hold i_mutex.
3007 */
3008 if (pos + len > inode->i_size)
Jan Karaffacfa72009-07-13 16:22:22 -04003009 ext4_truncate(inode);
Alex Tomas64769242008-07-11 19:27:31 -04003010 }
3011
Mingming Caod2a17632008-07-14 17:52:37 -04003012 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3013 goto retry;
Alex Tomas64769242008-07-11 19:27:31 -04003014out:
3015 return ret;
3016}
3017
Mingming Cao632eaea2008-07-11 19:27:31 -04003018/*
3019 * Check if we should update i_disksize
3020 * when write to the end of file but not require block allocation
3021 */
3022static int ext4_da_should_update_i_disksize(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003023 unsigned long offset)
Mingming Cao632eaea2008-07-11 19:27:31 -04003024{
3025 struct buffer_head *bh;
3026 struct inode *inode = page->mapping->host;
3027 unsigned int idx;
3028 int i;
3029
3030 bh = page_buffers(page);
3031 idx = offset >> inode->i_blkbits;
3032
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003033 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04003034 bh = bh->b_this_page;
3035
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04003036 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
Mingming Cao632eaea2008-07-11 19:27:31 -04003037 return 0;
3038 return 1;
3039}
3040
Alex Tomas64769242008-07-11 19:27:31 -04003041static int ext4_da_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003042 struct address_space *mapping,
3043 loff_t pos, unsigned len, unsigned copied,
3044 struct page *page, void *fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04003045{
3046 struct inode *inode = mapping->host;
3047 int ret = 0, ret2;
3048 handle_t *handle = ext4_journal_current_handle();
3049 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04003050 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003051 int write_mode = (int)(unsigned long)fsdata;
3052
3053 if (write_mode == FALL_BACK_TO_NONDELALLOC) {
3054 if (ext4_should_order_data(inode)) {
3055 return ext4_ordered_write_end(file, mapping, pos,
3056 len, copied, page, fsdata);
3057 } else if (ext4_should_writeback_data(inode)) {
3058 return ext4_writeback_write_end(file, mapping, pos,
3059 len, copied, page, fsdata);
3060 } else {
3061 BUG();
3062 }
3063 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003064
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003065 trace_ext4_da_write_end(inode, pos, len, copied);
Mingming Cao632eaea2008-07-11 19:27:31 -04003066 start = pos & (PAGE_CACHE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003067 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04003068
3069 /*
3070 * generic_write_end() will run mark_inode_dirty() if i_size
3071 * changes. So let's piggyback the i_disksize mark_inode_dirty
3072 * into that.
3073 */
3074
3075 new_i_size = pos + copied;
Mingming Cao632eaea2008-07-11 19:27:31 -04003076 if (new_i_size > EXT4_I(inode)->i_disksize) {
3077 if (ext4_da_should_update_i_disksize(page, end)) {
3078 down_write(&EXT4_I(inode)->i_data_sem);
3079 if (new_i_size > EXT4_I(inode)->i_disksize) {
3080 /*
3081 * Updating i_disksize when extending file
3082 * without needing block allocation
3083 */
3084 if (ext4_should_order_data(inode))
3085 ret = ext4_jbd2_file_inode(handle,
3086 inode);
Alex Tomas64769242008-07-11 19:27:31 -04003087
Mingming Cao632eaea2008-07-11 19:27:31 -04003088 EXT4_I(inode)->i_disksize = new_i_size;
3089 }
3090 up_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003091 /* We need to mark inode dirty even if
3092 * new_i_size is less that inode->i_size
3093 * bu greater than i_disksize.(hint delalloc)
3094 */
3095 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04003096 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003097 }
Alex Tomas64769242008-07-11 19:27:31 -04003098 ret2 = generic_write_end(file, mapping, pos, len, copied,
3099 page, fsdata);
3100 copied = ret2;
3101 if (ret2 < 0)
3102 ret = ret2;
3103 ret2 = ext4_journal_stop(handle);
3104 if (!ret)
3105 ret = ret2;
3106
3107 return ret ? ret : copied;
3108}
3109
3110static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
3111{
Alex Tomas64769242008-07-11 19:27:31 -04003112 /*
3113 * Drop reserved blocks
3114 */
3115 BUG_ON(!PageLocked(page));
3116 if (!page_has_buffers(page))
3117 goto out;
3118
Mingming Caod2a17632008-07-14 17:52:37 -04003119 ext4_da_page_release_reservation(page, offset);
Alex Tomas64769242008-07-11 19:27:31 -04003120
3121out:
3122 ext4_invalidatepage(page, offset);
3123
3124 return;
3125}
3126
Theodore Ts'occd25062009-02-26 01:04:07 -05003127/*
3128 * Force all delayed allocation blocks to be allocated for a given inode.
3129 */
3130int ext4_alloc_da_blocks(struct inode *inode)
3131{
3132 if (!EXT4_I(inode)->i_reserved_data_blocks &&
3133 !EXT4_I(inode)->i_reserved_meta_blocks)
3134 return 0;
3135
3136 /*
3137 * We do something simple for now. The filemap_flush() will
3138 * also start triggering a write of the data blocks, which is
3139 * not strictly speaking necessary (and for users of
3140 * laptop_mode, not even desirable). However, to do otherwise
3141 * would require replicating code paths in:
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003142 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003143 * ext4_da_writepages() ->
3144 * write_cache_pages() ---> (via passed in callback function)
3145 * __mpage_da_writepage() -->
3146 * mpage_add_bh_to_extent()
3147 * mpage_da_map_blocks()
3148 *
3149 * The problem is that write_cache_pages(), located in
3150 * mm/page-writeback.c, marks pages clean in preparation for
3151 * doing I/O, which is not desirable if we're not planning on
3152 * doing I/O at all.
3153 *
3154 * We could call write_cache_pages(), and then redirty all of
3155 * the pages by calling redirty_page_for_writeback() but that
3156 * would be ugly in the extreme. So instead we would need to
3157 * replicate parts of the code in the above functions,
3158 * simplifying them becuase we wouldn't actually intend to
3159 * write out the pages, but rather only collect contiguous
3160 * logical block extents, call the multi-block allocator, and
3161 * then update the buffer heads with the block allocations.
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003162 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003163 * For now, though, we'll cheat by calling filemap_flush(),
3164 * which will map the blocks, and start the I/O, but not
3165 * actually wait for the I/O to complete.
3166 */
3167 return filemap_flush(inode->i_mapping);
3168}
Alex Tomas64769242008-07-11 19:27:31 -04003169
3170/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003171 * bmap() is special. It gets used by applications such as lilo and by
3172 * the swapper to find the on-disk block of a specific piece of data.
3173 *
3174 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07003175 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003176 * filesystem and enables swap, then they may get a nasty shock when the
3177 * data getting swapped to that swapfile suddenly gets overwritten by
3178 * the original zero's written out previously to the journal and
3179 * awaiting writeback in the kernel's buffer cache.
3180 *
3181 * So, if we see any bmap calls here on a modified, data-journaled file,
3182 * take extra steps to flush any blocks which might be in the cache.
3183 */
Mingming Cao617ba132006-10-11 01:20:53 -07003184static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003185{
3186 struct inode *inode = mapping->host;
3187 journal_t *journal;
3188 int err;
3189
Alex Tomas64769242008-07-11 19:27:31 -04003190 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
3191 test_opt(inode->i_sb, DELALLOC)) {
3192 /*
3193 * With delalloc we want to sync the file
3194 * so that we can make sure we allocate
3195 * blocks for file
3196 */
3197 filemap_write_and_wait(mapping);
3198 }
3199
Frank Mayhar03901312009-01-07 00:06:22 -05003200 if (EXT4_JOURNAL(inode) && EXT4_I(inode)->i_state & EXT4_STATE_JDATA) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003201 /*
3202 * This is a REALLY heavyweight approach, but the use of
3203 * bmap on dirty files is expected to be extremely rare:
3204 * only if we run lilo or swapon on a freshly made file
3205 * do we expect this to happen.
3206 *
3207 * (bmap requires CAP_SYS_RAWIO so this does not
3208 * represent an unprivileged user DOS attack --- we'd be
3209 * in trouble if mortal users could trigger this path at
3210 * will.)
3211 *
Mingming Cao617ba132006-10-11 01:20:53 -07003212 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003213 * regular files. If somebody wants to bmap a directory
3214 * or symlink and gets confused because the buffer
3215 * hasn't yet been flushed to disk, they deserve
3216 * everything they get.
3217 */
3218
Mingming Cao617ba132006-10-11 01:20:53 -07003219 EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA;
3220 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07003221 jbd2_journal_lock_updates(journal);
3222 err = jbd2_journal_flush(journal);
3223 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003224
3225 if (err)
3226 return 0;
3227 }
3228
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003229 return generic_block_bmap(mapping, block, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003230}
3231
Mingming Cao617ba132006-10-11 01:20:53 -07003232static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003233{
Mingming Cao617ba132006-10-11 01:20:53 -07003234 return mpage_readpage(page, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003235}
3236
3237static int
Mingming Cao617ba132006-10-11 01:20:53 -07003238ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003239 struct list_head *pages, unsigned nr_pages)
3240{
Mingming Cao617ba132006-10-11 01:20:53 -07003241 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003242}
3243
Mingming Cao617ba132006-10-11 01:20:53 -07003244static void ext4_invalidatepage(struct page *page, unsigned long offset)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003245{
Mingming Cao617ba132006-10-11 01:20:53 -07003246 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003247
3248 /*
3249 * If it's a full truncate we just forget about the pending dirtying
3250 */
3251 if (offset == 0)
3252 ClearPageChecked(page);
3253
Frank Mayhar03901312009-01-07 00:06:22 -05003254 if (journal)
3255 jbd2_journal_invalidatepage(journal, page, offset);
3256 else
3257 block_invalidatepage(page, offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003258}
3259
Mingming Cao617ba132006-10-11 01:20:53 -07003260static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003261{
Mingming Cao617ba132006-10-11 01:20:53 -07003262 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003263
3264 WARN_ON(PageChecked(page));
3265 if (!page_has_buffers(page))
3266 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003267 if (journal)
3268 return jbd2_journal_try_to_free_buffers(journal, page, wait);
3269 else
3270 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003271}
3272
3273/*
3274 * If the O_DIRECT write will extend the file then add this inode to the
3275 * orphan list. So recovery will truncate it back to the original size
3276 * if the machine crashes during the write.
3277 *
3278 * If the O_DIRECT write is intantiating holes inside i_size and the machine
Jan Kara7fb54092008-02-10 01:08:38 -05003279 * crashes then stale disk data _may_ be exposed inside the file. But current
3280 * VFS code falls back into buffered path in that case so we are safe.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003281 */
Mingming Cao617ba132006-10-11 01:20:53 -07003282static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003283 const struct iovec *iov, loff_t offset,
3284 unsigned long nr_segs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003285{
3286 struct file *file = iocb->ki_filp;
3287 struct inode *inode = file->f_mapping->host;
Mingming Cao617ba132006-10-11 01:20:53 -07003288 struct ext4_inode_info *ei = EXT4_I(inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003289 handle_t *handle;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003290 ssize_t ret;
3291 int orphan = 0;
3292 size_t count = iov_length(iov, nr_segs);
3293
3294 if (rw == WRITE) {
3295 loff_t final_size = offset + count;
3296
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003297 if (final_size > inode->i_size) {
Jan Kara7fb54092008-02-10 01:08:38 -05003298 /* Credits for sb + inode write */
3299 handle = ext4_journal_start(inode, 2);
3300 if (IS_ERR(handle)) {
3301 ret = PTR_ERR(handle);
3302 goto out;
3303 }
Mingming Cao617ba132006-10-11 01:20:53 -07003304 ret = ext4_orphan_add(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003305 if (ret) {
3306 ext4_journal_stop(handle);
3307 goto out;
3308 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003309 orphan = 1;
3310 ei->i_disksize = inode->i_size;
Jan Kara7fb54092008-02-10 01:08:38 -05003311 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003312 }
3313 }
3314
3315 ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
3316 offset, nr_segs,
Mingming Cao617ba132006-10-11 01:20:53 -07003317 ext4_get_block, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003318
Jan Kara7fb54092008-02-10 01:08:38 -05003319 if (orphan) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003320 int err;
3321
Jan Kara7fb54092008-02-10 01:08:38 -05003322 /* Credits for sb + inode write */
3323 handle = ext4_journal_start(inode, 2);
3324 if (IS_ERR(handle)) {
3325 /* This is really bad luck. We've written the data
3326 * but cannot extend i_size. Bail out and pretend
3327 * the write failed... */
3328 ret = PTR_ERR(handle);
3329 goto out;
3330 }
3331 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003332 ext4_orphan_del(handle, inode);
Jan Kara7fb54092008-02-10 01:08:38 -05003333 if (ret > 0) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003334 loff_t end = offset + ret;
3335 if (end > inode->i_size) {
3336 ei->i_disksize = end;
3337 i_size_write(inode, end);
3338 /*
3339 * We're going to return a positive `ret'
3340 * here due to non-zero-length I/O, so there's
3341 * no way of reporting error returns from
Mingming Cao617ba132006-10-11 01:20:53 -07003342 * ext4_mark_inode_dirty() to userspace. So
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003343 * ignore it.
3344 */
Mingming Cao617ba132006-10-11 01:20:53 -07003345 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003346 }
3347 }
Mingming Cao617ba132006-10-11 01:20:53 -07003348 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003349 if (ret == 0)
3350 ret = err;
3351 }
3352out:
3353 return ret;
3354}
3355
3356/*
Mingming Cao617ba132006-10-11 01:20:53 -07003357 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003358 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3359 * much here because ->set_page_dirty is called under VFS locks. The page is
3360 * not necessarily locked.
3361 *
3362 * We cannot just dirty the page and leave attached buffers clean, because the
3363 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3364 * or jbddirty because all the journalling code will explode.
3365 *
3366 * So what we do is to mark the page "pending dirty" and next time writepage
3367 * is called, propagate that into the buffers appropriately.
3368 */
Mingming Cao617ba132006-10-11 01:20:53 -07003369static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003370{
3371 SetPageChecked(page);
3372 return __set_page_dirty_nobuffers(page);
3373}
3374
Mingming Cao617ba132006-10-11 01:20:53 -07003375static const struct address_space_operations ext4_ordered_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003376 .readpage = ext4_readpage,
3377 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003378 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003379 .sync_page = block_sync_page,
3380 .write_begin = ext4_write_begin,
3381 .write_end = ext4_ordered_write_end,
3382 .bmap = ext4_bmap,
3383 .invalidatepage = ext4_invalidatepage,
3384 .releasepage = ext4_releasepage,
3385 .direct_IO = ext4_direct_IO,
3386 .migratepage = buffer_migrate_page,
3387 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003388};
3389
Mingming Cao617ba132006-10-11 01:20:53 -07003390static const struct address_space_operations ext4_writeback_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003391 .readpage = ext4_readpage,
3392 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003393 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003394 .sync_page = block_sync_page,
3395 .write_begin = ext4_write_begin,
3396 .write_end = ext4_writeback_write_end,
3397 .bmap = ext4_bmap,
3398 .invalidatepage = ext4_invalidatepage,
3399 .releasepage = ext4_releasepage,
3400 .direct_IO = ext4_direct_IO,
3401 .migratepage = buffer_migrate_page,
3402 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003403};
3404
Mingming Cao617ba132006-10-11 01:20:53 -07003405static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003406 .readpage = ext4_readpage,
3407 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003408 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003409 .sync_page = block_sync_page,
3410 .write_begin = ext4_write_begin,
3411 .write_end = ext4_journalled_write_end,
3412 .set_page_dirty = ext4_journalled_set_page_dirty,
3413 .bmap = ext4_bmap,
3414 .invalidatepage = ext4_invalidatepage,
3415 .releasepage = ext4_releasepage,
3416 .is_partially_uptodate = block_is_partially_uptodate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003417};
3418
Alex Tomas64769242008-07-11 19:27:31 -04003419static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003420 .readpage = ext4_readpage,
3421 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003422 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003423 .writepages = ext4_da_writepages,
3424 .sync_page = block_sync_page,
3425 .write_begin = ext4_da_write_begin,
3426 .write_end = ext4_da_write_end,
3427 .bmap = ext4_bmap,
3428 .invalidatepage = ext4_da_invalidatepage,
3429 .releasepage = ext4_releasepage,
3430 .direct_IO = ext4_direct_IO,
3431 .migratepage = buffer_migrate_page,
3432 .is_partially_uptodate = block_is_partially_uptodate,
Alex Tomas64769242008-07-11 19:27:31 -04003433};
3434
Mingming Cao617ba132006-10-11 01:20:53 -07003435void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003436{
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04003437 if (ext4_should_order_data(inode) &&
3438 test_opt(inode->i_sb, DELALLOC))
3439 inode->i_mapping->a_ops = &ext4_da_aops;
3440 else if (ext4_should_order_data(inode))
Mingming Cao617ba132006-10-11 01:20:53 -07003441 inode->i_mapping->a_ops = &ext4_ordered_aops;
Alex Tomas64769242008-07-11 19:27:31 -04003442 else if (ext4_should_writeback_data(inode) &&
3443 test_opt(inode->i_sb, DELALLOC))
3444 inode->i_mapping->a_ops = &ext4_da_aops;
Mingming Cao617ba132006-10-11 01:20:53 -07003445 else if (ext4_should_writeback_data(inode))
3446 inode->i_mapping->a_ops = &ext4_writeback_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003447 else
Mingming Cao617ba132006-10-11 01:20:53 -07003448 inode->i_mapping->a_ops = &ext4_journalled_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003449}
3450
3451/*
Mingming Cao617ba132006-10-11 01:20:53 -07003452 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003453 * up to the end of the block which corresponds to `from'.
3454 * This required during truncate. We need to physically zero the tail end
3455 * of that block so it doesn't yield old data if the file is later grown.
3456 */
Jan Karacf108bc2008-07-11 19:27:31 -04003457int ext4_block_truncate_page(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003458 struct address_space *mapping, loff_t from)
3459{
Mingming Cao617ba132006-10-11 01:20:53 -07003460 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003461 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003462 unsigned blocksize, length, pos;
3463 ext4_lblk_t iblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003464 struct inode *inode = mapping->host;
3465 struct buffer_head *bh;
Jan Karacf108bc2008-07-11 19:27:31 -04003466 struct page *page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003467 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003468
Theodore Ts'of4a01012009-07-05 22:08:16 -04003469 page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
3470 mapping_gfp_mask(mapping) & ~__GFP_FS);
Jan Karacf108bc2008-07-11 19:27:31 -04003471 if (!page)
3472 return -EINVAL;
3473
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003474 blocksize = inode->i_sb->s_blocksize;
3475 length = blocksize - (offset & (blocksize - 1));
3476 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
3477
3478 /*
3479 * For "nobh" option, we can only work if we don't need to
3480 * read-in the page - otherwise we create buffers to do the IO.
3481 */
3482 if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) &&
Mingming Cao617ba132006-10-11 01:20:53 -07003483 ext4_should_writeback_data(inode) && PageUptodate(page)) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003484 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003485 set_page_dirty(page);
3486 goto unlock;
3487 }
3488
3489 if (!page_has_buffers(page))
3490 create_empty_buffers(page, blocksize, 0);
3491
3492 /* Find the buffer that contains "offset" */
3493 bh = page_buffers(page);
3494 pos = blocksize;
3495 while (offset >= pos) {
3496 bh = bh->b_this_page;
3497 iblock++;
3498 pos += blocksize;
3499 }
3500
3501 err = 0;
3502 if (buffer_freed(bh)) {
3503 BUFFER_TRACE(bh, "freed: skip");
3504 goto unlock;
3505 }
3506
3507 if (!buffer_mapped(bh)) {
3508 BUFFER_TRACE(bh, "unmapped");
Mingming Cao617ba132006-10-11 01:20:53 -07003509 ext4_get_block(inode, iblock, bh, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003510 /* unmapped? It's a hole - nothing to do */
3511 if (!buffer_mapped(bh)) {
3512 BUFFER_TRACE(bh, "still unmapped");
3513 goto unlock;
3514 }
3515 }
3516
3517 /* Ok, it's mapped. Make sure it's up-to-date */
3518 if (PageUptodate(page))
3519 set_buffer_uptodate(bh);
3520
3521 if (!buffer_uptodate(bh)) {
3522 err = -EIO;
3523 ll_rw_block(READ, 1, &bh);
3524 wait_on_buffer(bh);
3525 /* Uhhuh. Read error. Complain and punt. */
3526 if (!buffer_uptodate(bh))
3527 goto unlock;
3528 }
3529
Mingming Cao617ba132006-10-11 01:20:53 -07003530 if (ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003531 BUFFER_TRACE(bh, "get write access");
Mingming Cao617ba132006-10-11 01:20:53 -07003532 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003533 if (err)
3534 goto unlock;
3535 }
3536
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003537 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003538
3539 BUFFER_TRACE(bh, "zeroed end of block");
3540
3541 err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003542 if (ext4_should_journal_data(inode)) {
Frank Mayhar03901312009-01-07 00:06:22 -05003543 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003544 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003545 if (ext4_should_order_data(inode))
Jan Kara678aaf42008-07-11 19:27:31 -04003546 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003547 mark_buffer_dirty(bh);
3548 }
3549
3550unlock:
3551 unlock_page(page);
3552 page_cache_release(page);
3553 return err;
3554}
3555
3556/*
3557 * Probably it should be a library function... search for first non-zero word
3558 * or memcmp with zero_page, whatever is better for particular architecture.
3559 * Linus?
3560 */
3561static inline int all_zeroes(__le32 *p, __le32 *q)
3562{
3563 while (p < q)
3564 if (*p++)
3565 return 0;
3566 return 1;
3567}
3568
3569/**
Mingming Cao617ba132006-10-11 01:20:53 -07003570 * ext4_find_shared - find the indirect blocks for partial truncation.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003571 * @inode: inode in question
3572 * @depth: depth of the affected branch
Mingming Cao617ba132006-10-11 01:20:53 -07003573 * @offsets: offsets of pointers in that branch (see ext4_block_to_path)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003574 * @chain: place to store the pointers to partial indirect blocks
3575 * @top: place to the (detached) top of branch
3576 *
Mingming Cao617ba132006-10-11 01:20:53 -07003577 * This is a helper function used by ext4_truncate().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003578 *
3579 * When we do truncate() we may have to clean the ends of several
3580 * indirect blocks but leave the blocks themselves alive. Block is
3581 * partially truncated if some data below the new i_size is refered
3582 * from it (and it is on the path to the first completely truncated
3583 * data block, indeed). We have to free the top of that path along
3584 * with everything to the right of the path. Since no allocation
Mingming Cao617ba132006-10-11 01:20:53 -07003585 * past the truncation point is possible until ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003586 * finishes, we may safely do the latter, but top of branch may
3587 * require special attention - pageout below the truncation point
3588 * might try to populate it.
3589 *
3590 * We atomically detach the top of branch from the tree, store the
3591 * block number of its root in *@top, pointers to buffer_heads of
3592 * partially truncated blocks - in @chain[].bh and pointers to
3593 * their last elements that should not be removed - in
3594 * @chain[].p. Return value is the pointer to last filled element
3595 * of @chain.
3596 *
3597 * The work left to caller to do the actual freeing of subtrees:
3598 * a) free the subtree starting from *@top
3599 * b) free the subtrees whose roots are stored in
3600 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
3601 * c) free the subtrees growing from the inode past the @chain[0].
3602 * (no partially truncated stuff there). */
3603
Mingming Cao617ba132006-10-11 01:20:53 -07003604static Indirect *ext4_find_shared(struct inode *inode, int depth,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003605 ext4_lblk_t offsets[4], Indirect chain[4],
3606 __le32 *top)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003607{
3608 Indirect *partial, *p;
3609 int k, err;
3610
3611 *top = 0;
3612 /* Make k index the deepest non-null offest + 1 */
3613 for (k = depth; k > 1 && !offsets[k-1]; k--)
3614 ;
Mingming Cao617ba132006-10-11 01:20:53 -07003615 partial = ext4_get_branch(inode, k, offsets, chain, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003616 /* Writer: pointers */
3617 if (!partial)
3618 partial = chain + k-1;
3619 /*
3620 * If the branch acquired continuation since we've looked at it -
3621 * fine, it should all survive and (new) top doesn't belong to us.
3622 */
3623 if (!partial->key && *partial->p)
3624 /* Writer: end */
3625 goto no_top;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003626 for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003627 ;
3628 /*
3629 * OK, we've found the last block that must survive. The rest of our
3630 * branch should be detached before unlocking. However, if that rest
3631 * of branch is all ours and does not grow immediately from the inode
3632 * it's easier to cheat and just decrement partial->p.
3633 */
3634 if (p == chain + k - 1 && p > chain) {
3635 p->p--;
3636 } else {
3637 *top = *p->p;
Mingming Cao617ba132006-10-11 01:20:53 -07003638 /* Nope, don't do this in ext4. Must leave the tree intact */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003639#if 0
3640 *p->p = 0;
3641#endif
3642 }
3643 /* Writer: end */
3644
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003645 while (partial > p) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003646 brelse(partial->bh);
3647 partial--;
3648 }
3649no_top:
3650 return partial;
3651}
3652
3653/*
3654 * Zero a number of block pointers in either an inode or an indirect block.
3655 * If we restart the transaction we must again get write access to the
3656 * indirect block for further modification.
3657 *
3658 * We release `count' blocks on disk, but (last - first) may be greater
3659 * than `count' because there can be holes in there.
3660 */
Mingming Cao617ba132006-10-11 01:20:53 -07003661static void ext4_clear_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003662 struct buffer_head *bh,
3663 ext4_fsblk_t block_to_free,
3664 unsigned long count, __le32 *first,
3665 __le32 *last)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003666{
3667 __le32 *p;
3668 if (try_to_extend_transaction(handle, inode)) {
3669 if (bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05003670 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
3671 ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003672 }
Mingming Cao617ba132006-10-11 01:20:53 -07003673 ext4_mark_inode_dirty(handle, inode);
Jan Kara487caee2009-08-17 22:17:20 -04003674 ext4_truncate_restart_trans(handle, inode,
3675 blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003676 if (bh) {
3677 BUFFER_TRACE(bh, "retaking write access");
Mingming Cao617ba132006-10-11 01:20:53 -07003678 ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003679 }
3680 }
3681
3682 /*
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003683 * Any buffers which are on the journal will be in memory. We
3684 * find them on the hash table so jbd2_journal_revoke() will
3685 * run jbd2_journal_forget() on them. We've already detached
3686 * each block from the file, so bforget() in
3687 * jbd2_journal_forget() should be safe.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003688 *
Mingming Caodab291a2006-10-11 01:21:01 -07003689 * AKPM: turn on bforget in jbd2_journal_forget()!!!
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003690 */
3691 for (p = first; p < last; p++) {
3692 u32 nr = le32_to_cpu(*p);
3693 if (nr) {
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003694 struct buffer_head *tbh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003695
3696 *p = 0;
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003697 tbh = sb_find_get_block(inode->i_sb, nr);
3698 ext4_forget(handle, 0, inode, tbh, nr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003699 }
3700 }
3701
Alex Tomasc9de5602008-01-29 00:19:52 -05003702 ext4_free_blocks(handle, inode, block_to_free, count, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003703}
3704
3705/**
Mingming Cao617ba132006-10-11 01:20:53 -07003706 * ext4_free_data - free a list of data blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003707 * @handle: handle for this transaction
3708 * @inode: inode we are dealing with
3709 * @this_bh: indirect buffer_head which contains *@first and *@last
3710 * @first: array of block numbers
3711 * @last: points immediately past the end of array
3712 *
3713 * We are freeing all blocks refered from that array (numbers are stored as
3714 * little-endian 32-bit) and updating @inode->i_blocks appropriately.
3715 *
3716 * We accumulate contiguous runs of blocks to free. Conveniently, if these
3717 * blocks are contiguous then releasing them at one time will only affect one
3718 * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
3719 * actually use a lot of journal space.
3720 *
3721 * @this_bh will be %NULL if @first and @last point into the inode's direct
3722 * block pointers.
3723 */
Mingming Cao617ba132006-10-11 01:20:53 -07003724static void ext4_free_data(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003725 struct buffer_head *this_bh,
3726 __le32 *first, __le32 *last)
3727{
Mingming Cao617ba132006-10-11 01:20:53 -07003728 ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003729 unsigned long count = 0; /* Number of blocks in the run */
3730 __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
3731 corresponding to
3732 block_to_free */
Mingming Cao617ba132006-10-11 01:20:53 -07003733 ext4_fsblk_t nr; /* Current block # */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003734 __le32 *p; /* Pointer into inode/ind
3735 for current block */
3736 int err;
3737
3738 if (this_bh) { /* For indirect block */
3739 BUFFER_TRACE(this_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003740 err = ext4_journal_get_write_access(handle, this_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003741 /* Important: if we can't update the indirect pointers
3742 * to the blocks, we can't free them. */
3743 if (err)
3744 return;
3745 }
3746
3747 for (p = first; p < last; p++) {
3748 nr = le32_to_cpu(*p);
3749 if (nr) {
3750 /* accumulate blocks to free if they're contiguous */
3751 if (count == 0) {
3752 block_to_free = nr;
3753 block_to_free_p = p;
3754 count = 1;
3755 } else if (nr == block_to_free + count) {
3756 count++;
3757 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003758 ext4_clear_blocks(handle, inode, this_bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003759 block_to_free,
3760 count, block_to_free_p, p);
3761 block_to_free = nr;
3762 block_to_free_p = p;
3763 count = 1;
3764 }
3765 }
3766 }
3767
3768 if (count > 0)
Mingming Cao617ba132006-10-11 01:20:53 -07003769 ext4_clear_blocks(handle, inode, this_bh, block_to_free,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003770 count, block_to_free_p, p);
3771
3772 if (this_bh) {
Frank Mayhar03901312009-01-07 00:06:22 -05003773 BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata");
Duane Griffin71dc8fb2008-07-11 19:27:31 -04003774
3775 /*
3776 * The buffer head should have an attached journal head at this
3777 * point. However, if the data is corrupted and an indirect
3778 * block pointed to itself, it would have been detached when
3779 * the block was cleared. Check for this instead of OOPSing.
3780 */
Theodore Ts'oe7f07962009-01-20 09:50:19 -05003781 if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
Frank Mayhar03901312009-01-07 00:06:22 -05003782 ext4_handle_dirty_metadata(handle, inode, this_bh);
Duane Griffin71dc8fb2008-07-11 19:27:31 -04003783 else
3784 ext4_error(inode->i_sb, __func__,
3785 "circular indirect block detected, "
3786 "inode=%lu, block=%llu",
3787 inode->i_ino,
3788 (unsigned long long) this_bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003789 }
3790}
3791
3792/**
Mingming Cao617ba132006-10-11 01:20:53 -07003793 * ext4_free_branches - free an array of branches
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003794 * @handle: JBD handle for this transaction
3795 * @inode: inode we are dealing with
3796 * @parent_bh: the buffer_head which contains *@first and *@last
3797 * @first: array of block numbers
3798 * @last: pointer immediately past the end of array
3799 * @depth: depth of the branches to free
3800 *
3801 * We are freeing all blocks refered from these branches (numbers are
3802 * stored as little-endian 32-bit) and updating @inode->i_blocks
3803 * appropriately.
3804 */
Mingming Cao617ba132006-10-11 01:20:53 -07003805static void ext4_free_branches(handle_t *handle, struct inode *inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003806 struct buffer_head *parent_bh,
3807 __le32 *first, __le32 *last, int depth)
3808{
Mingming Cao617ba132006-10-11 01:20:53 -07003809 ext4_fsblk_t nr;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003810 __le32 *p;
3811
Frank Mayhar03901312009-01-07 00:06:22 -05003812 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003813 return;
3814
3815 if (depth--) {
3816 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07003817 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003818 p = last;
3819 while (--p >= first) {
3820 nr = le32_to_cpu(*p);
3821 if (!nr)
3822 continue; /* A hole */
3823
3824 /* Go read the buffer for the next level down */
3825 bh = sb_bread(inode->i_sb, nr);
3826
3827 /*
3828 * A read failure? Report error and clear slot
3829 * (should be rare).
3830 */
3831 if (!bh) {
Mingming Cao617ba132006-10-11 01:20:53 -07003832 ext4_error(inode->i_sb, "ext4_free_branches",
Mingming Cao2ae02102006-10-11 01:21:11 -07003833 "Read failure, inode=%lu, block=%llu",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003834 inode->i_ino, nr);
3835 continue;
3836 }
3837
3838 /* This zaps the entire block. Bottom up. */
3839 BUFFER_TRACE(bh, "free child branches");
Mingming Cao617ba132006-10-11 01:20:53 -07003840 ext4_free_branches(handle, inode, bh,
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003841 (__le32 *) bh->b_data,
3842 (__le32 *) bh->b_data + addr_per_block,
3843 depth);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003844
3845 /*
3846 * We've probably journalled the indirect block several
3847 * times during the truncate. But it's no longer
3848 * needed and we now drop it from the transaction via
Mingming Caodab291a2006-10-11 01:21:01 -07003849 * jbd2_journal_revoke().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003850 *
3851 * That's easy if it's exclusively part of this
3852 * transaction. But if it's part of the committing
Mingming Caodab291a2006-10-11 01:21:01 -07003853 * transaction then jbd2_journal_forget() will simply
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003854 * brelse() it. That means that if the underlying
Mingming Cao617ba132006-10-11 01:20:53 -07003855 * block is reallocated in ext4_get_block(),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003856 * unmap_underlying_metadata() will find this block
3857 * and will try to get rid of it. damn, damn.
3858 *
3859 * If this block has already been committed to the
3860 * journal, a revoke record will be written. And
3861 * revoke records must be emitted *before* clearing
3862 * this block's bit in the bitmaps.
3863 */
Mingming Cao617ba132006-10-11 01:20:53 -07003864 ext4_forget(handle, 1, inode, bh, bh->b_blocknr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003865
3866 /*
3867 * Everything below this this pointer has been
3868 * released. Now let this top-of-subtree go.
3869 *
3870 * We want the freeing of this indirect block to be
3871 * atomic in the journal with the updating of the
3872 * bitmap block which owns it. So make some room in
3873 * the journal.
3874 *
3875 * We zero the parent pointer *after* freeing its
3876 * pointee in the bitmaps, so if extend_transaction()
3877 * for some reason fails to put the bitmap changes and
3878 * the release into the same transaction, recovery
3879 * will merely complain about releasing a free block,
3880 * rather than leaking blocks.
3881 */
Frank Mayhar03901312009-01-07 00:06:22 -05003882 if (ext4_handle_is_aborted(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003883 return;
3884 if (try_to_extend_transaction(handle, inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003885 ext4_mark_inode_dirty(handle, inode);
Jan Kara487caee2009-08-17 22:17:20 -04003886 ext4_truncate_restart_trans(handle, inode,
3887 blocks_for_truncate(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003888 }
3889
Alex Tomasc9de5602008-01-29 00:19:52 -05003890 ext4_free_blocks(handle, inode, nr, 1, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003891
3892 if (parent_bh) {
3893 /*
3894 * The block which we have just freed is
3895 * pointed to by an indirect block: journal it
3896 */
3897 BUFFER_TRACE(parent_bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07003898 if (!ext4_journal_get_write_access(handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003899 parent_bh)){
3900 *p = 0;
3901 BUFFER_TRACE(parent_bh,
Frank Mayhar03901312009-01-07 00:06:22 -05003902 "call ext4_handle_dirty_metadata");
3903 ext4_handle_dirty_metadata(handle,
3904 inode,
3905 parent_bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003906 }
3907 }
3908 }
3909 } else {
3910 /* We have reached the bottom of the tree. */
3911 BUFFER_TRACE(parent_bh, "free data blocks");
Mingming Cao617ba132006-10-11 01:20:53 -07003912 ext4_free_data(handle, inode, parent_bh, first, last);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003913 }
3914}
3915
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003916int ext4_can_truncate(struct inode *inode)
3917{
3918 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3919 return 0;
3920 if (S_ISREG(inode->i_mode))
3921 return 1;
3922 if (S_ISDIR(inode->i_mode))
3923 return 1;
3924 if (S_ISLNK(inode->i_mode))
3925 return !ext4_inode_is_fast_symlink(inode);
3926 return 0;
3927}
3928
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003929/*
Mingming Cao617ba132006-10-11 01:20:53 -07003930 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003931 *
Mingming Cao617ba132006-10-11 01:20:53 -07003932 * We block out ext4_get_block() block instantiations across the entire
3933 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003934 * simultaneously on behalf of the same inode.
3935 *
3936 * As we work through the truncate and commmit bits of it to the journal there
3937 * is one core, guiding principle: the file's tree must always be consistent on
3938 * disk. We must be able to restart the truncate after a crash.
3939 *
3940 * The file's tree may be transiently inconsistent in memory (although it
3941 * probably isn't), but whenever we close off and commit a journal transaction,
3942 * the contents of (the filesystem + the journal) must be consistent and
3943 * restartable. It's pretty simple, really: bottom up, right to left (although
3944 * left-to-right works OK too).
3945 *
3946 * Note that at recovery time, journal replay occurs *before* the restart of
3947 * truncate against the orphan inode list.
3948 *
3949 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07003950 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003951 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07003952 * ext4_truncate() to have another go. So there will be instantiated blocks
3953 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003954 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07003955 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003956 */
Mingming Cao617ba132006-10-11 01:20:53 -07003957void ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003958{
3959 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -07003960 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003961 __le32 *i_data = ei->i_data;
Mingming Cao617ba132006-10-11 01:20:53 -07003962 int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003963 struct address_space *mapping = inode->i_mapping;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003964 ext4_lblk_t offsets[4];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003965 Indirect chain[4];
3966 Indirect *partial;
3967 __le32 nr = 0;
3968 int n;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003969 ext4_lblk_t last_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003970 unsigned blocksize = inode->i_sb->s_blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003971
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003972 if (!ext4_can_truncate(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003973 return;
3974
Theodore Ts'o0eab9282009-06-09 09:54:40 -04003975 if (ei->i_disksize && inode->i_size == 0 &&
3976 !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o7d8f9f72009-02-24 08:21:14 -05003977 ei->i_state |= EXT4_STATE_DA_ALLOC_CLOSE;
3978
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003979 if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) {
Jan Karacf108bc2008-07-11 19:27:31 -04003980 ext4_ext_truncate(inode);
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05003981 return;
3982 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003983
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003984 handle = start_transaction(inode);
Jan Karacf108bc2008-07-11 19:27:31 -04003985 if (IS_ERR(handle))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003986 return; /* AKPM: return what? */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003987
3988 last_block = (inode->i_size + blocksize-1)
Mingming Cao617ba132006-10-11 01:20:53 -07003989 >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003990
Jan Karacf108bc2008-07-11 19:27:31 -04003991 if (inode->i_size & (blocksize - 1))
3992 if (ext4_block_truncate_page(handle, mapping, inode->i_size))
3993 goto out_stop;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003994
Mingming Cao617ba132006-10-11 01:20:53 -07003995 n = ext4_block_to_path(inode, last_block, offsets, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003996 if (n == 0)
3997 goto out_stop; /* error */
3998
3999 /*
4000 * OK. This truncate is going to happen. We add the inode to the
4001 * orphan list, so that if this truncate spans multiple transactions,
4002 * and we crash, we will resume the truncate when the filesystem
4003 * recovers. It also marks the inode dirty, to catch the new size.
4004 *
4005 * Implication: the file must always be in a sane, consistent
4006 * truncatable state while each transaction commits.
4007 */
Mingming Cao617ba132006-10-11 01:20:53 -07004008 if (ext4_orphan_add(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004009 goto out_stop;
4010
4011 /*
Mingming Cao632eaea2008-07-11 19:27:31 -04004012 * From here we block out all ext4_get_block() callers who want to
4013 * modify the block allocation tree.
4014 */
4015 down_write(&ei->i_data_sem);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04004016
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004017 ext4_discard_preallocations(inode);
Theodore Ts'ob4df2032008-08-13 21:44:34 -04004018
Mingming Cao632eaea2008-07-11 19:27:31 -04004019 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004020 * The orphan list entry will now protect us from any crash which
4021 * occurs before the truncate completes, so it is now safe to propagate
4022 * the new, shorter inode size (held for now in i_size) into the
4023 * on-disk inode. We do this via i_disksize, which is the value which
Mingming Cao617ba132006-10-11 01:20:53 -07004024 * ext4 *really* writes onto the disk inode.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004025 */
4026 ei->i_disksize = inode->i_size;
4027
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004028 if (n == 1) { /* direct blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07004029 ext4_free_data(handle, inode, NULL, i_data+offsets[0],
4030 i_data + EXT4_NDIR_BLOCKS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004031 goto do_indirects;
4032 }
4033
Mingming Cao617ba132006-10-11 01:20:53 -07004034 partial = ext4_find_shared(inode, n, offsets, chain, &nr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004035 /* Kill the top of shared branch (not detached) */
4036 if (nr) {
4037 if (partial == chain) {
4038 /* Shared branch grows from the inode */
Mingming Cao617ba132006-10-11 01:20:53 -07004039 ext4_free_branches(handle, inode, NULL,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004040 &nr, &nr+1, (chain+n-1) - partial);
4041 *partial->p = 0;
4042 /*
4043 * We mark the inode dirty prior to restart,
4044 * and prior to stop. No need for it here.
4045 */
4046 } else {
4047 /* Shared branch grows from an indirect block */
4048 BUFFER_TRACE(partial->bh, "get_write_access");
Mingming Cao617ba132006-10-11 01:20:53 -07004049 ext4_free_branches(handle, inode, partial->bh,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004050 partial->p,
4051 partial->p+1, (chain+n-1) - partial);
4052 }
4053 }
4054 /* Clear the ends of indirect blocks on the shared branch */
4055 while (partial > chain) {
Mingming Cao617ba132006-10-11 01:20:53 -07004056 ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004057 (__le32*)partial->bh->b_data+addr_per_block,
4058 (chain+n-1) - partial);
4059 BUFFER_TRACE(partial->bh, "call brelse");
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004060 brelse(partial->bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004061 partial--;
4062 }
4063do_indirects:
4064 /* Kill the remaining (whole) subtrees */
4065 switch (offsets[0]) {
4066 default:
Mingming Cao617ba132006-10-11 01:20:53 -07004067 nr = i_data[EXT4_IND_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, 1);
4070 i_data[EXT4_IND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004071 }
Mingming Cao617ba132006-10-11 01:20:53 -07004072 case EXT4_IND_BLOCK:
4073 nr = i_data[EXT4_DIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004074 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004075 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
4076 i_data[EXT4_DIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004077 }
Mingming Cao617ba132006-10-11 01:20:53 -07004078 case EXT4_DIND_BLOCK:
4079 nr = i_data[EXT4_TIND_BLOCK];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004080 if (nr) {
Mingming Cao617ba132006-10-11 01:20:53 -07004081 ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
4082 i_data[EXT4_TIND_BLOCK] = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004083 }
Mingming Cao617ba132006-10-11 01:20:53 -07004084 case EXT4_TIND_BLOCK:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004085 ;
4086 }
4087
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004088 up_write(&ei->i_data_sem);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004089 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07004090 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004091
4092 /*
4093 * In a multi-transaction truncate, we only make the final transaction
4094 * synchronous
4095 */
4096 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05004097 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004098out_stop:
4099 /*
4100 * If this was a simple ftruncate(), and the file will remain alive
4101 * then we need to clear up the orphan record which we created above.
4102 * However, if this was a real unlink then we were called by
Mingming Cao617ba132006-10-11 01:20:53 -07004103 * ext4_delete_inode(), and we allow that function to clean up the
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004104 * orphan info for us.
4105 */
4106 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07004107 ext4_orphan_del(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004108
Mingming Cao617ba132006-10-11 01:20:53 -07004109 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004110}
4111
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004112/*
Mingming Cao617ba132006-10-11 01:20:53 -07004113 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004114 * underlying buffer_head on success. If 'in_mem' is true, we have all
4115 * data in memory that is needed to recreate the on-disk version of this
4116 * inode.
4117 */
Mingming Cao617ba132006-10-11 01:20:53 -07004118static int __ext4_get_inode_loc(struct inode *inode,
4119 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004120{
Theodore Ts'o240799c2008-10-09 23:53:47 -04004121 struct ext4_group_desc *gdp;
4122 struct buffer_head *bh;
4123 struct super_block *sb = inode->i_sb;
4124 ext4_fsblk_t block;
4125 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004126
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004127 iloc->bh = NULL;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004128 if (!ext4_valid_inum(sb, inode->i_ino))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004129 return -EIO;
4130
Theodore Ts'o240799c2008-10-09 23:53:47 -04004131 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
4132 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
4133 if (!gdp)
4134 return -EIO;
4135
4136 /*
4137 * Figure out the offset within the block group inode table
4138 */
4139 inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb));
4140 inode_offset = ((inode->i_ino - 1) %
4141 EXT4_INODES_PER_GROUP(sb));
4142 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4143 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4144
4145 bh = sb_getblk(sb, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004146 if (!bh) {
Theodore Ts'o240799c2008-10-09 23:53:47 -04004147 ext4_error(sb, "ext4_get_inode_loc", "unable to read "
4148 "inode block - inode=%lu, block=%llu",
4149 inode->i_ino, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004150 return -EIO;
4151 }
4152 if (!buffer_uptodate(bh)) {
4153 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04004154
4155 /*
4156 * If the buffer has the write error flag, we have failed
4157 * to write out another inode in the same block. In this
4158 * case, we don't have to read the block because we may
4159 * read the old inode data successfully.
4160 */
4161 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
4162 set_buffer_uptodate(bh);
4163
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004164 if (buffer_uptodate(bh)) {
4165 /* someone brought it uptodate while we waited */
4166 unlock_buffer(bh);
4167 goto has_buffer;
4168 }
4169
4170 /*
4171 * If we have all information of the inode in memory and this
4172 * is the only valid inode in the block, we need not read the
4173 * block.
4174 */
4175 if (in_mem) {
4176 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004177 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004178
Theodore Ts'o240799c2008-10-09 23:53:47 -04004179 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004180
4181 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004182 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004183 if (!bitmap_bh)
4184 goto make_io;
4185
4186 /*
4187 * If the inode bitmap isn't in cache then the
4188 * optimisation may end up performing two reads instead
4189 * of one, so skip it.
4190 */
4191 if (!buffer_uptodate(bitmap_bh)) {
4192 brelse(bitmap_bh);
4193 goto make_io;
4194 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04004195 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004196 if (i == inode_offset)
4197 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07004198 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004199 break;
4200 }
4201 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004202 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004203 /* all other inodes are free, so skip I/O */
4204 memset(bh->b_data, 0, bh->b_size);
4205 set_buffer_uptodate(bh);
4206 unlock_buffer(bh);
4207 goto has_buffer;
4208 }
4209 }
4210
4211make_io:
4212 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04004213 * If we need to do any I/O, try to pre-readahead extra
4214 * blocks from the inode table.
4215 */
4216 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4217 ext4_fsblk_t b, end, table;
4218 unsigned num;
4219
4220 table = ext4_inode_table(sb, gdp);
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004221 /* s_inode_readahead_blks is always a power of 2 */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004222 b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
4223 if (table > b)
4224 b = table;
4225 end = b + EXT4_SB(sb)->s_inode_readahead_blks;
4226 num = EXT4_INODES_PER_GROUP(sb);
4227 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4228 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004229 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004230 table += num / inodes_per_block;
4231 if (end > table)
4232 end = table;
4233 while (b <= end)
4234 sb_breadahead(sb, b++);
4235 }
4236
4237 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004238 * There are other valid inodes in the buffer, this inode
4239 * has in-inode xattrs, or we don't have this inode in memory.
4240 * Read the block from disk.
4241 */
4242 get_bh(bh);
4243 bh->b_end_io = end_buffer_read_sync;
4244 submit_bh(READ_META, bh);
4245 wait_on_buffer(bh);
4246 if (!buffer_uptodate(bh)) {
Theodore Ts'o240799c2008-10-09 23:53:47 -04004247 ext4_error(sb, __func__,
4248 "unable to read inode block - inode=%lu, "
4249 "block=%llu", inode->i_ino, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004250 brelse(bh);
4251 return -EIO;
4252 }
4253 }
4254has_buffer:
4255 iloc->bh = bh;
4256 return 0;
4257}
4258
Mingming Cao617ba132006-10-11 01:20:53 -07004259int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004260{
4261 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07004262 return __ext4_get_inode_loc(inode, iloc,
4263 !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004264}
4265
Mingming Cao617ba132006-10-11 01:20:53 -07004266void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004267{
Mingming Cao617ba132006-10-11 01:20:53 -07004268 unsigned int flags = EXT4_I(inode)->i_flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004269
4270 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
Mingming Cao617ba132006-10-11 01:20:53 -07004271 if (flags & EXT4_SYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004272 inode->i_flags |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07004273 if (flags & EXT4_APPEND_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004274 inode->i_flags |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07004275 if (flags & EXT4_IMMUTABLE_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004276 inode->i_flags |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07004277 if (flags & EXT4_NOATIME_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004278 inode->i_flags |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004279 if (flags & EXT4_DIRSYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004280 inode->i_flags |= S_DIRSYNC;
4281}
4282
Jan Karaff9ddf72007-07-18 09:24:20 -04004283/* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4284void ext4_get_inode_flags(struct ext4_inode_info *ei)
4285{
4286 unsigned int flags = ei->vfs_inode.i_flags;
4287
4288 ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
4289 EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL);
4290 if (flags & S_SYNC)
4291 ei->i_flags |= EXT4_SYNC_FL;
4292 if (flags & S_APPEND)
4293 ei->i_flags |= EXT4_APPEND_FL;
4294 if (flags & S_IMMUTABLE)
4295 ei->i_flags |= EXT4_IMMUTABLE_FL;
4296 if (flags & S_NOATIME)
4297 ei->i_flags |= EXT4_NOATIME_FL;
4298 if (flags & S_DIRSYNC)
4299 ei->i_flags |= EXT4_DIRSYNC_FL;
4300}
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004301
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004302static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004303 struct ext4_inode_info *ei)
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004304{
4305 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004306 struct inode *inode = &(ei->vfs_inode);
4307 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004308
4309 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4310 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
4311 /* we are using combined 48 bit field */
4312 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4313 le32_to_cpu(raw_inode->i_blocks_lo);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004314 if (ei->i_flags & EXT4_HUGE_FILE_FL) {
4315 /* i_blocks represent file system block size */
4316 return i_blocks << (inode->i_blkbits - 9);
4317 } else {
4318 return i_blocks;
4319 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004320 } else {
4321 return le32_to_cpu(raw_inode->i_blocks_lo);
4322 }
4323}
Jan Karaff9ddf72007-07-18 09:24:20 -04004324
David Howells1d1fe1e2008-02-07 00:15:37 -08004325struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004326{
Mingming Cao617ba132006-10-11 01:20:53 -07004327 struct ext4_iloc iloc;
4328 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004329 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004330 struct buffer_head *bh;
David Howells1d1fe1e2008-02-07 00:15:37 -08004331 struct inode *inode;
4332 long ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004333 int block;
4334
David Howells1d1fe1e2008-02-07 00:15:37 -08004335 inode = iget_locked(sb, ino);
4336 if (!inode)
4337 return ERR_PTR(-ENOMEM);
4338 if (!(inode->i_state & I_NEW))
4339 return inode;
4340
4341 ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004342
David Howells1d1fe1e2008-02-07 00:15:37 -08004343 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4344 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004345 goto bad_inode;
4346 bh = iloc.bh;
Mingming Cao617ba132006-10-11 01:20:53 -07004347 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004348 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
4349 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4350 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004351 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004352 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4353 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
4354 }
4355 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004356
4357 ei->i_state = 0;
4358 ei->i_dir_start_lookup = 0;
4359 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4360 /* We now have enough fields to check if the inode was active or not.
4361 * This is needed because nfsd might try to access dead inodes
4362 * the test is that same one that e2fsck uses
4363 * NeilBrown 1999oct15
4364 */
4365 if (inode->i_nlink == 0) {
4366 if (inode->i_mode == 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07004367 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004368 /* this inode is deleted */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004369 brelse(bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004370 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004371 goto bad_inode;
4372 }
4373 /* The only unlinked inodes we let through here have
4374 * valid i_mode and are being read by the orphan
4375 * recovery code: that's fine, we're about to complete
4376 * the process of deleting those. */
4377 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004378 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004379 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004380 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Theodore Ts'oa9e81742009-04-24 16:11:18 -04004381 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004382 ei->i_file_acl |=
4383 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004384 inode->i_size = ext4_isize(raw_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004385 ei->i_disksize = inode->i_size;
4386 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4387 ei->i_block_group = iloc.block_group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04004388 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004389 /*
4390 * NOTE! The in-memory inode i_data array is in little-endian order
4391 * even on big-endian machines: we do NOT byteswap the block numbers!
4392 */
Mingming Cao617ba132006-10-11 01:20:53 -07004393 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004394 ei->i_data[block] = raw_inode->i_block[block];
4395 INIT_LIST_HEAD(&ei->i_orphan);
4396
Eric Sandeen0040d982008-02-05 22:36:43 -05004397 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004398 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
Mingming Cao617ba132006-10-11 01:20:53 -07004399 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004400 EXT4_INODE_SIZE(inode->i_sb)) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004401 brelse(bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004402 ret = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004403 goto bad_inode;
Kirill Korotaeve5d28612007-06-23 17:16:51 -07004404 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004405 if (ei->i_extra_isize == 0) {
4406 /* The extra space is currently unused. Use it. */
Mingming Cao617ba132006-10-11 01:20:53 -07004407 ei->i_extra_isize = sizeof(struct ext4_inode) -
4408 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004409 } else {
4410 __le32 *magic = (void *)raw_inode +
Mingming Cao617ba132006-10-11 01:20:53 -07004411 EXT4_GOOD_OLD_INODE_SIZE +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004412 ei->i_extra_isize;
Mingming Cao617ba132006-10-11 01:20:53 -07004413 if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC))
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004414 ei->i_state |= EXT4_STATE_XATTR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004415 }
4416 } else
4417 ei->i_extra_isize = 0;
4418
Kalpak Shahef7f3832007-07-18 09:15:20 -04004419 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4420 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4421 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4422 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4423
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004424 inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
4425 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4426 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4427 inode->i_version |=
4428 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4429 }
4430
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004431 ret = 0;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004432 if (ei->i_file_acl &&
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004433 ((ei->i_file_acl <
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004434 (le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) +
4435 EXT4_SB(sb)->s_gdb_count)) ||
4436 (ei->i_file_acl >= ext4_blocks_count(EXT4_SB(sb)->s_es)))) {
4437 ext4_error(sb, __func__,
4438 "bad extended attribute block %llu in inode #%lu",
4439 ei->i_file_acl, inode->i_ino);
4440 ret = -EIO;
4441 goto bad_inode;
4442 } else if (ei->i_flags & EXT4_EXTENTS_FL) {
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004443 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4444 (S_ISLNK(inode->i_mode) &&
4445 !ext4_inode_is_fast_symlink(inode)))
4446 /* Validate extent which is part of inode */
4447 ret = ext4_ext_check_inode(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004448 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004449 (S_ISLNK(inode->i_mode) &&
4450 !ext4_inode_is_fast_symlink(inode))) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004451 /* Validate block references which are part of inode */
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004452 ret = ext4_check_inode_blockref(inode);
4453 }
4454 if (ret) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004455 brelse(bh);
4456 goto bad_inode;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -04004457 }
4458
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004459 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004460 inode->i_op = &ext4_file_inode_operations;
4461 inode->i_fop = &ext4_file_operations;
4462 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004463 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004464 inode->i_op = &ext4_dir_inode_operations;
4465 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004466 } else if (S_ISLNK(inode->i_mode)) {
Duane Griffine83c1392008-12-19 20:47:15 +00004467 if (ext4_inode_is_fast_symlink(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004468 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00004469 nd_terminate_link(ei->i_data, inode->i_size,
4470 sizeof(ei->i_data) - 1);
4471 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004472 inode->i_op = &ext4_symlink_inode_operations;
4473 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004474 }
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004475 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4476 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004477 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004478 if (raw_inode->i_block[0])
4479 init_special_inode(inode, inode->i_mode,
4480 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4481 else
4482 init_special_inode(inode, inode->i_mode,
4483 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004484 } else {
4485 brelse(bh);
4486 ret = -EIO;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004487 ext4_error(inode->i_sb, __func__,
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004488 "bogus i_mode (%o) for inode=%lu",
4489 inode->i_mode, inode->i_ino);
4490 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004491 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004492 brelse(iloc.bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004493 ext4_set_inode_flags(inode);
David Howells1d1fe1e2008-02-07 00:15:37 -08004494 unlock_new_inode(inode);
4495 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004496
4497bad_inode:
David Howells1d1fe1e2008-02-07 00:15:37 -08004498 iget_failed(inode);
4499 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004500}
4501
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004502static int ext4_inode_blocks_set(handle_t *handle,
4503 struct ext4_inode *raw_inode,
4504 struct ext4_inode_info *ei)
4505{
4506 struct inode *inode = &(ei->vfs_inode);
4507 u64 i_blocks = inode->i_blocks;
4508 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004509
4510 if (i_blocks <= ~0U) {
4511 /*
4512 * i_blocks can be represnted in a 32 bit variable
4513 * as multiple of 512 bytes
4514 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004515 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004516 raw_inode->i_blocks_high = 0;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004517 ei->i_flags &= ~EXT4_HUGE_FILE_FL;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004518 return 0;
4519 }
4520 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
4521 return -EFBIG;
4522
4523 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004524 /*
4525 * i_blocks can be represented in a 48 bit variable
4526 * as multiple of 512 bytes
4527 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004528 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004529 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004530 ei->i_flags &= ~EXT4_HUGE_FILE_FL;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004531 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004532 ei->i_flags |= EXT4_HUGE_FILE_FL;
4533 /* i_block is stored in file system block size */
4534 i_blocks = i_blocks >> (inode->i_blkbits - 9);
4535 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4536 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004537 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004538 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004539}
4540
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004541/*
4542 * Post the struct inode info into an on-disk inode location in the
4543 * buffer-cache. This gobbles the caller's reference to the
4544 * buffer_head in the inode location struct.
4545 *
4546 * The caller must have write access to iloc->bh.
4547 */
Mingming Cao617ba132006-10-11 01:20:53 -07004548static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004549 struct inode *inode,
Mingming Cao617ba132006-10-11 01:20:53 -07004550 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004551{
Mingming Cao617ba132006-10-11 01:20:53 -07004552 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4553 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004554 struct buffer_head *bh = iloc->bh;
4555 int err = 0, rc, block;
4556
4557 /* For fields not not tracking in the in-memory inode,
4558 * initialise them to zero for new inodes. */
Mingming Cao617ba132006-10-11 01:20:53 -07004559 if (ei->i_state & EXT4_STATE_NEW)
4560 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004561
Jan Karaff9ddf72007-07-18 09:24:20 -04004562 ext4_get_inode_flags(ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004563 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004564 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004565 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
4566 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
4567/*
4568 * Fix up interoperability with old kernels. Otherwise, old inodes get
4569 * re-used with the upper 16 bits of the uid/gid intact
4570 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004571 if (!ei->i_dtime) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004572 raw_inode->i_uid_high =
4573 cpu_to_le16(high_16_bits(inode->i_uid));
4574 raw_inode->i_gid_high =
4575 cpu_to_le16(high_16_bits(inode->i_gid));
4576 } else {
4577 raw_inode->i_uid_high = 0;
4578 raw_inode->i_gid_high = 0;
4579 }
4580 } else {
4581 raw_inode->i_uid_low =
4582 cpu_to_le16(fs_high2lowuid(inode->i_uid));
4583 raw_inode->i_gid_low =
4584 cpu_to_le16(fs_high2lowgid(inode->i_gid));
4585 raw_inode->i_uid_high = 0;
4586 raw_inode->i_gid_high = 0;
4587 }
4588 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004589
4590 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4591 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4592 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4593 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4594
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004595 if (ext4_inode_blocks_set(handle, raw_inode, ei))
4596 goto out_brelse;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004597 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -04004598 /* clear the migrate flag in the raw_inode */
4599 raw_inode->i_flags = cpu_to_le32(ei->i_flags & ~EXT4_EXT_MIGRATE);
Mingming Cao9b8f1f02006-10-11 01:21:13 -07004600 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
4601 cpu_to_le32(EXT4_OS_HURD))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004602 raw_inode->i_file_acl_high =
4603 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004604 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004605 ext4_isize_set(raw_inode, ei->i_disksize);
4606 if (ei->i_disksize > 0x7fffffffULL) {
4607 struct super_block *sb = inode->i_sb;
4608 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
4609 EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
4610 EXT4_SB(sb)->s_es->s_rev_level ==
4611 cpu_to_le32(EXT4_GOOD_OLD_REV)) {
4612 /* If this is the first large file
4613 * created, add a flag to the superblock.
4614 */
4615 err = ext4_journal_get_write_access(handle,
4616 EXT4_SB(sb)->s_sbh);
4617 if (err)
4618 goto out_brelse;
4619 ext4_update_dynamic_rev(sb);
4620 EXT4_SET_RO_COMPAT_FEATURE(sb,
Mingming Cao617ba132006-10-11 01:20:53 -07004621 EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004622 sb->s_dirt = 1;
Frank Mayhar03901312009-01-07 00:06:22 -05004623 ext4_handle_sync(handle);
4624 err = ext4_handle_dirty_metadata(handle, inode,
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004625 EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004626 }
4627 }
4628 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
4629 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4630 if (old_valid_dev(inode->i_rdev)) {
4631 raw_inode->i_block[0] =
4632 cpu_to_le32(old_encode_dev(inode->i_rdev));
4633 raw_inode->i_block[1] = 0;
4634 } else {
4635 raw_inode->i_block[0] = 0;
4636 raw_inode->i_block[1] =
4637 cpu_to_le32(new_encode_dev(inode->i_rdev));
4638 raw_inode->i_block[2] = 0;
4639 }
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004640 } else
4641 for (block = 0; block < EXT4_N_BLOCKS; block++)
4642 raw_inode->i_block[block] = ei->i_data[block];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004643
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004644 raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
4645 if (ei->i_extra_isize) {
4646 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4647 raw_inode->i_version_hi =
4648 cpu_to_le32(inode->i_version >> 32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004649 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004650 }
4651
Frank Mayhar03901312009-01-07 00:06:22 -05004652 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
4653 rc = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004654 if (!err)
4655 err = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07004656 ei->i_state &= ~EXT4_STATE_NEW;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004657
4658out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004659 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004660 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004661 return err;
4662}
4663
4664/*
Mingming Cao617ba132006-10-11 01:20:53 -07004665 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004666 *
4667 * We are called from a few places:
4668 *
4669 * - Within generic_file_write() for O_SYNC files.
4670 * Here, there will be no transaction running. We wait for any running
4671 * trasnaction to commit.
4672 *
4673 * - Within sys_sync(), kupdate and such.
4674 * We wait on commit, if tol to.
4675 *
4676 * - Within prune_icache() (PF_MEMALLOC == true)
4677 * Here we simply return. We can't afford to block kswapd on the
4678 * journal commit.
4679 *
4680 * In all cases it is actually safe for us to return without doing anything,
4681 * because the inode has been copied into a raw inode buffer in
Mingming Cao617ba132006-10-11 01:20:53 -07004682 * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004683 * knfsd.
4684 *
4685 * Note that we are absolutely dependent upon all inode dirtiers doing the
4686 * right thing: they *must* call mark_inode_dirty() after dirtying info in
4687 * which we are interested.
4688 *
4689 * It would be a bug for them to not do this. The code:
4690 *
4691 * mark_inode_dirty(inode)
4692 * stuff();
4693 * inode->i_size = expr;
4694 *
4695 * is in error because a kswapd-driven write_inode() could occur while
4696 * `stuff()' is running, and the new i_size will be lost. Plus the inode
4697 * will no longer be on the superblock's dirty inode list.
4698 */
Mingming Cao617ba132006-10-11 01:20:53 -07004699int ext4_write_inode(struct inode *inode, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004700{
4701 if (current->flags & PF_MEMALLOC)
4702 return 0;
4703
Mingming Cao617ba132006-10-11 01:20:53 -07004704 if (ext4_journal_current_handle()) {
Mingming Caob38bd332007-07-19 01:48:35 -07004705 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004706 dump_stack();
4707 return -EIO;
4708 }
4709
4710 if (!wait)
4711 return 0;
4712
Mingming Cao617ba132006-10-11 01:20:53 -07004713 return ext4_force_commit(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004714}
4715
4716/*
Mingming Cao617ba132006-10-11 01:20:53 -07004717 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004718 *
4719 * Called from notify_change.
4720 *
4721 * We want to trap VFS attempts to truncate the file as soon as
4722 * possible. In particular, we want to make sure that when the VFS
4723 * shrinks i_size, we put the inode on the orphan list and modify
4724 * i_disksize immediately, so that during the subsequent flushing of
4725 * dirty pages and freeing of disk blocks, we can guarantee that any
4726 * commit will leave the blocks being flushed in an unused state on
4727 * disk. (On recovery, the inode will get truncated and the blocks will
4728 * be freed, so we have a strong guarantee that no future commit will
4729 * leave these blocks visible to the user.)
4730 *
Jan Kara678aaf42008-07-11 19:27:31 -04004731 * Another thing we have to assure is that if we are in ordered mode
4732 * and inode is still attached to the committing transaction, we must
4733 * we start writeout of all the dirty pages which are being truncated.
4734 * This way we are sure that all the data written in the previous
4735 * transaction are already on disk (truncate waits for pages under
4736 * writeback).
4737 *
4738 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004739 */
Mingming Cao617ba132006-10-11 01:20:53 -07004740int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004741{
4742 struct inode *inode = dentry->d_inode;
4743 int error, rc = 0;
4744 const unsigned int ia_valid = attr->ia_valid;
4745
4746 error = inode_change_ok(inode, attr);
4747 if (error)
4748 return error;
4749
4750 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
4751 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
4752 handle_t *handle;
4753
4754 /* (user+group)*(old+new) structure, inode write (sb,
4755 * inode block, ? - but truncate inode update has it) */
Mingming Cao617ba132006-10-11 01:20:53 -07004756 handle = ext4_journal_start(inode, 2*(EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)+
4757 EXT4_QUOTA_DEL_BLOCKS(inode->i_sb))+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004758 if (IS_ERR(handle)) {
4759 error = PTR_ERR(handle);
4760 goto err_out;
4761 }
Jan Karaa269eb12009-01-26 17:04:39 +01004762 error = vfs_dq_transfer(inode, attr) ? -EDQUOT : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004763 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07004764 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004765 return error;
4766 }
4767 /* Update corresponding info in inode so that everything is in
4768 * one transaction */
4769 if (attr->ia_valid & ATTR_UID)
4770 inode->i_uid = attr->ia_uid;
4771 if (attr->ia_valid & ATTR_GID)
4772 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07004773 error = ext4_mark_inode_dirty(handle, inode);
4774 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004775 }
4776
Eric Sandeene2b46572008-01-28 23:58:27 -05004777 if (attr->ia_valid & ATTR_SIZE) {
4778 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) {
4779 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4780
4781 if (attr->ia_size > sbi->s_bitmap_maxbytes) {
4782 error = -EFBIG;
4783 goto err_out;
4784 }
4785 }
4786 }
4787
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004788 if (S_ISREG(inode->i_mode) &&
4789 attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
4790 handle_t *handle;
4791
Mingming Cao617ba132006-10-11 01:20:53 -07004792 handle = ext4_journal_start(inode, 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004793 if (IS_ERR(handle)) {
4794 error = PTR_ERR(handle);
4795 goto err_out;
4796 }
4797
Mingming Cao617ba132006-10-11 01:20:53 -07004798 error = ext4_orphan_add(handle, inode);
4799 EXT4_I(inode)->i_disksize = attr->ia_size;
4800 rc = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004801 if (!error)
4802 error = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07004803 ext4_journal_stop(handle);
Jan Kara678aaf42008-07-11 19:27:31 -04004804
4805 if (ext4_should_order_data(inode)) {
4806 error = ext4_begin_ordered_truncate(inode,
4807 attr->ia_size);
4808 if (error) {
4809 /* Do as much error cleanup as possible */
4810 handle = ext4_journal_start(inode, 3);
4811 if (IS_ERR(handle)) {
4812 ext4_orphan_del(NULL, inode);
4813 goto err_out;
4814 }
4815 ext4_orphan_del(handle, inode);
4816 ext4_journal_stop(handle);
4817 goto err_out;
4818 }
4819 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004820 }
4821
4822 rc = inode_setattr(inode, attr);
4823
Mingming Cao617ba132006-10-11 01:20:53 -07004824 /* If inode_setattr's call to ext4_truncate failed to get a
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004825 * transaction handle at all, we need to clean up the in-core
4826 * orphan list manually. */
4827 if (inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07004828 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004829
4830 if (!rc && (ia_valid & ATTR_MODE))
Mingming Cao617ba132006-10-11 01:20:53 -07004831 rc = ext4_acl_chmod(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004832
4833err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07004834 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004835 if (!error)
4836 error = rc;
4837 return error;
4838}
4839
Mingming Cao3e3398a2008-07-11 19:27:31 -04004840int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
4841 struct kstat *stat)
4842{
4843 struct inode *inode;
4844 unsigned long delalloc_blocks;
4845
4846 inode = dentry->d_inode;
4847 generic_fillattr(inode, stat);
4848
4849 /*
4850 * We can't update i_blocks if the block allocation is delayed
4851 * otherwise in the case of system crash before the real block
4852 * allocation is done, we will have i_blocks inconsistent with
4853 * on-disk file blocks.
4854 * We always keep i_blocks updated together with real
4855 * allocation. But to not confuse with user, stat
4856 * will return the blocks that include the delayed allocation
4857 * blocks for this file.
4858 */
4859 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
4860 delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks;
4861 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
4862
4863 stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
4864 return 0;
4865}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004866
Mingming Caoa02908f2008-08-19 22:16:07 -04004867static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks,
4868 int chunk)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004869{
Mingming Caoa02908f2008-08-19 22:16:07 -04004870 int indirects;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004871
Mingming Caoa02908f2008-08-19 22:16:07 -04004872 /* if nrblocks are contiguous */
4873 if (chunk) {
4874 /*
4875 * With N contiguous data blocks, it need at most
4876 * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks
4877 * 2 dindirect blocks
4878 * 1 tindirect block
4879 */
4880 indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb);
4881 return indirects + 3;
4882 }
4883 /*
4884 * if nrblocks are not contiguous, worse case, each block touch
4885 * a indirect block, and each indirect block touch a double indirect
4886 * block, plus a triple indirect block
4887 */
4888 indirects = nrblocks * 2 + 1;
4889 return indirects;
4890}
Alex Tomasa86c6182006-10-11 01:21:03 -07004891
Mingming Caoa02908f2008-08-19 22:16:07 -04004892static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4893{
4894 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
Theodore Ts'oac51d832008-11-06 16:49:36 -05004895 return ext4_indirect_trans_blocks(inode, nrblocks, chunk);
4896 return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
Mingming Caoa02908f2008-08-19 22:16:07 -04004897}
Theodore Ts'oac51d832008-11-06 16:49:36 -05004898
Mingming Caoa02908f2008-08-19 22:16:07 -04004899/*
4900 * Account for index blocks, block groups bitmaps and block group
4901 * descriptor blocks if modify datablocks and index blocks
4902 * worse case, the indexs blocks spread over different block groups
4903 *
4904 * If datablocks are discontiguous, they are possible to spread over
4905 * different block groups too. If they are contiugous, with flexbg,
4906 * they could still across block group boundary.
4907 *
4908 * Also account for superblock, inode, quota and xattr blocks
4909 */
4910int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4911{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004912 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
4913 int gdpblocks;
Mingming Caoa02908f2008-08-19 22:16:07 -04004914 int idxblocks;
4915 int ret = 0;
4916
4917 /*
4918 * How many index blocks need to touch to modify nrblocks?
4919 * The "Chunk" flag indicating whether the nrblocks is
4920 * physically contiguous on disk
4921 *
4922 * For Direct IO and fallocate, they calls get_block to allocate
4923 * one single extent at a time, so they could set the "Chunk" flag
4924 */
4925 idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
4926
4927 ret = idxblocks;
4928
4929 /*
4930 * Now let's see how many group bitmaps and group descriptors need
4931 * to account
4932 */
4933 groups = idxblocks;
4934 if (chunk)
4935 groups += 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004936 else
Mingming Caoa02908f2008-08-19 22:16:07 -04004937 groups += nrblocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004938
Mingming Caoa02908f2008-08-19 22:16:07 -04004939 gdpblocks = groups;
Theodore Ts'o8df96752009-05-01 08:50:38 -04004940 if (groups > ngroups)
4941 groups = ngroups;
Mingming Caoa02908f2008-08-19 22:16:07 -04004942 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
4943 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
4944
4945 /* bitmaps and block group descriptor blocks */
4946 ret += groups + gdpblocks;
4947
4948 /* Blocks for super block, inode, quota and xattr blocks */
4949 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004950
4951 return ret;
4952}
4953
4954/*
Mingming Caoa02908f2008-08-19 22:16:07 -04004955 * Calulate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04004956 * the modification of a single pages into a single transaction,
4957 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04004958 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004959 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04004960 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004961 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04004962 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04004963 */
4964int ext4_writepage_trans_blocks(struct inode *inode)
4965{
4966 int bpp = ext4_journal_blocks_per_page(inode);
4967 int ret;
4968
4969 ret = ext4_meta_trans_blocks(inode, bpp, 0);
4970
4971 /* Account for data blocks for journalled mode */
4972 if (ext4_should_journal_data(inode))
4973 ret += bpp;
4974 return ret;
4975}
Mingming Caof3bd1f32008-08-19 22:16:03 -04004976
4977/*
4978 * Calculate the journal credits for a chunk of data modification.
4979 *
4980 * This is called from DIO, fallocate or whoever calling
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04004981 * ext4_get_blocks() to map/allocate a chunk of contigous disk blocks.
Mingming Caof3bd1f32008-08-19 22:16:03 -04004982 *
4983 * journal buffers for data blocks are not included here, as DIO
4984 * and fallocate do no need to journal data buffers.
4985 */
4986int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
4987{
4988 return ext4_meta_trans_blocks(inode, nrblocks, 1);
4989}
4990
Mingming Caoa02908f2008-08-19 22:16:07 -04004991/*
Mingming Cao617ba132006-10-11 01:20:53 -07004992 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004993 * Give this, we know that the caller already has write access to iloc->bh.
4994 */
Mingming Cao617ba132006-10-11 01:20:53 -07004995int ext4_mark_iloc_dirty(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004996 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004997{
4998 int err = 0;
4999
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005000 if (test_opt(inode->i_sb, I_VERSION))
5001 inode_inc_iversion(inode);
5002
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005003 /* the do_update_inode consumes one bh->b_count */
5004 get_bh(iloc->bh);
5005
Mingming Caodab291a2006-10-11 01:21:01 -07005006 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Mingming Cao617ba132006-10-11 01:20:53 -07005007 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005008 put_bh(iloc->bh);
5009 return err;
5010}
5011
5012/*
5013 * On success, We end up with an outstanding reference count against
5014 * iloc->bh. This _must_ be cleaned up later.
5015 */
5016
5017int
Mingming Cao617ba132006-10-11 01:20:53 -07005018ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
5019 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005020{
Frank Mayhar03901312009-01-07 00:06:22 -05005021 int err;
5022
5023 err = ext4_get_inode_loc(inode, iloc);
5024 if (!err) {
5025 BUFFER_TRACE(iloc->bh, "get_write_access");
5026 err = ext4_journal_get_write_access(handle, iloc->bh);
5027 if (err) {
5028 brelse(iloc->bh);
5029 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005030 }
5031 }
Mingming Cao617ba132006-10-11 01:20:53 -07005032 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005033 return err;
5034}
5035
5036/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005037 * Expand an inode by new_extra_isize bytes.
5038 * Returns 0 on success or negative error number on failure.
5039 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05005040static int ext4_expand_extra_isize(struct inode *inode,
5041 unsigned int new_extra_isize,
5042 struct ext4_iloc iloc,
5043 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005044{
5045 struct ext4_inode *raw_inode;
5046 struct ext4_xattr_ibody_header *header;
5047 struct ext4_xattr_entry *entry;
5048
5049 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
5050 return 0;
5051
5052 raw_inode = ext4_raw_inode(&iloc);
5053
5054 header = IHDR(inode, raw_inode);
5055 entry = IFIRST(header);
5056
5057 /* No extended attributes present */
5058 if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) ||
5059 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
5060 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
5061 new_extra_isize);
5062 EXT4_I(inode)->i_extra_isize = new_extra_isize;
5063 return 0;
5064 }
5065
5066 /* try to expand with EAs present */
5067 return ext4_expand_extra_isize_ea(inode, new_extra_isize,
5068 raw_inode, handle);
5069}
5070
5071/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005072 * What we do here is to mark the in-core inode as clean with respect to inode
5073 * dirtiness (it may still be data-dirty).
5074 * This means that the in-core inode may be reaped by prune_icache
5075 * without having to perform any I/O. This is a very good thing,
5076 * because *any* task may call prune_icache - even ones which
5077 * have a transaction open against a different journal.
5078 *
5079 * Is this cheating? Not really. Sure, we haven't written the
5080 * inode out, but prune_icache isn't a user-visible syncing function.
5081 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5082 * we start and wait on commits.
5083 *
5084 * Is this efficient/effective? Well, we're being nice to the system
5085 * by cleaning up our inodes proactively so they can be reaped
5086 * without I/O. But we are potentially leaving up to five seconds'
5087 * worth of inodes floating about which prune_icache wants us to
5088 * write out. One way to fix that would be to get prune_icache()
5089 * to do a write_super() to free up some memory. It has the desired
5090 * effect.
5091 */
Mingming Cao617ba132006-10-11 01:20:53 -07005092int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005093{
Mingming Cao617ba132006-10-11 01:20:53 -07005094 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005095 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5096 static unsigned int mnt_count;
5097 int err, ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005098
5099 might_sleep();
Mingming Cao617ba132006-10-11 01:20:53 -07005100 err = ext4_reserve_inode_write(handle, inode, &iloc);
Frank Mayhar03901312009-01-07 00:06:22 -05005101 if (ext4_handle_valid(handle) &&
5102 EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005103 !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) {
5104 /*
5105 * We need extra buffer credits since we may write into EA block
5106 * with this same handle. If journal_extend fails, then it will
5107 * only result in a minor loss of functionality for that inode.
5108 * If this is felt to be critical, then e2fsck should be run to
5109 * force a large enough s_min_extra_isize.
5110 */
5111 if ((jbd2_journal_extend(handle,
5112 EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
5113 ret = ext4_expand_extra_isize(inode,
5114 sbi->s_want_extra_isize,
5115 iloc, handle);
5116 if (ret) {
5117 EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND;
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005118 if (mnt_count !=
5119 le16_to_cpu(sbi->s_es->s_mnt_count)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04005120 ext4_warning(inode->i_sb, __func__,
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005121 "Unable to expand inode %lu. Delete"
5122 " some EAs or run e2fsck.",
5123 inode->i_ino);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005124 mnt_count =
5125 le16_to_cpu(sbi->s_es->s_mnt_count);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005126 }
5127 }
5128 }
5129 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005130 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07005131 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005132 return err;
5133}
5134
5135/*
Mingming Cao617ba132006-10-11 01:20:53 -07005136 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005137 *
5138 * We're really interested in the case where a file is being extended.
5139 * i_size has been changed by generic_commit_write() and we thus need
5140 * to include the updated inode in the current transaction.
5141 *
Jan Karaa269eb12009-01-26 17:04:39 +01005142 * Also, vfs_dq_alloc_block() will always dirty the inode when blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005143 * are allocated to the file.
5144 *
5145 * If the inode is marked synchronous, we don't honour that here - doing
5146 * so would cause a commit on atime updates, which we don't bother doing.
5147 * We handle synchronous inodes at the highest possible level.
5148 */
Mingming Cao617ba132006-10-11 01:20:53 -07005149void ext4_dirty_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005150{
Mingming Cao617ba132006-10-11 01:20:53 -07005151 handle_t *current_handle = ext4_journal_current_handle();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005152 handle_t *handle;
5153
Frank Mayhar03901312009-01-07 00:06:22 -05005154 if (!ext4_handle_valid(current_handle)) {
5155 ext4_mark_inode_dirty(current_handle, inode);
5156 return;
5157 }
5158
Mingming Cao617ba132006-10-11 01:20:53 -07005159 handle = ext4_journal_start(inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005160 if (IS_ERR(handle))
5161 goto out;
5162 if (current_handle &&
5163 current_handle->h_transaction != handle->h_transaction) {
5164 /* This task has a transaction open against a different fs */
5165 printk(KERN_EMERG "%s: transactions do not match!\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04005166 __func__);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005167 } else {
5168 jbd_debug(5, "marking dirty. outer handle=%p\n",
5169 current_handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005170 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005171 }
Mingming Cao617ba132006-10-11 01:20:53 -07005172 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005173out:
5174 return;
5175}
5176
5177#if 0
5178/*
5179 * Bind an inode's backing buffer_head into this transaction, to prevent
5180 * it from being flushed to disk early. Unlike
Mingming Cao617ba132006-10-11 01:20:53 -07005181 * ext4_reserve_inode_write, this leaves behind no bh reference and
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005182 * returns no iloc structure, so the caller needs to repeat the iloc
5183 * lookup to mark the inode dirty later.
5184 */
Mingming Cao617ba132006-10-11 01:20:53 -07005185static int ext4_pin_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005186{
Mingming Cao617ba132006-10-11 01:20:53 -07005187 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005188
5189 int err = 0;
5190 if (handle) {
Mingming Cao617ba132006-10-11 01:20:53 -07005191 err = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005192 if (!err) {
5193 BUFFER_TRACE(iloc.bh, "get_write_access");
Mingming Caodab291a2006-10-11 01:21:01 -07005194 err = jbd2_journal_get_write_access(handle, iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005195 if (!err)
Frank Mayhar03901312009-01-07 00:06:22 -05005196 err = ext4_handle_dirty_metadata(handle,
5197 inode,
5198 iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005199 brelse(iloc.bh);
5200 }
5201 }
Mingming Cao617ba132006-10-11 01:20:53 -07005202 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005203 return err;
5204}
5205#endif
5206
Mingming Cao617ba132006-10-11 01:20:53 -07005207int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005208{
5209 journal_t *journal;
5210 handle_t *handle;
5211 int err;
5212
5213 /*
5214 * We have to be very careful here: changing a data block's
5215 * journaling status dynamically is dangerous. If we write a
5216 * data block to the journal, change the status and then delete
5217 * that block, we risk forgetting to revoke the old log record
5218 * from the journal and so a subsequent replay can corrupt data.
5219 * So, first we make sure that the journal is empty and that
5220 * nobody is changing anything.
5221 */
5222
Mingming Cao617ba132006-10-11 01:20:53 -07005223 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005224 if (!journal)
5225 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04005226 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005227 return -EROFS;
5228
Mingming Caodab291a2006-10-11 01:21:01 -07005229 jbd2_journal_lock_updates(journal);
5230 jbd2_journal_flush(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005231
5232 /*
5233 * OK, there are no updates running now, and all cached data is
5234 * synced to disk. We are now in a completely consistent state
5235 * which doesn't have anything in the journal, and we know that
5236 * no filesystem updates are running, so it is safe to modify
5237 * the inode's in-core data-journaling state flag now.
5238 */
5239
5240 if (val)
Mingming Cao617ba132006-10-11 01:20:53 -07005241 EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005242 else
Mingming Cao617ba132006-10-11 01:20:53 -07005243 EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL;
5244 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005245
Mingming Caodab291a2006-10-11 01:21:01 -07005246 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005247
5248 /* Finally we can mark the inode as dirty. */
5249
Mingming Cao617ba132006-10-11 01:20:53 -07005250 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005251 if (IS_ERR(handle))
5252 return PTR_ERR(handle);
5253
Mingming Cao617ba132006-10-11 01:20:53 -07005254 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005255 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005256 ext4_journal_stop(handle);
5257 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005258
5259 return err;
5260}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005261
5262static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5263{
5264 return !buffer_mapped(bh);
5265}
5266
Nick Pigginc2ec1752009-03-31 15:23:21 -07005267int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005268{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005269 struct page *page = vmf->page;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005270 loff_t size;
5271 unsigned long len;
5272 int ret = -EINVAL;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005273 void *fsdata;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005274 struct file *file = vma->vm_file;
5275 struct inode *inode = file->f_path.dentry->d_inode;
5276 struct address_space *mapping = inode->i_mapping;
5277
5278 /*
5279 * Get i_alloc_sem to stop truncates messing with the inode. We cannot
5280 * get i_mutex because we are already holding mmap_sem.
5281 */
5282 down_read(&inode->i_alloc_sem);
5283 size = i_size_read(inode);
5284 if (page->mapping != mapping || size <= page_offset(page)
5285 || !PageUptodate(page)) {
5286 /* page got truncated from under us? */
5287 goto out_unlock;
5288 }
5289 ret = 0;
5290 if (PageMappedToDisk(page))
5291 goto out_unlock;
5292
5293 if (page->index == size >> PAGE_CACHE_SHIFT)
5294 len = size & ~PAGE_CACHE_MASK;
5295 else
5296 len = PAGE_CACHE_SIZE;
5297
5298 if (page_has_buffers(page)) {
5299 /* return if we have all the buffers mapped */
5300 if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
5301 ext4_bh_unmapped))
5302 goto out_unlock;
5303 }
5304 /*
5305 * OK, we need to fill the hole... Do write_begin write_end
5306 * to do block allocation/reservation.We are not holding
5307 * inode.i__mutex here. That allow * parallel write_begin,
5308 * write_end call. lock_page prevent this from happening
5309 * on the same page though
5310 */
5311 ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005312 len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005313 if (ret < 0)
5314 goto out_unlock;
5315 ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04005316 len, len, page, fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005317 if (ret < 0)
5318 goto out_unlock;
5319 ret = 0;
5320out_unlock:
Nick Pigginc2ec1752009-03-31 15:23:21 -07005321 if (ret)
5322 ret = VM_FAULT_SIGBUS;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005323 up_read(&inode->i_alloc_sem);
5324 return ret;
5325}