blob: b4051c9ac5f22c1d187031bb5ad1bd3a75044ce3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/ext3/inode.c
3 *
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
Dave Kleikampe9ad5622006-09-27 01:49:35 -070016 * (sct@redhat.com), 1993, 1998
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * 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
Dave Kleikampe9ad5622006-09-27 01:49:35 -070020 * (jj@sunsite.ms.mff.cuni.cz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
22 * Assorted race fixes, rewrite of ext3_get_block() by Al Viro, 2000
23 */
24
25#include <linux/module.h>
26#include <linux/fs.h>
27#include <linux/time.h>
28#include <linux/ext3_jbd.h>
29#include <linux/jbd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/highuid.h>
31#include <linux/pagemap.h>
32#include <linux/quotaops.h>
33#include <linux/string.h>
34#include <linux/buffer_head.h>
35#include <linux/writeback.h>
36#include <linux/mpage.h>
37#include <linux/uio.h>
Jens Axboecaa38fb2006-07-23 01:41:26 +020038#include <linux/bio.h>
Josef Bacik68c9d702008-10-03 17:32:43 -040039#include <linux/fiemap.h>
Duane Griffinb5ed3112008-12-19 20:47:14 +000040#include <linux/namei.h>
Lukas Czerner785c4bc2011-05-23 18:33:01 +020041#include <trace/events/ext3.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xattr.h"
43#include "acl.h"
44
45static int ext3_writepage_trans_blocks(struct inode *inode);
46
47/*
48 * Test whether an inode is a fast symlink.
49 */
Andrew Mortond6859bf2006-03-26 01:38:03 -080050static int ext3_inode_is_fast_symlink(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 int ea_blocks = EXT3_I(inode)->i_file_acl ?
53 (inode->i_sb->s_blocksize >> 9) : 0;
54
Andrew Mortond6859bf2006-03-26 01:38:03 -080055 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Andrew Mortond6859bf2006-03-26 01:38:03 -080058/*
59 * The ext3 forget function must perform a revoke if we are freeing data
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * which has been journaled. Metadata (eg. indirect blocks) must be
Mingming Caoae6ddcc2006-09-27 01:49:27 -070061 * revoked in all cases.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 *
63 * "bh" may be NULL: a metadata block may have been freed from memory
64 * but there may still be a record of it in the journal, and that record
65 * still needs to be revoked.
66 */
Andrew Mortond6859bf2006-03-26 01:38:03 -080067int ext3_forget(handle_t *handle, int is_metadata, struct inode *inode,
Mingming Cao1c2bf372006-06-25 05:48:06 -070068 struct buffer_head *bh, ext3_fsblk_t blocknr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 int err;
71
72 might_sleep();
73
Lukas Czerner785c4bc2011-05-23 18:33:01 +020074 trace_ext3_forget(inode, is_metadata, blocknr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 BUFFER_TRACE(bh, "enter");
76
77 jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
78 "data mode %lx\n",
79 bh, is_metadata, inode->i_mode,
80 test_opt(inode->i_sb, DATA_FLAGS));
81
82 /* Never use the revoke function if we are doing full data
83 * journaling: there is no need to, and a V1 superblock won't
84 * support it. Otherwise, only skip the revoke on un-journaled
85 * data blocks. */
86
87 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ||
88 (!is_metadata && !ext3_should_journal_data(inode))) {
89 if (bh) {
90 BUFFER_TRACE(bh, "call journal_forget");
91 return ext3_journal_forget(handle, bh);
92 }
93 return 0;
94 }
95
96 /*
97 * data!=journal && (is_metadata || should_journal_data(inode))
98 */
99 BUFFER_TRACE(bh, "call ext3_journal_revoke");
100 err = ext3_journal_revoke(handle, blocknr, bh);
101 if (err)
Harvey Harrisone05b6b52008-04-28 02:16:15 -0700102 ext3_abort(inode->i_sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 "error %d when attempting revoke", err);
104 BUFFER_TRACE(bh, "exit");
105 return err;
106}
107
108/*
Andrew Mortond6859bf2006-03-26 01:38:03 -0800109 * Work out how many blocks we need to proceed with the next chunk of a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * truncate transaction.
111 */
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700112static unsigned long blocks_for_truncate(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 unsigned long needed;
115
116 needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
117
118 /* Give ourselves just enough room to cope with inodes in which
119 * i_blocks is corrupt: we've seen disk corruptions in the past
120 * which resulted in random data in an inode which looked enough
121 * like a regular file for ext3 to try to delete it. Things
122 * will go a bit crazy if that happens, but at least we should
123 * try not to panic the whole kernel. */
124 if (needed < 2)
125 needed = 2;
126
127 /* But we need to bound the transaction so we don't overflow the
128 * journal. */
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700129 if (needed > EXT3_MAX_TRANS_DATA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 needed = EXT3_MAX_TRANS_DATA;
131
Jan Kara1f545872005-06-23 22:01:04 -0700132 return EXT3_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700135/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * Truncate transactions can be complex and absolutely huge. So we need to
137 * be able to restart the transaction at a conventient checkpoint to make
138 * sure we don't overflow the journal.
139 *
140 * start_transaction gets us a new handle for a truncate transaction,
141 * and extend_transaction tries to extend the existing one a bit. If
142 * extend fails, we need to propagate the failure up and restart the
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700143 * transaction in the top-level truncate loop. --sct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 */
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700145static handle_t *start_transaction(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 handle_t *result;
148
149 result = ext3_journal_start(inode, blocks_for_truncate(inode));
150 if (!IS_ERR(result))
151 return result;
152
153 ext3_std_error(inode->i_sb, PTR_ERR(result));
154 return result;
155}
156
157/*
158 * Try to extend this transaction for the purposes of truncation.
159 *
160 * Returns 0 if we managed to create more room. If we can't create more
161 * room, and the transaction must be restarted we return 1.
162 */
163static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
164{
165 if (handle->h_buffer_credits > EXT3_RESERVE_TRANS_BLOCKS)
166 return 0;
167 if (!ext3_journal_extend(handle, blocks_for_truncate(inode)))
168 return 0;
169 return 1;
170}
171
172/*
173 * Restart the transaction associated with *handle. This does a commit,
174 * so before we call here everything must be consistently dirtied against
175 * this transaction.
176 */
Jan Kara00171d32009-08-11 19:06:10 +0200177static int truncate_restart_transaction(handle_t *handle, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Jan Kara00171d32009-08-11 19:06:10 +0200179 int ret;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 jbd_debug(2, "restarting handle %p\n", handle);
Jan Kara00171d32009-08-11 19:06:10 +0200182 /*
183 * Drop truncate_mutex to avoid deadlock with ext3_get_blocks_handle
184 * At this moment, get_block can be called only for blocks inside
185 * i_size since page cache has been already dropped and writes are
186 * blocked by i_mutex. So we can safely drop the truncate_mutex.
187 */
188 mutex_unlock(&EXT3_I(inode)->truncate_mutex);
189 ret = ext3_journal_restart(handle, blocks_for_truncate(inode));
190 mutex_lock(&EXT3_I(inode)->truncate_mutex);
191 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/*
Al Viroac14a952010-06-06 07:08:19 -0400195 * Called at inode eviction from icache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 */
Al Viroac14a952010-06-06 07:08:19 -0400197void ext3_evict_inode (struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Al Viroac14a952010-06-06 07:08:19 -0400199 struct ext3_block_alloc_info *rsv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 handle_t *handle;
Al Viroac14a952010-06-06 07:08:19 -0400201 int want_delete = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Lukas Czerner785c4bc2011-05-23 18:33:01 +0200203 trace_ext3_evict_inode(inode);
Al Viroac14a952010-06-06 07:08:19 -0400204 if (!inode->i_nlink && !is_bad_inode(inode)) {
Christoph Hellwig871a2932010-03-03 09:05:07 -0500205 dquot_initialize(inode);
Al Viroac14a952010-06-06 07:08:19 -0400206 want_delete = 1;
207 }
Christoph Hellwig907f4552010-03-03 09:05:06 -0500208
Mark Fashehfef26652005-09-09 13:01:31 -0700209 truncate_inode_pages(&inode->i_data, 0);
210
Al Viroac14a952010-06-06 07:08:19 -0400211 ext3_discard_reservation(inode);
212 rsv = EXT3_I(inode)->i_block_alloc_info;
213 EXT3_I(inode)->i_block_alloc_info = NULL;
214 if (unlikely(rsv))
215 kfree(rsv);
216
217 if (!want_delete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 goto no_delete;
219
220 handle = start_transaction(inode);
221 if (IS_ERR(handle)) {
Andrew Mortond6859bf2006-03-26 01:38:03 -0800222 /*
223 * If we're going to skip the normal cleanup, we still need to
224 * make sure that the in-core orphan linked list is properly
225 * cleaned up.
226 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 ext3_orphan_del(NULL, inode);
228 goto no_delete;
229 }
230
231 if (IS_SYNC(inode))
232 handle->h_sync = 1;
233 inode->i_size = 0;
234 if (inode->i_blocks)
235 ext3_truncate(inode);
236 /*
Jan Kara40680f2f2011-05-24 22:24:47 +0200237 * Kill off the orphan record created when the inode lost the last
238 * link. Note that ext3_orphan_del() has to be able to cope with the
239 * deletion of a non-existent orphan - ext3_truncate() could
240 * have removed the record.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 */
242 ext3_orphan_del(handle, inode);
243 EXT3_I(inode)->i_dtime = get_seconds();
244
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700245 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 * One subtle ordering requirement: if anything has gone wrong
247 * (transaction abort, IO errors, whatever), then we can still
248 * do these next steps (the fs will already have been marked as
249 * having errors), but we can't free the inode if the mark_dirty
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700250 * fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 */
Al Viroac14a952010-06-06 07:08:19 -0400252 if (ext3_mark_inode_dirty(handle, inode)) {
253 /* If that failed, just dquot_drop() and be done with that */
254 dquot_drop(inode);
255 end_writeback(inode);
256 } else {
257 ext3_xattr_delete_inode(handle, inode);
258 dquot_free_inode(inode);
259 dquot_drop(inode);
260 end_writeback(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 ext3_free_inode(handle, inode);
Al Viroac14a952010-06-06 07:08:19 -0400262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 ext3_journal_stop(handle);
264 return;
265no_delete:
Al Viroac14a952010-06-06 07:08:19 -0400266 end_writeback(inode);
267 dquot_drop(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270typedef struct {
271 __le32 *p;
272 __le32 key;
273 struct buffer_head *bh;
274} Indirect;
275
276static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
277{
278 p->key = *(p->p = v);
279 p->bh = bh;
280}
281
Andrew Mortond6859bf2006-03-26 01:38:03 -0800282static int verify_chain(Indirect *from, Indirect *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 while (from <= to && from->key == *from->p)
285 from++;
286 return (from > to);
287}
288
289/**
290 * ext3_block_to_path - parse the block number into array of offsets
291 * @inode: inode in question (we are only interested in its superblock)
292 * @i_block: block number to be parsed
293 * @offsets: array to store the offsets in
294 * @boundary: set this non-zero if the referred-to block is likely to be
295 * followed (on disk) by an indirect block.
296 *
297 * To store the locations of file's data ext3 uses a data structure common
298 * for UNIX filesystems - tree of pointers anchored in the inode, with
299 * data blocks at leaves and indirect blocks in intermediate nodes.
300 * This function translates the block number into path in that tree -
301 * return value is the path length and @offsets[n] is the offset of
302 * pointer to (n+1)th node in the nth one. If @block is out of range
303 * (negative or too large) warning is printed and zero returned.
304 *
305 * Note: function doesn't find node addresses, so no IO is needed. All
306 * we need to know is the capacity of indirect blocks (taken from the
307 * inode->i_sb).
308 */
309
310/*
311 * Portability note: the last comparison (check that we fit into triple
312 * indirect block) is spelled differently, because otherwise on an
313 * architecture with 32-bit longs and 8Kb pages we might get into trouble
314 * if our filesystem had 8Kb blocks. We might use long long, but that would
315 * kill us on x86. Oh, well, at least the sign propagation does not matter -
316 * i_block would have to be negative in the very beginning, so we would not
317 * get there at all.
318 */
319
320static int ext3_block_to_path(struct inode *inode,
321 long i_block, int offsets[4], int *boundary)
322{
323 int ptrs = EXT3_ADDR_PER_BLOCK(inode->i_sb);
324 int ptrs_bits = EXT3_ADDR_PER_BLOCK_BITS(inode->i_sb);
325 const long direct_blocks = EXT3_NDIR_BLOCKS,
326 indirect_blocks = ptrs,
327 double_blocks = (1 << (ptrs_bits * 2));
328 int n = 0;
329 int final = 0;
330
331 if (i_block < 0) {
332 ext3_warning (inode->i_sb, "ext3_block_to_path", "block < 0");
333 } else if (i_block < direct_blocks) {
334 offsets[n++] = i_block;
335 final = direct_blocks;
336 } else if ( (i_block -= direct_blocks) < indirect_blocks) {
337 offsets[n++] = EXT3_IND_BLOCK;
338 offsets[n++] = i_block;
339 final = ptrs;
340 } else if ((i_block -= indirect_blocks) < double_blocks) {
341 offsets[n++] = EXT3_DIND_BLOCK;
342 offsets[n++] = i_block >> ptrs_bits;
343 offsets[n++] = i_block & (ptrs - 1);
344 final = ptrs;
345 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
346 offsets[n++] = EXT3_TIND_BLOCK;
347 offsets[n++] = i_block >> (ptrs_bits * 2);
348 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
349 offsets[n++] = i_block & (ptrs - 1);
350 final = ptrs;
351 } else {
Andrew Mortond6859bf2006-03-26 01:38:03 -0800352 ext3_warning(inode->i_sb, "ext3_block_to_path", "block > big");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 if (boundary)
Mingming Cao89747d32006-03-26 01:37:55 -0800355 *boundary = final - 1 - (i_block & (ptrs - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return n;
357}
358
359/**
360 * ext3_get_branch - read the chain of indirect blocks leading to data
361 * @inode: inode in question
362 * @depth: depth of the chain (1 - direct pointer, etc.)
363 * @offsets: offsets of pointers in inode/indirect blocks
364 * @chain: place to store the result
365 * @err: here we store the error value
366 *
367 * Function fills the array of triples <key, p, bh> and returns %NULL
368 * if everything went OK or the pointer to the last filled triple
369 * (incomplete one) otherwise. Upon the return chain[i].key contains
370 * the number of (i+1)-th block in the chain (as it is stored in memory,
371 * i.e. little-endian 32-bit), chain[i].p contains the address of that
372 * number (it points into struct inode for i==0 and into the bh->b_data
373 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
374 * block for i>0 and NULL for i==0. In other words, it holds the block
375 * numbers of the chain, addresses they were taken from (and where we can
376 * verify that chain did not change) and buffer_heads hosting these
377 * numbers.
378 *
379 * Function stops when it stumbles upon zero pointer (absent block)
380 * (pointer to last triple returned, *@err == 0)
381 * or when it gets an IO error reading an indirect block
382 * (ditto, *@err == -EIO)
383 * or when it notices that chain had been changed while it was reading
384 * (ditto, *@err == -EAGAIN)
385 * or when it reads all @depth-1 indirect blocks successfully and finds
386 * the whole chain, all way to the data (returns %NULL, *err == 0).
387 */
388static Indirect *ext3_get_branch(struct inode *inode, int depth, int *offsets,
389 Indirect chain[4], int *err)
390{
391 struct super_block *sb = inode->i_sb;
392 Indirect *p = chain;
393 struct buffer_head *bh;
394
395 *err = 0;
396 /* i_data is not going away, no lock needed */
397 add_chain (chain, NULL, EXT3_I(inode)->i_data + *offsets);
398 if (!p->key)
399 goto no_block;
400 while (--depth) {
401 bh = sb_bread(sb, le32_to_cpu(p->key));
402 if (!bh)
403 goto failure;
404 /* Reader: pointers */
405 if (!verify_chain(chain, p))
406 goto changed;
407 add_chain(++p, bh, (__le32*)bh->b_data + *++offsets);
408 /* Reader: end */
409 if (!p->key)
410 goto no_block;
411 }
412 return NULL;
413
414changed:
415 brelse(bh);
416 *err = -EAGAIN;
417 goto no_block;
418failure:
419 *err = -EIO;
420no_block:
421 return p;
422}
423
424/**
425 * ext3_find_near - find a place for allocation with sufficient locality
426 * @inode: owner
427 * @ind: descriptor of indirect block.
428 *
Benoit Boissinot1cc8dcf52008-04-21 22:45:55 +0000429 * This function returns the preferred place for block allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 * It is used when heuristic for sequential allocation fails.
431 * Rules are:
432 * + if there is a block to the left of our position - allocate near it.
433 * + if pointer will live in indirect block - allocate near that block.
434 * + if pointer will live in inode - allocate in the same
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700435 * cylinder group.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 *
437 * In the latter case we colour the starting block by the callers PID to
438 * prevent it from clashing with concurrent allocations for a different inode
439 * in the same block group. The PID is used here so that functionally related
440 * files will be close-by on-disk.
441 *
442 * Caller must make sure that @ind is valid and will stay that way.
443 */
Mingming Cao43d23f92006-06-25 05:48:07 -0700444static ext3_fsblk_t ext3_find_near(struct inode *inode, Indirect *ind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 struct ext3_inode_info *ei = EXT3_I(inode);
447 __le32 *start = ind->bh ? (__le32*) ind->bh->b_data : ei->i_data;
448 __le32 *p;
Mingming Cao43d23f92006-06-25 05:48:07 -0700449 ext3_fsblk_t bg_start;
450 ext3_grpblk_t colour;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 /* Try to find previous block */
Andrew Mortond6859bf2006-03-26 01:38:03 -0800453 for (p = ind->p - 1; p >= start; p--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (*p)
455 return le32_to_cpu(*p);
Andrew Mortond6859bf2006-03-26 01:38:03 -0800456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /* No such thing, so let's try location of indirect block */
459 if (ind->bh)
460 return ind->bh->b_blocknr;
461
462 /*
Andrew Mortond6859bf2006-03-26 01:38:03 -0800463 * It is going to be referred to from the inode itself? OK, just put it
464 * into the same cylinder group then.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 */
Mingming Cao43d23f92006-06-25 05:48:07 -0700466 bg_start = ext3_group_first_block_no(inode->i_sb, ei->i_block_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 colour = (current->pid % 16) *
468 (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
469 return bg_start + colour;
470}
471
472/**
Benoit Boissinot1cc8dcf52008-04-21 22:45:55 +0000473 * ext3_find_goal - find a preferred place for allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 * @inode: owner
475 * @block: block we want
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 * @partial: pointer to the last triple within a chain
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 *
Benoit Boissinot1cc8dcf52008-04-21 22:45:55 +0000478 * Normally this function find the preferred place for block allocation,
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800479 * returns it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 */
481
Mingming Cao43d23f92006-06-25 05:48:07 -0700482static ext3_fsblk_t ext3_find_goal(struct inode *inode, long block,
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800483 Indirect *partial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Andrew Mortond6859bf2006-03-26 01:38:03 -0800485 struct ext3_block_alloc_info *block_i;
486
487 block_i = EXT3_I(inode)->i_block_alloc_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 /*
490 * try the heuristic for sequential allocation,
491 * failing that at least try to get decent locality.
492 */
493 if (block_i && (block == block_i->last_alloc_logical_block + 1)
494 && (block_i->last_alloc_physical_block != 0)) {
Mingming Caofe55c452005-05-01 08:59:20 -0700495 return block_i->last_alloc_physical_block + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497
Mingming Caofe55c452005-05-01 08:59:20 -0700498 return ext3_find_near(inode, partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
Andrew Mortond6859bf2006-03-26 01:38:03 -0800500
Mingming Caob47b2472006-03-26 01:37:56 -0800501/**
Namhyung Kima4c18ad2010-10-19 00:34:35 +0900502 * ext3_blks_to_allocate - Look up the block map and count the number
Mingming Caob47b2472006-03-26 01:37:56 -0800503 * of direct blocks need to be allocated for the given branch.
504 *
Dave Kleikampe9ad5622006-09-27 01:49:35 -0700505 * @branch: chain of indirect blocks
Mingming Caob47b2472006-03-26 01:37:56 -0800506 * @k: number of blocks need for indirect blocks
507 * @blks: number of data blocks to be mapped.
508 * @blocks_to_boundary: the offset in the indirect block
509 *
510 * return the total number of blocks to be allocate, including the
511 * direct and indirect blocks.
512 */
Andrew Mortond6859bf2006-03-26 01:38:03 -0800513static int ext3_blks_to_allocate(Indirect *branch, int k, unsigned long blks,
Mingming Caob47b2472006-03-26 01:37:56 -0800514 int blocks_to_boundary)
515{
516 unsigned long count = 0;
517
518 /*
519 * Simple case, [t,d]Indirect block(s) has not allocated yet
520 * then it's clear blocks on that path have not allocated
521 */
522 if (k > 0) {
Andrew Mortond6859bf2006-03-26 01:38:03 -0800523 /* right now we don't handle cross boundary allocation */
Mingming Caob47b2472006-03-26 01:37:56 -0800524 if (blks < blocks_to_boundary + 1)
525 count += blks;
526 else
527 count += blocks_to_boundary + 1;
528 return count;
529 }
530
531 count++;
532 while (count < blks && count <= blocks_to_boundary &&
533 le32_to_cpu(*(branch[0].p + count)) == 0) {
534 count++;
535 }
536 return count;
537}
538
539/**
Namhyung Kima4c18ad2010-10-19 00:34:35 +0900540 * ext3_alloc_blocks - multiple allocate blocks needed for a branch
541 * @handle: handle for this transaction
542 * @inode: owner
543 * @goal: preferred place for allocation
Mingming Caob47b2472006-03-26 01:37:56 -0800544 * @indirect_blks: the number of blocks need to allocate for indirect
545 * blocks
Namhyung Kima4c18ad2010-10-19 00:34:35 +0900546 * @blks: number of blocks need to allocated for direct blocks
Mingming Caob47b2472006-03-26 01:37:56 -0800547 * @new_blocks: on return it will store the new block numbers for
548 * the indirect blocks(if needed) and the first direct block,
Namhyung Kima4c18ad2010-10-19 00:34:35 +0900549 * @err: here we store the error value
550 *
551 * return the number of direct blocks allocated
Mingming Caob47b2472006-03-26 01:37:56 -0800552 */
553static int ext3_alloc_blocks(handle_t *handle, struct inode *inode,
Mingming Cao43d23f92006-06-25 05:48:07 -0700554 ext3_fsblk_t goal, int indirect_blks, int blks,
555 ext3_fsblk_t new_blocks[4], int *err)
Mingming Caob47b2472006-03-26 01:37:56 -0800556{
557 int target, i;
558 unsigned long count = 0;
559 int index = 0;
Mingming Cao43d23f92006-06-25 05:48:07 -0700560 ext3_fsblk_t current_block = 0;
Mingming Caob47b2472006-03-26 01:37:56 -0800561 int ret = 0;
562
563 /*
564 * Here we try to allocate the requested multiple blocks at once,
565 * on a best-effort basis.
566 * To build a branch, we should allocate blocks for
567 * the indirect blocks(if not allocated yet), and at least
568 * the first direct block of this branch. That's the
569 * minimum number of blocks need to allocate(required)
570 */
571 target = blks + indirect_blks;
572
573 while (1) {
574 count = target;
575 /* allocating blocks for indirect blocks and direct blocks */
Andrew Mortond6859bf2006-03-26 01:38:03 -0800576 current_block = ext3_new_blocks(handle,inode,goal,&count,err);
Mingming Caob47b2472006-03-26 01:37:56 -0800577 if (*err)
578 goto failed_out;
579
580 target -= count;
581 /* allocate blocks for indirect blocks */
582 while (index < indirect_blks && count) {
583 new_blocks[index++] = current_block++;
584 count--;
585 }
586
587 if (count > 0)
588 break;
589 }
590
591 /* save the new block number for the first direct block */
592 new_blocks[index] = current_block;
593
594 /* total number of blocks allocated for direct blocks */
595 ret = count;
596 *err = 0;
597 return ret;
598failed_out:
599 for (i = 0; i <index; i++)
600 ext3_free_blocks(handle, inode, new_blocks[i], 1);
601 return ret;
602}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604/**
605 * ext3_alloc_branch - allocate and set up a chain of blocks.
Namhyung Kima4c18ad2010-10-19 00:34:35 +0900606 * @handle: handle for this transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * @inode: owner
Mingming Caob47b2472006-03-26 01:37:56 -0800608 * @indirect_blks: number of allocated indirect blocks
609 * @blks: number of allocated direct blocks
Namhyung Kima4c18ad2010-10-19 00:34:35 +0900610 * @goal: preferred place for allocation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * @offsets: offsets (in the blocks) to store the pointers to next.
612 * @branch: place to store the chain in.
613 *
Mingming Caob47b2472006-03-26 01:37:56 -0800614 * This function allocates blocks, zeroes out all but the last one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 * links them into chain and (if we are synchronous) writes them to disk.
616 * In other words, it prepares a branch that can be spliced onto the
617 * inode. It stores the information about that chain in the branch[], in
618 * the same format as ext3_get_branch() would do. We are calling it after
619 * we had read the existing part of chain and partial points to the last
620 * triple of that (one with zero ->key). Upon the exit we have the same
Glauber de Oliveira Costa5b116872005-10-30 15:02:48 -0800621 * picture as after the successful ext3_get_block(), except that in one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 * place chain is disconnected - *branch->p is still zero (we did not
623 * set the last link), but branch->key contains the number that should
624 * be placed into *branch->p to fill that gap.
625 *
626 * If allocation fails we free all blocks we've allocated (and forget
627 * their buffer_heads) and return the error value the from failed
628 * ext3_alloc_block() (normally -ENOSPC). Otherwise we set the chain
629 * as described above and return 0.
630 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631static int ext3_alloc_branch(handle_t *handle, struct inode *inode,
Mingming Cao43d23f92006-06-25 05:48:07 -0700632 int indirect_blks, int *blks, ext3_fsblk_t goal,
Mingming Caob47b2472006-03-26 01:37:56 -0800633 int *offsets, Indirect *branch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 int blocksize = inode->i_sb->s_blocksize;
Mingming Caob47b2472006-03-26 01:37:56 -0800636 int i, n = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 int err = 0;
Mingming Caob47b2472006-03-26 01:37:56 -0800638 struct buffer_head *bh;
639 int num;
Mingming Cao43d23f92006-06-25 05:48:07 -0700640 ext3_fsblk_t new_blocks[4];
641 ext3_fsblk_t current_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Mingming Caob47b2472006-03-26 01:37:56 -0800643 num = ext3_alloc_blocks(handle, inode, goal, indirect_blks,
644 *blks, new_blocks, &err);
645 if (err)
646 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Mingming Caob47b2472006-03-26 01:37:56 -0800648 branch[0].key = cpu_to_le32(new_blocks[0]);
649 /*
650 * metadata blocks and data blocks are allocated.
651 */
652 for (n = 1; n <= indirect_blks; n++) {
653 /*
654 * Get buffer_head for parent block, zero it out
655 * and set the pointer to new one, then send
656 * parent to disk.
657 */
658 bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
659 branch[n].bh = bh;
660 lock_buffer(bh);
661 BUFFER_TRACE(bh, "call get_create_access");
662 err = ext3_journal_get_create_access(handle, bh);
663 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 unlock_buffer(bh);
Mingming Caob47b2472006-03-26 01:37:56 -0800665 brelse(bh);
666 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Mingming Caob47b2472006-03-26 01:37:56 -0800669 memset(bh->b_data, 0, blocksize);
670 branch[n].p = (__le32 *) bh->b_data + offsets[n];
671 branch[n].key = cpu_to_le32(new_blocks[n]);
672 *branch[n].p = branch[n].key;
673 if ( n == indirect_blks) {
674 current_block = new_blocks[n];
675 /*
676 * End of chain, update the last new metablock of
677 * the chain to point to the new allocated
678 * data blocks numbers
679 */
680 for (i=1; i < num; i++)
681 *(branch[n].p + i) = cpu_to_le32(++current_block);
682 }
683 BUFFER_TRACE(bh, "marking uptodate");
684 set_buffer_uptodate(bh);
685 unlock_buffer(bh);
686
687 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
688 err = ext3_journal_dirty_metadata(handle, bh);
689 if (err)
690 goto failed;
691 }
692 *blks = num;
693 return err;
694failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /* Allocation failed, free what we already allocated */
Mingming Caob47b2472006-03-26 01:37:56 -0800696 for (i = 1; i <= n ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 BUFFER_TRACE(branch[i].bh, "call journal_forget");
698 ext3_journal_forget(handle, branch[i].bh);
699 }
Mingming Caob47b2472006-03-26 01:37:56 -0800700 for (i = 0; i <indirect_blks; i++)
701 ext3_free_blocks(handle, inode, new_blocks[i], 1);
702
703 ext3_free_blocks(handle, inode, new_blocks[i], num);
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return err;
706}
707
708/**
Andrew Mortond6859bf2006-03-26 01:38:03 -0800709 * ext3_splice_branch - splice the allocated branch onto inode.
Namhyung Kima4c18ad2010-10-19 00:34:35 +0900710 * @handle: handle for this transaction
Andrew Mortond6859bf2006-03-26 01:38:03 -0800711 * @inode: owner
712 * @block: (logical) number of block we are adding
Andrew Mortond6859bf2006-03-26 01:38:03 -0800713 * @where: location of missing link
714 * @num: number of indirect blocks we are adding
715 * @blks: number of direct blocks we are adding
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 *
Andrew Mortond6859bf2006-03-26 01:38:03 -0800717 * This function fills the missing link and does all housekeeping needed in
718 * inode (->i_blocks, etc.). In case of success we end up with the full
719 * chain to new block and return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
Andrew Mortond6859bf2006-03-26 01:38:03 -0800721static int ext3_splice_branch(handle_t *handle, struct inode *inode,
722 long block, Indirect *where, int num, int blks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 int i;
725 int err = 0;
Andrew Mortond6859bf2006-03-26 01:38:03 -0800726 struct ext3_block_alloc_info *block_i;
Mingming Cao43d23f92006-06-25 05:48:07 -0700727 ext3_fsblk_t current_block;
Jan Karafe8bc912009-10-16 19:26:15 +0200728 struct ext3_inode_info *ei = EXT3_I(inode);
Andrew Mortond6859bf2006-03-26 01:38:03 -0800729
Jan Karafe8bc912009-10-16 19:26:15 +0200730 block_i = ei->i_block_alloc_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 /*
732 * If we're splicing into a [td]indirect block (as opposed to the
733 * inode) then we need to get write access to the [td]indirect block
734 * before the splice.
735 */
736 if (where->bh) {
737 BUFFER_TRACE(where->bh, "get_write_access");
738 err = ext3_journal_get_write_access(handle, where->bh);
739 if (err)
740 goto err_out;
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /* That's it */
743
744 *where->p = where->key;
Andrew Mortond6859bf2006-03-26 01:38:03 -0800745
746 /*
747 * Update the host buffer_head or inode to point to more just allocated
748 * direct blocks blocks
749 */
Mingming Caob47b2472006-03-26 01:37:56 -0800750 if (num == 0 && blks > 1) {
Mingming Cao5dea5172006-05-03 19:55:12 -0700751 current_block = le32_to_cpu(where->key) + 1;
Mingming Caob47b2472006-03-26 01:37:56 -0800752 for (i = 1; i < blks; i++)
753 *(where->p + i ) = cpu_to_le32(current_block++);
754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 /*
757 * update the most recently allocated logical & physical block
758 * in i_block_alloc_info, to assist find the proper goal block for next
759 * allocation
760 */
761 if (block_i) {
Mingming Caob47b2472006-03-26 01:37:56 -0800762 block_i->last_alloc_logical_block = block + blks - 1;
Andrew Mortond6859bf2006-03-26 01:38:03 -0800763 block_i->last_alloc_physical_block =
Mingming Cao5dea5172006-05-03 19:55:12 -0700764 le32_to_cpu(where[num].key) + blks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766
767 /* We are done with atomic stuff, now do the rest of housekeeping */
768
769 inode->i_ctime = CURRENT_TIME_SEC;
770 ext3_mark_inode_dirty(handle, inode);
Jan Karafe8bc912009-10-16 19:26:15 +0200771 /* ext3_mark_inode_dirty already updated i_sync_tid */
772 atomic_set(&ei->i_datasync_tid, handle->h_transaction->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 /* had we spliced it onto indirect block? */
775 if (where->bh) {
776 /*
Andrew Mortond6859bf2006-03-26 01:38:03 -0800777 * If we spliced it onto an indirect block, we haven't
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 * altered the inode. Note however that if it is being spliced
779 * onto an indirect block at the very end of the file (the
780 * file is growing) then we *will* alter the inode to reflect
781 * the new i_size. But that is not done here - it is done in
782 * generic_commit_write->__mark_inode_dirty->ext3_dirty_inode.
783 */
784 jbd_debug(5, "splicing indirect only\n");
785 BUFFER_TRACE(where->bh, "call ext3_journal_dirty_metadata");
786 err = ext3_journal_dirty_metadata(handle, where->bh);
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700787 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 goto err_out;
789 } else {
790 /*
791 * OK, we spliced it into the inode itself on a direct block.
792 * Inode was dirtied above.
793 */
794 jbd_debug(5, "splicing direct\n");
795 }
796 return err;
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798err_out:
Mingming Caob47b2472006-03-26 01:37:56 -0800799 for (i = 1; i <= num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 BUFFER_TRACE(where[i].bh, "call journal_forget");
801 ext3_journal_forget(handle, where[i].bh);
Andrew Mortond6859bf2006-03-26 01:38:03 -0800802 ext3_free_blocks(handle,inode,le32_to_cpu(where[i-1].key),1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
Mingming Caob47b2472006-03-26 01:37:56 -0800804 ext3_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks);
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return err;
807}
808
809/*
810 * Allocation strategy is simple: if we have to allocate something, we will
811 * have to go the whole way to leaf. So let's do it before attaching anything
812 * to tree, set linkage between the newborn blocks, write them if sync is
813 * required, recheck the path, free and repeat if check fails, otherwise
814 * set the last missing link (that will protect us from any truncate-generated
815 * removals - all blocks on the path are immune now) and possibly force the
816 * write on the parent block.
817 * That has a nice additional property: no special recovery from the failed
818 * allocations is needed - we simply release blocks and do not touch anything
819 * reachable from inode.
820 *
Andrew Mortond6859bf2006-03-26 01:38:03 -0800821 * `handle' can be NULL if create == 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 *
823 * The BKL may not be held on entry here. Be sure to take it early.
Mingming Cao89747d32006-03-26 01:37:55 -0800824 * return > 0, # of blocks mapped or allocated.
825 * return = 0, if plain lookup failed.
826 * return < 0, error case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 */
Andrew Mortond6859bf2006-03-26 01:38:03 -0800828int ext3_get_blocks_handle(handle_t *handle, struct inode *inode,
829 sector_t iblock, unsigned long maxblocks,
830 struct buffer_head *bh_result,
Jan Kara43237b52009-05-20 18:41:58 +0200831 int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 int err = -EIO;
834 int offsets[4];
835 Indirect chain[4];
836 Indirect *partial;
Mingming Cao43d23f92006-06-25 05:48:07 -0700837 ext3_fsblk_t goal;
Mingming Caob47b2472006-03-26 01:37:56 -0800838 int indirect_blks;
Mingming Cao89747d32006-03-26 01:37:55 -0800839 int blocks_to_boundary = 0;
840 int depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 struct ext3_inode_info *ei = EXT3_I(inode);
Mingming Cao89747d32006-03-26 01:37:55 -0800842 int count = 0;
Mingming Cao43d23f92006-06-25 05:48:07 -0700843 ext3_fsblk_t first_block = 0;
Mingming Cao89747d32006-03-26 01:37:55 -0800844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Lukas Czerner785c4bc2011-05-23 18:33:01 +0200846 trace_ext3_get_blocks_enter(inode, iblock, maxblocks, create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 J_ASSERT(handle != NULL || create == 0);
Andrew Mortond6859bf2006-03-26 01:38:03 -0800848 depth = ext3_block_to_path(inode,iblock,offsets,&blocks_to_boundary);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 if (depth == 0)
851 goto out;
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 partial = ext3_get_branch(inode, depth, offsets, chain, &err);
854
855 /* Simplest case - block found, no allocation needed */
856 if (!partial) {
Mingming Cao5dea5172006-05-03 19:55:12 -0700857 first_block = le32_to_cpu(chain[depth - 1].key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 clear_buffer_new(bh_result);
Mingming Cao89747d32006-03-26 01:37:55 -0800859 count++;
860 /*map more blocks*/
861 while (count < maxblocks && count <= blocks_to_boundary) {
Mingming Cao43d23f92006-06-25 05:48:07 -0700862 ext3_fsblk_t blk;
Mingming Cao5dea5172006-05-03 19:55:12 -0700863
Jan Karae8ef7aa2009-06-17 16:26:23 -0700864 if (!verify_chain(chain, chain + depth - 1)) {
Mingming Cao89747d32006-03-26 01:37:55 -0800865 /*
866 * Indirect block might be removed by
867 * truncate while we were reading it.
868 * Handling of that case: forget what we've
869 * got now. Flag the err as EAGAIN, so it
870 * will reread.
871 */
872 err = -EAGAIN;
873 count = 0;
874 break;
875 }
Mingming Cao5dea5172006-05-03 19:55:12 -0700876 blk = le32_to_cpu(*(chain[depth-1].p + count));
877
878 if (blk == first_block + count)
Mingming Cao89747d32006-03-26 01:37:55 -0800879 count++;
880 else
881 break;
882 }
883 if (err != -EAGAIN)
884 goto got_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
886
887 /* Next simple case - plain lookup or failed read of indirect block */
Mingming Caofe55c452005-05-01 08:59:20 -0700888 if (!create || err == -EIO)
889 goto cleanup;
890
Jan Kara40680f2f2011-05-24 22:24:47 +0200891 /*
892 * Block out ext3_truncate while we alter the tree
893 */
Arjan van de Ven97461512006-03-23 03:00:42 -0800894 mutex_lock(&ei->truncate_mutex);
Mingming Caofe55c452005-05-01 08:59:20 -0700895
896 /*
897 * If the indirect block is missing while we are reading
898 * the chain(ext3_get_branch() returns -EAGAIN err), or
899 * if the chain has been changed after we grab the semaphore,
900 * (either because another process truncated this branch, or
901 * another get_block allocated this branch) re-grab the chain to see if
902 * the request block has been allocated or not.
903 *
904 * Since we already block the truncate/other get_block
905 * at this point, we will have the current copy of the chain when we
906 * splice the branch into the tree.
907 */
908 if (err == -EAGAIN || !verify_chain(chain, partial)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 while (partial > chain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 brelse(partial->bh);
911 partial--;
912 }
Mingming Caofe55c452005-05-01 08:59:20 -0700913 partial = ext3_get_branch(inode, depth, offsets, chain, &err);
914 if (!partial) {
Mingming Cao89747d32006-03-26 01:37:55 -0800915 count++;
Arjan van de Ven97461512006-03-23 03:00:42 -0800916 mutex_unlock(&ei->truncate_mutex);
Mingming Caofe55c452005-05-01 08:59:20 -0700917 if (err)
918 goto cleanup;
919 clear_buffer_new(bh_result);
920 goto got_it;
921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923
924 /*
Mingming Caofe55c452005-05-01 08:59:20 -0700925 * Okay, we need to do block allocation. Lazily initialize the block
926 * allocation info here if necessary
927 */
928 if (S_ISREG(inode->i_mode) && (!ei->i_block_alloc_info))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 ext3_init_block_alloc_info(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Akinobu Mitafb01bfd2008-02-06 01:40:16 -0800931 goal = ext3_find_goal(inode, iblock, partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Mingming Caob47b2472006-03-26 01:37:56 -0800933 /* the number of blocks need to allocate for [d,t]indirect blocks */
934 indirect_blks = (chain + depth) - partial - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 /*
Mingming Caob47b2472006-03-26 01:37:56 -0800937 * Next look up the indirect map to count the totoal number of
938 * direct blocks to allocate for this branch.
939 */
940 count = ext3_blks_to_allocate(partial, indirect_blks,
941 maxblocks, blocks_to_boundary);
Mingming Caob47b2472006-03-26 01:37:56 -0800942 err = ext3_alloc_branch(handle, inode, indirect_blks, &count, goal,
Mingming Caofe55c452005-05-01 08:59:20 -0700943 offsets + (partial - chain), partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Mingming Caofe55c452005-05-01 08:59:20 -0700945 /*
946 * The ext3_splice_branch call will free and forget any buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 * on the new chain if there is a failure, but that risks using
948 * up transaction credits, especially for bitmaps where the
949 * credits cannot be returned. Can we handle this somehow? We
Mingming Caofe55c452005-05-01 08:59:20 -0700950 * may need to return -EAGAIN upwards in the worst case. --sct
951 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (!err)
Mingming Caob47b2472006-03-26 01:37:56 -0800953 err = ext3_splice_branch(handle, inode, iblock,
954 partial, indirect_blks, count);
Arjan van de Ven97461512006-03-23 03:00:42 -0800955 mutex_unlock(&ei->truncate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (err)
957 goto cleanup;
958
959 set_buffer_new(bh_result);
Mingming Caofe55c452005-05-01 08:59:20 -0700960got_it:
961 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
Suparna Bhattacharya20acaa12006-09-16 12:15:58 -0700962 if (count > blocks_to_boundary)
Mingming Caofe55c452005-05-01 08:59:20 -0700963 set_buffer_boundary(bh_result);
Mingming Cao89747d32006-03-26 01:37:55 -0800964 err = count;
Mingming Caofe55c452005-05-01 08:59:20 -0700965 /* Clean up and exit */
966 partial = chain + depth - 1; /* the whole chain */
967cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 while (partial > chain) {
Mingming Caofe55c452005-05-01 08:59:20 -0700969 BUFFER_TRACE(partial->bh, "call brelse");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 brelse(partial->bh);
971 partial--;
972 }
Mingming Caofe55c452005-05-01 08:59:20 -0700973 BUFFER_TRACE(bh_result, "returned");
974out:
Lukas Czerner785c4bc2011-05-23 18:33:01 +0200975 trace_ext3_get_blocks_exit(inode, iblock,
976 depth ? le32_to_cpu(chain[depth-1].key) : 0,
977 count, err);
Mingming Caofe55c452005-05-01 08:59:20 -0700978 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Jan Karabd1939d2008-02-06 01:40:21 -0800981/* Maximum number of blocks we map for direct IO at once. */
982#define DIO_MAX_BLOCKS 4096
983/*
984 * Number of credits we need for writing DIO_MAX_BLOCKS:
985 * We need sb + group descriptor + bitmap + inode -> 4
986 * For B blocks with A block pointers per block we need:
987 * 1 (triple ind.) + (B/A/A + 2) (doubly ind.) + (B/A + 2) (indirect).
988 * If we plug in 4096 for B and 256 for A (for 1KB block size), we get 25.
989 */
990#define DIO_CREDITS 25
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Badari Pulavartyf91a2ad2006-03-26 01:38:04 -0800992static int ext3_get_block(struct inode *inode, sector_t iblock,
993 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Dmitriy Monakhov3e4fdaf2007-02-10 01:46:35 -0800995 handle_t *handle = ext3_journal_current_handle();
Jan Karabd1939d2008-02-06 01:40:21 -0800996 int ret = 0, started = 0;
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -0800997 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Jan Karabd1939d2008-02-06 01:40:21 -0800999 if (create && !handle) { /* Direct IO write... */
1000 if (max_blocks > DIO_MAX_BLOCKS)
1001 max_blocks = DIO_MAX_BLOCKS;
1002 handle = ext3_journal_start(inode, DIO_CREDITS +
Dmitry Monakhovc4590012009-12-09 03:05:30 +03001003 EXT3_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb));
Jan Karabd1939d2008-02-06 01:40:21 -08001004 if (IS_ERR(handle)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 ret = PTR_ERR(handle);
Jan Karabd1939d2008-02-06 01:40:21 -08001006 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
Jan Karabd1939d2008-02-06 01:40:21 -08001008 started = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010
Jan Karabd1939d2008-02-06 01:40:21 -08001011 ret = ext3_get_blocks_handle(handle, inode, iblock,
Jan Kara43237b52009-05-20 18:41:58 +02001012 max_blocks, bh_result, create);
Jan Karabd1939d2008-02-06 01:40:21 -08001013 if (ret > 0) {
1014 bh_result->b_size = (ret << inode->i_blkbits);
1015 ret = 0;
Mingming Cao89747d32006-03-26 01:37:55 -08001016 }
Jan Karabd1939d2008-02-06 01:40:21 -08001017 if (started)
1018 ext3_journal_stop(handle);
1019out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return ret;
1021}
1022
Josef Bacik68c9d702008-10-03 17:32:43 -04001023int ext3_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1024 u64 start, u64 len)
1025{
1026 return generic_block_fiemap(inode, fieinfo, start, len,
1027 ext3_get_block);
1028}
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030/*
1031 * `handle' can be NULL if create is zero
1032 */
Andrew Mortond6859bf2006-03-26 01:38:03 -08001033struct buffer_head *ext3_getblk(handle_t *handle, struct inode *inode,
1034 long block, int create, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 struct buffer_head dummy;
1037 int fatal = 0, err;
1038
1039 J_ASSERT(handle != NULL || create == 0);
1040
1041 dummy.b_state = 0;
1042 dummy.b_blocknr = -1000;
1043 buffer_trace_init(&dummy.b_history);
Mingming Cao89747d32006-03-26 01:37:55 -08001044 err = ext3_get_blocks_handle(handle, inode, block, 1,
Jan Kara43237b52009-05-20 18:41:58 +02001045 &dummy, create);
Badari Pulavarty3665d0e2006-09-08 09:48:21 -07001046 /*
1047 * ext3_get_blocks_handle() returns number of blocks
1048 * mapped. 0 in case of a HOLE.
1049 */
1050 if (err > 0) {
1051 if (err > 1)
1052 WARN_ON(1);
Mingming Cao89747d32006-03-26 01:37:55 -08001053 err = 0;
Mingming Cao89747d32006-03-26 01:37:55 -08001054 }
1055 *errp = err;
1056 if (!err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 struct buffer_head *bh;
1058 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Glauber de Oliveira Costa2973dfd2005-10-30 15:03:05 -08001059 if (!bh) {
1060 *errp = -EIO;
1061 goto err;
1062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (buffer_new(&dummy)) {
1064 J_ASSERT(create != 0);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -07001065 J_ASSERT(handle != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Andrew Mortond6859bf2006-03-26 01:38:03 -08001067 /*
1068 * Now that we do not always journal data, we should
1069 * keep in mind whether this should always journal the
1070 * new buffer as metadata. For now, regular file
1071 * writes use ext3_get_block instead, so it's not a
1072 * problem.
1073 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 lock_buffer(bh);
1075 BUFFER_TRACE(bh, "call get_create_access");
1076 fatal = ext3_journal_get_create_access(handle, bh);
1077 if (!fatal && !buffer_uptodate(bh)) {
Andrew Mortond6859bf2006-03-26 01:38:03 -08001078 memset(bh->b_data,0,inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 set_buffer_uptodate(bh);
1080 }
1081 unlock_buffer(bh);
1082 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1083 err = ext3_journal_dirty_metadata(handle, bh);
1084 if (!fatal)
1085 fatal = err;
1086 } else {
1087 BUFFER_TRACE(bh, "not a new buffer");
1088 }
1089 if (fatal) {
1090 *errp = fatal;
1091 brelse(bh);
1092 bh = NULL;
1093 }
1094 return bh;
1095 }
Glauber de Oliveira Costa2973dfd2005-10-30 15:03:05 -08001096err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return NULL;
1098}
1099
Andrew Mortond6859bf2006-03-26 01:38:03 -08001100struct buffer_head *ext3_bread(handle_t *handle, struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 int block, int create, int *err)
1102{
1103 struct buffer_head * bh;
1104
1105 bh = ext3_getblk(handle, inode, block, create, err);
1106 if (!bh)
1107 return bh;
1108 if (buffer_uptodate(bh))
1109 return bh;
Jens Axboecaa38fb2006-07-23 01:41:26 +02001110 ll_rw_block(READ_META, 1, &bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 wait_on_buffer(bh);
1112 if (buffer_uptodate(bh))
1113 return bh;
1114 put_bh(bh);
1115 *err = -EIO;
1116 return NULL;
1117}
1118
1119static int walk_page_buffers( handle_t *handle,
1120 struct buffer_head *head,
1121 unsigned from,
1122 unsigned to,
1123 int *partial,
1124 int (*fn)( handle_t *handle,
1125 struct buffer_head *bh))
1126{
1127 struct buffer_head *bh;
1128 unsigned block_start, block_end;
1129 unsigned blocksize = head->b_size;
1130 int err, ret = 0;
1131 struct buffer_head *next;
1132
1133 for ( bh = head, block_start = 0;
1134 ret == 0 && (bh != head || !block_start);
Dave Kleikampe9ad5622006-09-27 01:49:35 -07001135 block_start = block_end, bh = next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 {
1137 next = bh->b_this_page;
1138 block_end = block_start + blocksize;
1139 if (block_end <= from || block_start >= to) {
1140 if (partial && !buffer_uptodate(bh))
1141 *partial = 1;
1142 continue;
1143 }
1144 err = (*fn)(handle, bh);
1145 if (!ret)
1146 ret = err;
1147 }
1148 return ret;
1149}
1150
1151/*
1152 * To preserve ordering, it is essential that the hole instantiation and
1153 * the data write be encapsulated in a single transaction. We cannot
1154 * close off a transaction and start a new one between the ext3_get_block()
1155 * and the commit_write(). So doing the journal_start at the start of
1156 * prepare_write() is the right place.
1157 *
1158 * Also, this function can nest inside ext3_writepage() ->
1159 * block_write_full_page(). In that case, we *know* that ext3_writepage()
1160 * has generated enough buffer credits to do the whole page. So we won't
1161 * block on the journal in that case, which is good, because the caller may
1162 * be PF_MEMALLOC.
1163 *
1164 * By accident, ext3 can be reentered when a transaction is open via
1165 * quota file writes. If we were to commit the transaction while thus
1166 * reentered, there can be a deadlock - we would be holding a quota
1167 * lock, and the commit would never complete if another thread had a
1168 * transaction open and was blocking on the quota lock - a ranking
1169 * violation.
1170 *
1171 * So what we do is to rely on the fact that journal_stop/journal_start
1172 * will _not_ run commit under these circumstances because handle->h_ref
1173 * is elevated. We'll still have enough credits for the tiny quotafile
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001174 * write.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 */
Andrew Mortond6859bf2006-03-26 01:38:03 -08001176static int do_journal_get_write_access(handle_t *handle,
1177 struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Jan Kara5f11e6a2010-08-05 12:38:26 +02001179 int dirty = buffer_dirty(bh);
1180 int ret;
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (!buffer_mapped(bh) || buffer_freed(bh))
1183 return 0;
Jan Kara5f11e6a2010-08-05 12:38:26 +02001184 /*
1185 * __block_prepare_write() could have dirtied some buffers. Clean
1186 * the dirty bit as jbd2_journal_get_write_access() could complain
1187 * otherwise about fs integrity issues. Setting of the dirty bit
1188 * by __block_prepare_write() isn't a real problem here as we clear
1189 * the bit before releasing a page lock and thus writeback cannot
1190 * ever write the buffer.
1191 */
1192 if (dirty)
1193 clear_buffer_dirty(bh);
1194 ret = ext3_journal_get_write_access(handle, bh);
1195 if (!ret && dirty)
1196 ret = ext3_journal_dirty_metadata(handle, bh);
1197 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
Jan Kara68eb3db2009-12-01 16:53:06 +01001200/*
1201 * Truncate blocks that were not used by write. We have to truncate the
1202 * pagecache as well so that corresponding buffers get properly unmapped.
1203 */
1204static void ext3_truncate_failed_write(struct inode *inode)
1205{
1206 truncate_inode_pages(inode->i_mapping, inode->i_size);
1207 ext3_truncate(inode);
1208}
1209
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001210static int ext3_write_begin(struct file *file, struct address_space *mapping,
1211 loff_t pos, unsigned len, unsigned flags,
1212 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001214 struct inode *inode = mapping->host;
Jan Kara695f6ae2009-04-02 16:57:17 -07001215 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 handle_t *handle;
1217 int retries = 0;
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001218 struct page *page;
1219 pgoff_t index;
1220 unsigned from, to;
Jan Kara695f6ae2009-04-02 16:57:17 -07001221 /* Reserve one block more for addition to orphan list in case
1222 * we allocate blocks but write fails for some reason */
1223 int needed_blocks = ext3_writepage_trans_blocks(inode) + 1;
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001224
Lukas Czerner785c4bc2011-05-23 18:33:01 +02001225 trace_ext3_write_begin(inode, pos, len, flags);
1226
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001227 index = pos >> PAGE_CACHE_SHIFT;
1228 from = pos & (PAGE_CACHE_SIZE - 1);
1229 to = from + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231retry:
Nick Piggin54566b22009-01-04 12:00:53 -08001232 page = grab_cache_page_write_begin(mapping, index, flags);
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001233 if (!page)
1234 return -ENOMEM;
1235 *pagep = page;
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 handle = ext3_journal_start(inode, needed_blocks);
Andrew Morton1aa9b4b2007-04-01 23:49:43 -07001238 if (IS_ERR(handle)) {
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001239 unlock_page(page);
1240 page_cache_release(page);
Andrew Morton1aa9b4b2007-04-01 23:49:43 -07001241 ret = PTR_ERR(handle);
1242 goto out;
1243 }
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001244 ret = __block_write_begin(page, pos, len, ext3_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (ret)
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001246 goto write_begin_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 if (ext3_should_journal_data(inode)) {
1249 ret = walk_page_buffers(handle, page_buffers(page),
1250 from, to, NULL, do_journal_get_write_access);
1251 }
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001252write_begin_failed:
1253 if (ret) {
Aneesh Kumar K.V5ec8b752008-10-18 20:28:00 -07001254 /*
1255 * block_write_begin may have instantiated a few blocks
1256 * outside i_size. Trim these off again. Don't need
1257 * i_size_read because we hold i_mutex.
Jan Kara695f6ae2009-04-02 16:57:17 -07001258 *
1259 * Add inode to orphan list in case we crash before truncate
Jan Kara9eaaa2d2009-07-13 20:26:52 +02001260 * finishes. Do this only if ext3_can_truncate() agrees so
1261 * that orphan processing code is happy.
Aneesh Kumar K.V5ec8b752008-10-18 20:28:00 -07001262 */
Jan Kara9eaaa2d2009-07-13 20:26:52 +02001263 if (pos + len > inode->i_size && ext3_can_truncate(inode))
Jan Kara695f6ae2009-04-02 16:57:17 -07001264 ext3_orphan_add(handle, inode);
1265 ext3_journal_stop(handle);
1266 unlock_page(page);
1267 page_cache_release(page);
1268 if (pos + len > inode->i_size)
Jan Kara68eb3db2009-12-01 16:53:06 +01001269 ext3_truncate_failed_write(inode);
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (ret == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
1272 goto retry;
Andrew Morton1aa9b4b2007-04-01 23:49:43 -07001273out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return ret;
1275}
1276
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001277
Andrew Mortond6859bf2006-03-26 01:38:03 -08001278int ext3_journal_dirty_data(handle_t *handle, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 int err = journal_dirty_data(handle, bh);
1281 if (err)
Harvey Harrisone05b6b52008-04-28 02:16:15 -07001282 ext3_journal_abort_handle(__func__, __func__,
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001283 bh, handle, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return err;
1285}
1286
Jan Kara695f6ae2009-04-02 16:57:17 -07001287/* For ordered writepage and write_end functions */
1288static int journal_dirty_data_fn(handle_t *handle, struct buffer_head *bh)
1289{
1290 /*
1291 * Write could have mapped the buffer but it didn't copy the data in
1292 * yet. So avoid filing such buffer into a transaction.
1293 */
1294 if (buffer_mapped(bh) && buffer_uptodate(bh))
1295 return ext3_journal_dirty_data(handle, bh);
1296 return 0;
1297}
1298
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001299/* For write_end() in data=journal mode */
1300static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 if (!buffer_mapped(bh) || buffer_freed(bh))
1303 return 0;
1304 set_buffer_uptodate(bh);
1305 return ext3_journal_dirty_metadata(handle, bh);
1306}
1307
1308/*
Jan Kara695f6ae2009-04-02 16:57:17 -07001309 * This is nasty and subtle: ext3_write_begin() could have allocated blocks
1310 * for the whole page but later we failed to copy the data in. Update inode
1311 * size according to what we managed to copy. The rest is going to be
1312 * truncated in write_end function.
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001313 */
Jan Kara695f6ae2009-04-02 16:57:17 -07001314static void update_file_sizes(struct inode *inode, loff_t pos, unsigned copied)
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001315{
Jan Kara695f6ae2009-04-02 16:57:17 -07001316 /* What matters to us is i_disksize. We don't write i_size anywhere */
1317 if (pos + copied > inode->i_size)
1318 i_size_write(inode, pos + copied);
1319 if (pos + copied > EXT3_I(inode)->i_disksize) {
1320 EXT3_I(inode)->i_disksize = pos + copied;
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001321 mark_inode_dirty(inode);
1322 }
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001323}
1324
1325/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 * We need to pick up the new inode size which generic_commit_write gave us
1327 * `file' can be NULL - eg, when called from page_symlink().
1328 *
1329 * ext3 never places buffers on inode->i_mapping->private_list. metadata
1330 * buffers are managed internally.
1331 */
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001332static int ext3_ordered_write_end(struct file *file,
1333 struct address_space *mapping,
1334 loff_t pos, unsigned len, unsigned copied,
1335 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 handle_t *handle = ext3_journal_current_handle();
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001338 struct inode *inode = file->f_mapping->host;
1339 unsigned from, to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 int ret = 0, ret2;
1341
Lukas Czerner785c4bc2011-05-23 18:33:01 +02001342 trace_ext3_ordered_write_end(inode, pos, len, copied);
Jan Kara695f6ae2009-04-02 16:57:17 -07001343 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
1344
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001345 from = pos & (PAGE_CACHE_SIZE - 1);
Jan Kara695f6ae2009-04-02 16:57:17 -07001346 to = from + copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 ret = walk_page_buffers(handle, page_buffers(page),
Jan Kara695f6ae2009-04-02 16:57:17 -07001348 from, to, NULL, journal_dirty_data_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Jan Kara695f6ae2009-04-02 16:57:17 -07001350 if (ret == 0)
1351 update_file_sizes(inode, pos, copied);
1352 /*
1353 * There may be allocated blocks outside of i_size because
1354 * we failed to copy some data. Prepare for truncate.
1355 */
Jan Kara9eaaa2d2009-07-13 20:26:52 +02001356 if (pos + len > inode->i_size && ext3_can_truncate(inode))
Jan Kara695f6ae2009-04-02 16:57:17 -07001357 ext3_orphan_add(handle, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 ret2 = ext3_journal_stop(handle);
1359 if (!ret)
1360 ret = ret2;
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001361 unlock_page(page);
1362 page_cache_release(page);
1363
Jan Kara695f6ae2009-04-02 16:57:17 -07001364 if (pos + len > inode->i_size)
Jan Kara68eb3db2009-12-01 16:53:06 +01001365 ext3_truncate_failed_write(inode);
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001366 return ret ? ret : copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001369static int ext3_writeback_write_end(struct file *file,
1370 struct address_space *mapping,
1371 loff_t pos, unsigned len, unsigned copied,
1372 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
1374 handle_t *handle = ext3_journal_current_handle();
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001375 struct inode *inode = file->f_mapping->host;
Jan Kara695f6ae2009-04-02 16:57:17 -07001376 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Lukas Czerner785c4bc2011-05-23 18:33:01 +02001378 trace_ext3_writeback_write_end(inode, pos, len, copied);
Jan Kara695f6ae2009-04-02 16:57:17 -07001379 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
1380 update_file_sizes(inode, pos, copied);
1381 /*
1382 * There may be allocated blocks outside of i_size because
1383 * we failed to copy some data. Prepare for truncate.
1384 */
Jan Kara9eaaa2d2009-07-13 20:26:52 +02001385 if (pos + len > inode->i_size && ext3_can_truncate(inode))
Jan Kara695f6ae2009-04-02 16:57:17 -07001386 ext3_orphan_add(handle, inode);
1387 ret = ext3_journal_stop(handle);
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001388 unlock_page(page);
1389 page_cache_release(page);
1390
Jan Kara695f6ae2009-04-02 16:57:17 -07001391 if (pos + len > inode->i_size)
Jan Kara68eb3db2009-12-01 16:53:06 +01001392 ext3_truncate_failed_write(inode);
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001393 return ret ? ret : copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001396static int ext3_journalled_write_end(struct file *file,
1397 struct address_space *mapping,
1398 loff_t pos, unsigned len, unsigned copied,
1399 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
1401 handle_t *handle = ext3_journal_current_handle();
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001402 struct inode *inode = mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 int ret = 0, ret2;
1404 int partial = 0;
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001405 unsigned from, to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Lukas Czerner785c4bc2011-05-23 18:33:01 +02001407 trace_ext3_journalled_write_end(inode, pos, len, copied);
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001408 from = pos & (PAGE_CACHE_SIZE - 1);
1409 to = from + len;
1410
1411 if (copied < len) {
1412 if (!PageUptodate(page))
1413 copied = 0;
Jan Kara695f6ae2009-04-02 16:57:17 -07001414 page_zero_new_buffers(page, from + copied, to);
1415 to = from + copied;
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 ret = walk_page_buffers(handle, page_buffers(page), from,
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001419 to, &partial, write_end_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (!partial)
1421 SetPageUptodate(page);
Jan Kara695f6ae2009-04-02 16:57:17 -07001422
1423 if (pos + copied > inode->i_size)
1424 i_size_write(inode, pos + copied);
1425 /*
1426 * There may be allocated blocks outside of i_size because
1427 * we failed to copy some data. Prepare for truncate.
1428 */
Jan Kara9eaaa2d2009-07-13 20:26:52 +02001429 if (pos + len > inode->i_size && ext3_can_truncate(inode))
Jan Kara695f6ae2009-04-02 16:57:17 -07001430 ext3_orphan_add(handle, inode);
Jan Kara9df93932010-01-06 21:58:48 +01001431 ext3_set_inode_state(inode, EXT3_STATE_JDATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (inode->i_size > EXT3_I(inode)->i_disksize) {
1433 EXT3_I(inode)->i_disksize = inode->i_size;
1434 ret2 = ext3_mark_inode_dirty(handle, inode);
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001435 if (!ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 ret = ret2;
1437 }
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 ret2 = ext3_journal_stop(handle);
1440 if (!ret)
1441 ret = ret2;
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001442 unlock_page(page);
1443 page_cache_release(page);
1444
Jan Kara695f6ae2009-04-02 16:57:17 -07001445 if (pos + len > inode->i_size)
Jan Kara68eb3db2009-12-01 16:53:06 +01001446 ext3_truncate_failed_write(inode);
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001447 return ret ? ret : copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
1449
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001450/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 * bmap() is special. It gets used by applications such as lilo and by
1452 * the swapper to find the on-disk block of a specific piece of data.
1453 *
1454 * Naturally, this is dangerous if the block concerned is still in the
1455 * journal. If somebody makes a swapfile on an ext3 data-journaling
1456 * filesystem and enables swap, then they may get a nasty shock when the
1457 * data getting swapped to that swapfile suddenly gets overwritten by
1458 * the original zero's written out previously to the journal and
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001459 * awaiting writeback in the kernel's buffer cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 *
1461 * So, if we see any bmap calls here on a modified, data-journaled file,
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001462 * take extra steps to flush any blocks which might be in the cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 */
1464static sector_t ext3_bmap(struct address_space *mapping, sector_t block)
1465{
1466 struct inode *inode = mapping->host;
1467 journal_t *journal;
1468 int err;
1469
Jan Kara9df93932010-01-06 21:58:48 +01001470 if (ext3_test_inode_state(inode, EXT3_STATE_JDATA)) {
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001471 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 * This is a REALLY heavyweight approach, but the use of
1473 * bmap on dirty files is expected to be extremely rare:
1474 * only if we run lilo or swapon on a freshly made file
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001475 * do we expect this to happen.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 *
1477 * (bmap requires CAP_SYS_RAWIO so this does not
1478 * represent an unprivileged user DOS attack --- we'd be
1479 * in trouble if mortal users could trigger this path at
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001480 * will.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 *
1482 * NB. EXT3_STATE_JDATA is not set on files other than
1483 * regular files. If somebody wants to bmap a directory
1484 * or symlink and gets confused because the buffer
1485 * hasn't yet been flushed to disk, they deserve
1486 * everything they get.
1487 */
1488
Jan Kara9df93932010-01-06 21:58:48 +01001489 ext3_clear_inode_state(inode, EXT3_STATE_JDATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 journal = EXT3_JOURNAL(inode);
1491 journal_lock_updates(journal);
1492 err = journal_flush(journal);
1493 journal_unlock_updates(journal);
1494
1495 if (err)
1496 return 0;
1497 }
1498
1499 return generic_block_bmap(mapping,block,ext3_get_block);
1500}
1501
1502static int bget_one(handle_t *handle, struct buffer_head *bh)
1503{
1504 get_bh(bh);
1505 return 0;
1506}
1507
1508static int bput_one(handle_t *handle, struct buffer_head *bh)
1509{
1510 put_bh(bh);
1511 return 0;
1512}
1513
Jan Kara9e80d402009-03-26 13:08:04 +01001514static int buffer_unmapped(handle_t *handle, struct buffer_head *bh)
1515{
1516 return !buffer_mapped(bh);
1517}
Jan Kara695f6ae2009-04-02 16:57:17 -07001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519/*
1520 * Note that we always start a transaction even if we're not journalling
1521 * data. This is to preserve ordering: any hole instantiation within
1522 * __block_write_full_page -> ext3_get_block() should be journalled
1523 * along with the data so we don't crash and then get metadata which
1524 * refers to old data.
1525 *
1526 * In all journalling modes block_write_full_page() will start the I/O.
1527 *
1528 * Problem:
1529 *
1530 * ext3_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
1531 * ext3_writepage()
1532 *
1533 * Similar for:
1534 *
1535 * ext3_file_write() -> generic_file_write() -> __alloc_pages() -> ...
1536 *
1537 * Same applies to ext3_get_block(). We will deadlock on various things like
Arjan van de Ven97461512006-03-23 03:00:42 -08001538 * lock_journal and i_truncate_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 *
1540 * Setting PF_MEMALLOC here doesn't work - too many internal memory
1541 * allocations fail.
1542 *
1543 * 16May01: If we're reentered then journal_current_handle() will be
1544 * non-zero. We simply *return*.
1545 *
1546 * 1 July 2001: @@@ FIXME:
1547 * In journalled data mode, a data buffer may be metadata against the
1548 * current transaction. But the same file is part of a shared mapping
1549 * and someone does a writepage() on it.
1550 *
1551 * We will move the buffer onto the async_data list, but *after* it has
1552 * been dirtied. So there's a small window where we have dirty data on
1553 * BJ_Metadata.
1554 *
1555 * Note that this only applies to the last partial page in the file. The
1556 * bit which block_write_full_page() uses prepare/commit for. (That's
1557 * broken code anyway: it's wrong for msync()).
1558 *
1559 * It's a rare case: affects the final partial page, for journalled data
1560 * where the file is subject to bith write() and writepage() in the same
1561 * transction. To fix it we'll need a custom block_write_full_page().
1562 * We'll probably need that anyway for journalling writepage() output.
1563 *
1564 * We don't honour synchronous mounts for writepage(). That would be
1565 * disastrous. Any write() or metadata operation will sync the fs for
1566 * us.
1567 *
1568 * AKPM2: if all the page's buffers are mapped to disk and !data=journal,
1569 * we don't need to open a transaction here.
1570 */
1571static int ext3_ordered_writepage(struct page *page,
Andrew Mortond6859bf2006-03-26 01:38:03 -08001572 struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
1574 struct inode *inode = page->mapping->host;
1575 struct buffer_head *page_bufs;
1576 handle_t *handle = NULL;
1577 int ret = 0;
1578 int err;
1579
1580 J_ASSERT(PageLocked(page));
Dmitry Monakhov49792c82010-03-02 15:51:02 +03001581 WARN_ON_ONCE(IS_RDONLY(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 /*
1584 * We give up here if we're reentered, because it might be for a
1585 * different filesystem.
1586 */
1587 if (ext3_journal_current_handle())
1588 goto out_fail;
1589
Lukas Czerner785c4bc2011-05-23 18:33:01 +02001590 trace_ext3_ordered_writepage(page);
Jan Kara9e80d402009-03-26 13:08:04 +01001591 if (!page_has_buffers(page)) {
1592 create_empty_buffers(page, inode->i_sb->s_blocksize,
1593 (1 << BH_Dirty)|(1 << BH_Uptodate));
Jan Kara430db322009-04-07 18:25:01 -04001594 page_bufs = page_buffers(page);
1595 } else {
1596 page_bufs = page_buffers(page);
1597 if (!walk_page_buffers(NULL, page_bufs, 0, PAGE_CACHE_SIZE,
1598 NULL, buffer_unmapped)) {
1599 /* Provide NULL get_block() to catch bugs if buffers
1600 * weren't really mapped */
1601 return block_write_full_page(page, NULL, wbc);
1602 }
Jan Kara9e80d402009-03-26 13:08:04 +01001603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
1605
1606 if (IS_ERR(handle)) {
1607 ret = PTR_ERR(handle);
1608 goto out_fail;
1609 }
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 walk_page_buffers(handle, page_bufs, 0,
1612 PAGE_CACHE_SIZE, NULL, bget_one);
1613
1614 ret = block_write_full_page(page, ext3_get_block, wbc);
1615
1616 /*
1617 * The page can become unlocked at any point now, and
1618 * truncate can then come in and change things. So we
1619 * can't touch *page from now on. But *page_bufs is
1620 * safe due to elevated refcount.
1621 */
1622
1623 /*
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001624 * And attach them to the current transaction. But only if
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 * block_write_full_page() succeeded. Otherwise they are unmapped,
1626 * and generally junk.
1627 */
1628 if (ret == 0) {
1629 err = walk_page_buffers(handle, page_bufs, 0, PAGE_CACHE_SIZE,
1630 NULL, journal_dirty_data_fn);
1631 if (!ret)
1632 ret = err;
1633 }
1634 walk_page_buffers(handle, page_bufs, 0,
1635 PAGE_CACHE_SIZE, NULL, bput_one);
1636 err = ext3_journal_stop(handle);
1637 if (!ret)
1638 ret = err;
1639 return ret;
1640
1641out_fail:
1642 redirty_page_for_writepage(wbc, page);
1643 unlock_page(page);
1644 return ret;
1645}
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647static int ext3_writeback_writepage(struct page *page,
1648 struct writeback_control *wbc)
1649{
1650 struct inode *inode = page->mapping->host;
1651 handle_t *handle = NULL;
1652 int ret = 0;
1653 int err;
1654
Dmitry Monakhov49792c82010-03-02 15:51:02 +03001655 J_ASSERT(PageLocked(page));
1656 WARN_ON_ONCE(IS_RDONLY(inode));
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 if (ext3_journal_current_handle())
1659 goto out_fail;
1660
Lukas Czerner785c4bc2011-05-23 18:33:01 +02001661 trace_ext3_writeback_writepage(page);
Jan Kara430db322009-04-07 18:25:01 -04001662 if (page_has_buffers(page)) {
1663 if (!walk_page_buffers(NULL, page_buffers(page), 0,
1664 PAGE_CACHE_SIZE, NULL, buffer_unmapped)) {
1665 /* Provide NULL get_block() to catch bugs if buffers
1666 * weren't really mapped */
1667 return block_write_full_page(page, NULL, wbc);
1668 }
1669 }
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
1672 if (IS_ERR(handle)) {
1673 ret = PTR_ERR(handle);
1674 goto out_fail;
1675 }
1676
Christoph Hellwig4c4d3902010-06-07 10:20:39 +02001677 ret = block_write_full_page(page, ext3_get_block, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679 err = ext3_journal_stop(handle);
1680 if (!ret)
1681 ret = err;
1682 return ret;
1683
1684out_fail:
1685 redirty_page_for_writepage(wbc, page);
1686 unlock_page(page);
1687 return ret;
1688}
1689
1690static int ext3_journalled_writepage(struct page *page,
1691 struct writeback_control *wbc)
1692{
1693 struct inode *inode = page->mapping->host;
1694 handle_t *handle = NULL;
1695 int ret = 0;
1696 int err;
1697
Dmitry Monakhov49792c82010-03-02 15:51:02 +03001698 J_ASSERT(PageLocked(page));
1699 WARN_ON_ONCE(IS_RDONLY(inode));
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (ext3_journal_current_handle())
1702 goto no_write;
1703
Lukas Czerner785c4bc2011-05-23 18:33:01 +02001704 trace_ext3_journalled_writepage(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
1706 if (IS_ERR(handle)) {
1707 ret = PTR_ERR(handle);
1708 goto no_write;
1709 }
1710
1711 if (!page_has_buffers(page) || PageChecked(page)) {
1712 /*
1713 * It's mmapped pagecache. Add buffers and journal it. There
1714 * doesn't seem much point in redirtying the page here.
1715 */
1716 ClearPageChecked(page);
Christoph Hellwigebdec242010-10-06 10:47:23 +02001717 ret = __block_write_begin(page, 0, PAGE_CACHE_SIZE,
1718 ext3_get_block);
Denis Lunevab4eb432005-11-13 16:07:17 -08001719 if (ret != 0) {
1720 ext3_journal_stop(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 goto out_unlock;
Denis Lunevab4eb432005-11-13 16:07:17 -08001722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 ret = walk_page_buffers(handle, page_buffers(page), 0,
1724 PAGE_CACHE_SIZE, NULL, do_journal_get_write_access);
1725
1726 err = walk_page_buffers(handle, page_buffers(page), 0,
Nick Pigginf4fc66a2007-10-16 01:25:05 -07001727 PAGE_CACHE_SIZE, NULL, write_end_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 if (ret == 0)
1729 ret = err;
Jan Kara9df93932010-01-06 21:58:48 +01001730 ext3_set_inode_state(inode, EXT3_STATE_JDATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 unlock_page(page);
1732 } else {
1733 /*
1734 * It may be a page full of checkpoint-mode buffers. We don't
1735 * really know unless we go poke around in the buffer_heads.
1736 * But block_write_full_page will do the right thing.
1737 */
1738 ret = block_write_full_page(page, ext3_get_block, wbc);
1739 }
1740 err = ext3_journal_stop(handle);
1741 if (!ret)
1742 ret = err;
1743out:
1744 return ret;
1745
1746no_write:
1747 redirty_page_for_writepage(wbc, page);
1748out_unlock:
1749 unlock_page(page);
1750 goto out;
1751}
1752
1753static int ext3_readpage(struct file *file, struct page *page)
1754{
Lukas Czerner785c4bc2011-05-23 18:33:01 +02001755 trace_ext3_readpage(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return mpage_readpage(page, ext3_get_block);
1757}
1758
1759static int
1760ext3_readpages(struct file *file, struct address_space *mapping,
1761 struct list_head *pages, unsigned nr_pages)
1762{
1763 return mpage_readpages(mapping, pages, nr_pages, ext3_get_block);
1764}
1765
NeilBrown2ff28e22006-03-26 01:37:18 -08001766static void ext3_invalidatepage(struct page *page, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
1768 journal_t *journal = EXT3_JOURNAL(page->mapping->host);
1769
Lukas Czerner785c4bc2011-05-23 18:33:01 +02001770 trace_ext3_invalidatepage(page, offset);
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 /*
1773 * If it's a full truncate we just forget about the pending dirtying
1774 */
1775 if (offset == 0)
1776 ClearPageChecked(page);
1777
NeilBrown2ff28e22006-03-26 01:37:18 -08001778 journal_invalidatepage(journal, page, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
Al Viro27496a82005-10-21 03:20:48 -04001781static int ext3_releasepage(struct page *page, gfp_t wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
1783 journal_t *journal = EXT3_JOURNAL(page->mapping->host);
1784
Lukas Czerner785c4bc2011-05-23 18:33:01 +02001785 trace_ext3_releasepage(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 WARN_ON(PageChecked(page));
1787 if (!page_has_buffers(page))
1788 return 0;
1789 return journal_try_to_free_buffers(journal, page, wait);
1790}
1791
1792/*
1793 * If the O_DIRECT write will extend the file then add this inode to the
1794 * orphan list. So recovery will truncate it back to the original size
1795 * if the machine crashes during the write.
1796 *
1797 * If the O_DIRECT write is intantiating holes inside i_size and the machine
Jan Karabd1939d2008-02-06 01:40:21 -08001798 * crashes then stale disk data _may_ be exposed inside the file. But current
1799 * VFS code falls back into buffered path in that case so we are safe.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 */
1801static ssize_t ext3_direct_IO(int rw, struct kiocb *iocb,
1802 const struct iovec *iov, loff_t offset,
1803 unsigned long nr_segs)
1804{
1805 struct file *file = iocb->ki_filp;
1806 struct inode *inode = file->f_mapping->host;
1807 struct ext3_inode_info *ei = EXT3_I(inode);
Jan Karabd1939d2008-02-06 01:40:21 -08001808 handle_t *handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 ssize_t ret;
1810 int orphan = 0;
1811 size_t count = iov_length(iov, nr_segs);
Eric Sandeenea0174a2009-10-12 21:34:27 -05001812 int retries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Lukas Czerner785c4bc2011-05-23 18:33:01 +02001814 trace_ext3_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw);
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (rw == WRITE) {
1817 loff_t final_size = offset + count;
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (final_size > inode->i_size) {
Jan Karabd1939d2008-02-06 01:40:21 -08001820 /* Credits for sb + inode write */
1821 handle = ext3_journal_start(inode, 2);
1822 if (IS_ERR(handle)) {
1823 ret = PTR_ERR(handle);
1824 goto out;
1825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 ret = ext3_orphan_add(handle, inode);
Jan Karabd1939d2008-02-06 01:40:21 -08001827 if (ret) {
1828 ext3_journal_stop(handle);
1829 goto out;
1830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 orphan = 1;
1832 ei->i_disksize = inode->i_size;
Jan Karabd1939d2008-02-06 01:40:21 -08001833 ext3_journal_stop(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
1835 }
1836
Eric Sandeenea0174a2009-10-12 21:34:27 -05001837retry:
Mingming Caoae6ddcc2006-09-27 01:49:27 -07001838 ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 offset, nr_segs,
Badari Pulavartyf91a2ad2006-03-26 01:38:04 -08001840 ext3_get_block, NULL);
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02001841 /*
1842 * In case of error extending write may have instantiated a few
1843 * blocks outside i_size. Trim these off again.
1844 */
1845 if (unlikely((rw & WRITE) && ret < 0)) {
1846 loff_t isize = i_size_read(inode);
1847 loff_t end = offset + iov_length(iov, nr_segs);
1848
1849 if (end > isize)
Jan Kara40680f2f2011-05-24 22:24:47 +02001850 ext3_truncate_failed_write(inode);
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02001851 }
Eric Sandeenea0174a2009-10-12 21:34:27 -05001852 if (ret == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
1853 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Jan Karabd1939d2008-02-06 01:40:21 -08001855 if (orphan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 int err;
1857
Jan Karabd1939d2008-02-06 01:40:21 -08001858 /* Credits for sb + inode write */
1859 handle = ext3_journal_start(inode, 2);
1860 if (IS_ERR(handle)) {
1861 /* This is really bad luck. We've written the data
Jan Kara7eb49692010-03-01 14:02:37 +01001862 * but cannot extend i_size. Truncate allocated blocks
1863 * and pretend the write failed... */
Jan Kara40680f2f2011-05-24 22:24:47 +02001864 ext3_truncate_failed_write(inode);
Jan Karabd1939d2008-02-06 01:40:21 -08001865 ret = PTR_ERR(handle);
1866 goto out;
1867 }
1868 if (inode->i_nlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 ext3_orphan_del(handle, inode);
Jan Karabd1939d2008-02-06 01:40:21 -08001870 if (ret > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 loff_t end = offset + ret;
1872 if (end > inode->i_size) {
1873 ei->i_disksize = end;
1874 i_size_write(inode, end);
1875 /*
1876 * We're going to return a positive `ret'
1877 * here due to non-zero-length I/O, so there's
1878 * no way of reporting error returns from
1879 * ext3_mark_inode_dirty() to userspace. So
1880 * ignore it.
1881 */
1882 ext3_mark_inode_dirty(handle, inode);
1883 }
1884 }
1885 err = ext3_journal_stop(handle);
1886 if (ret == 0)
1887 ret = err;
1888 }
1889out:
Lukas Czerner785c4bc2011-05-23 18:33:01 +02001890 trace_ext3_direct_IO_exit(inode, offset,
1891 iov_length(iov, nr_segs), rw, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return ret;
1893}
1894
1895/*
1896 * Pages can be marked dirty completely asynchronously from ext3's journalling
1897 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
1898 * much here because ->set_page_dirty is called under VFS locks. The page is
1899 * not necessarily locked.
1900 *
1901 * We cannot just dirty the page and leave attached buffers clean, because the
1902 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
1903 * or jbddirty because all the journalling code will explode.
1904 *
1905 * So what we do is to mark the page "pending dirty" and next time writepage
1906 * is called, propagate that into the buffers appropriately.
1907 */
1908static int ext3_journalled_set_page_dirty(struct page *page)
1909{
1910 SetPageChecked(page);
1911 return __set_page_dirty_nobuffers(page);
1912}
1913
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001914static const struct address_space_operations ext3_ordered_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001915 .readpage = ext3_readpage,
1916 .readpages = ext3_readpages,
1917 .writepage = ext3_ordered_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001918 .write_begin = ext3_write_begin,
1919 .write_end = ext3_ordered_write_end,
1920 .bmap = ext3_bmap,
1921 .invalidatepage = ext3_invalidatepage,
1922 .releasepage = ext3_releasepage,
1923 .direct_IO = ext3_direct_IO,
1924 .migratepage = buffer_migrate_page,
1925 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02001926 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927};
1928
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001929static const struct address_space_operations ext3_writeback_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001930 .readpage = ext3_readpage,
1931 .readpages = ext3_readpages,
1932 .writepage = ext3_writeback_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001933 .write_begin = ext3_write_begin,
1934 .write_end = ext3_writeback_write_end,
1935 .bmap = ext3_bmap,
1936 .invalidatepage = ext3_invalidatepage,
1937 .releasepage = ext3_releasepage,
1938 .direct_IO = ext3_direct_IO,
1939 .migratepage = buffer_migrate_page,
1940 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02001941 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942};
1943
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001944static const struct address_space_operations ext3_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001945 .readpage = ext3_readpage,
1946 .readpages = ext3_readpages,
1947 .writepage = ext3_journalled_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07001948 .write_begin = ext3_write_begin,
1949 .write_end = ext3_journalled_write_end,
1950 .set_page_dirty = ext3_journalled_set_page_dirty,
1951 .bmap = ext3_bmap,
1952 .invalidatepage = ext3_invalidatepage,
1953 .releasepage = ext3_releasepage,
1954 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02001955 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956};
1957
1958void ext3_set_aops(struct inode *inode)
1959{
1960 if (ext3_should_order_data(inode))
1961 inode->i_mapping->a_ops = &ext3_ordered_aops;
1962 else if (ext3_should_writeback_data(inode))
1963 inode->i_mapping->a_ops = &ext3_writeback_aops;
1964 else
1965 inode->i_mapping->a_ops = &ext3_journalled_aops;
1966}
1967
1968/*
1969 * ext3_block_truncate_page() zeroes out a mapping from file offset `from'
1970 * up to the end of the block which corresponds to `from'.
1971 * This required during truncate. We need to physically zero the tail end
1972 * of that block so it doesn't yield old data if the file is later grown.
1973 */
1974static int ext3_block_truncate_page(handle_t *handle, struct page *page,
1975 struct address_space *mapping, loff_t from)
1976{
Mingming Cao43d23f92006-06-25 05:48:07 -07001977 ext3_fsblk_t index = from >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1979 unsigned blocksize, iblock, length, pos;
1980 struct inode *inode = mapping->host;
1981 struct buffer_head *bh;
1982 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 blocksize = inode->i_sb->s_blocksize;
1985 length = blocksize - (offset & (blocksize - 1));
1986 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 if (!page_has_buffers(page))
1989 create_empty_buffers(page, blocksize, 0);
1990
1991 /* Find the buffer that contains "offset" */
1992 bh = page_buffers(page);
1993 pos = blocksize;
1994 while (offset >= pos) {
1995 bh = bh->b_this_page;
1996 iblock++;
1997 pos += blocksize;
1998 }
1999
2000 err = 0;
2001 if (buffer_freed(bh)) {
2002 BUFFER_TRACE(bh, "freed: skip");
2003 goto unlock;
2004 }
2005
2006 if (!buffer_mapped(bh)) {
2007 BUFFER_TRACE(bh, "unmapped");
2008 ext3_get_block(inode, iblock, bh, 0);
2009 /* unmapped? It's a hole - nothing to do */
2010 if (!buffer_mapped(bh)) {
2011 BUFFER_TRACE(bh, "still unmapped");
2012 goto unlock;
2013 }
2014 }
2015
2016 /* Ok, it's mapped. Make sure it's up-to-date */
2017 if (PageUptodate(page))
2018 set_buffer_uptodate(bh);
2019
2020 if (!buffer_uptodate(bh)) {
2021 err = -EIO;
2022 ll_rw_block(READ, 1, &bh);
2023 wait_on_buffer(bh);
2024 /* Uhhuh. Read error. Complain and punt. */
2025 if (!buffer_uptodate(bh))
2026 goto unlock;
2027 }
2028
2029 if (ext3_should_journal_data(inode)) {
2030 BUFFER_TRACE(bh, "get write access");
2031 err = ext3_journal_get_write_access(handle, bh);
2032 if (err)
2033 goto unlock;
2034 }
2035
Christoph Lametereebd2aa2008-02-04 22:28:29 -08002036 zero_user(page, offset, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 BUFFER_TRACE(bh, "zeroed end of block");
2038
2039 err = 0;
2040 if (ext3_should_journal_data(inode)) {
2041 err = ext3_journal_dirty_metadata(handle, bh);
2042 } else {
2043 if (ext3_should_order_data(inode))
2044 err = ext3_journal_dirty_data(handle, bh);
2045 mark_buffer_dirty(bh);
2046 }
2047
2048unlock:
2049 unlock_page(page);
2050 page_cache_release(page);
2051 return err;
2052}
2053
2054/*
2055 * Probably it should be a library function... search for first non-zero word
2056 * or memcmp with zero_page, whatever is better for particular architecture.
2057 * Linus?
2058 */
2059static inline int all_zeroes(__le32 *p, __le32 *q)
2060{
2061 while (p < q)
2062 if (*p++)
2063 return 0;
2064 return 1;
2065}
2066
2067/**
2068 * ext3_find_shared - find the indirect blocks for partial truncation.
2069 * @inode: inode in question
2070 * @depth: depth of the affected branch
2071 * @offsets: offsets of pointers in that branch (see ext3_block_to_path)
2072 * @chain: place to store the pointers to partial indirect blocks
2073 * @top: place to the (detached) top of branch
2074 *
2075 * This is a helper function used by ext3_truncate().
2076 *
2077 * When we do truncate() we may have to clean the ends of several
2078 * indirect blocks but leave the blocks themselves alive. Block is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002079 * partially truncated if some data below the new i_size is referred
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 * from it (and it is on the path to the first completely truncated
2081 * data block, indeed). We have to free the top of that path along
2082 * with everything to the right of the path. Since no allocation
2083 * past the truncation point is possible until ext3_truncate()
2084 * finishes, we may safely do the latter, but top of branch may
2085 * require special attention - pageout below the truncation point
2086 * might try to populate it.
2087 *
2088 * We atomically detach the top of branch from the tree, store the
2089 * block number of its root in *@top, pointers to buffer_heads of
2090 * partially truncated blocks - in @chain[].bh and pointers to
2091 * their last elements that should not be removed - in
2092 * @chain[].p. Return value is the pointer to last filled element
2093 * of @chain.
2094 *
2095 * The work left to caller to do the actual freeing of subtrees:
2096 * a) free the subtree starting from *@top
2097 * b) free the subtrees whose roots are stored in
2098 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
2099 * c) free the subtrees growing from the inode past the @chain[0].
2100 * (no partially truncated stuff there). */
2101
Andrew Mortond6859bf2006-03-26 01:38:03 -08002102static Indirect *ext3_find_shared(struct inode *inode, int depth,
2103 int offsets[4], Indirect chain[4], __le32 *top)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
2105 Indirect *partial, *p;
2106 int k, err;
2107
2108 *top = 0;
Uwe Kleine-Königbf48aab2009-10-28 20:11:03 +01002109 /* Make k index the deepest non-null offset + 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 for (k = depth; k > 1 && !offsets[k-1]; k--)
2111 ;
2112 partial = ext3_get_branch(inode, k, offsets, chain, &err);
2113 /* Writer: pointers */
2114 if (!partial)
2115 partial = chain + k-1;
2116 /*
2117 * If the branch acquired continuation since we've looked at it -
2118 * fine, it should all survive and (new) top doesn't belong to us.
2119 */
2120 if (!partial->key && *partial->p)
2121 /* Writer: end */
2122 goto no_top;
2123 for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--)
2124 ;
2125 /*
2126 * OK, we've found the last block that must survive. The rest of our
2127 * branch should be detached before unlocking. However, if that rest
2128 * of branch is all ours and does not grow immediately from the inode
2129 * it's easier to cheat and just decrement partial->p.
2130 */
2131 if (p == chain + k - 1 && p > chain) {
2132 p->p--;
2133 } else {
2134 *top = *p->p;
2135 /* Nope, don't do this in ext3. Must leave the tree intact */
2136#if 0
2137 *p->p = 0;
2138#endif
2139 }
2140 /* Writer: end */
2141
Andrew Mortond6859bf2006-03-26 01:38:03 -08002142 while(partial > p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 brelse(partial->bh);
2144 partial--;
2145 }
2146no_top:
2147 return partial;
2148}
2149
2150/*
2151 * Zero a number of block pointers in either an inode or an indirect block.
2152 * If we restart the transaction we must again get write access to the
2153 * indirect block for further modification.
2154 *
2155 * We release `count' blocks on disk, but (last - first) may be greater
2156 * than `count' because there can be holes in there.
2157 */
Andrew Mortond6859bf2006-03-26 01:38:03 -08002158static void ext3_clear_blocks(handle_t *handle, struct inode *inode,
Mingming Cao43d23f92006-06-25 05:48:07 -07002159 struct buffer_head *bh, ext3_fsblk_t block_to_free,
Andrew Mortond6859bf2006-03-26 01:38:03 -08002160 unsigned long count, __le32 *first, __le32 *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
2162 __le32 *p;
2163 if (try_to_extend_transaction(handle, inode)) {
2164 if (bh) {
2165 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
Namhyung Kim156e7432010-11-25 01:53:13 +09002166 if (ext3_journal_dirty_metadata(handle, bh))
2167 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
2169 ext3_mark_inode_dirty(handle, inode);
Jan Kara00171d32009-08-11 19:06:10 +02002170 truncate_restart_transaction(handle, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if (bh) {
2172 BUFFER_TRACE(bh, "retaking write access");
Namhyung Kim156e7432010-11-25 01:53:13 +09002173 if (ext3_journal_get_write_access(handle, bh))
2174 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
2176 }
2177
2178 /*
2179 * Any buffers which are on the journal will be in memory. We find
2180 * them on the hash table so journal_revoke() will run journal_forget()
2181 * on them. We've already detached each block from the file, so
2182 * bforget() in journal_forget() should be safe.
2183 *
2184 * AKPM: turn on bforget in journal_forget()!!!
2185 */
2186 for (p = first; p < last; p++) {
2187 u32 nr = le32_to_cpu(*p);
2188 if (nr) {
2189 struct buffer_head *bh;
2190
2191 *p = 0;
2192 bh = sb_find_get_block(inode->i_sb, nr);
2193 ext3_forget(handle, 0, inode, bh, nr);
2194 }
2195 }
2196
2197 ext3_free_blocks(handle, inode, block_to_free, count);
2198}
2199
2200/**
2201 * ext3_free_data - free a list of data blocks
2202 * @handle: handle for this transaction
2203 * @inode: inode we are dealing with
2204 * @this_bh: indirect buffer_head which contains *@first and *@last
2205 * @first: array of block numbers
2206 * @last: points immediately past the end of array
2207 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002208 * We are freeing all blocks referred from that array (numbers are stored as
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 * little-endian 32-bit) and updating @inode->i_blocks appropriately.
2210 *
2211 * We accumulate contiguous runs of blocks to free. Conveniently, if these
2212 * blocks are contiguous then releasing them at one time will only affect one
2213 * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
2214 * actually use a lot of journal space.
2215 *
2216 * @this_bh will be %NULL if @first and @last point into the inode's direct
2217 * block pointers.
2218 */
2219static void ext3_free_data(handle_t *handle, struct inode *inode,
2220 struct buffer_head *this_bh,
2221 __le32 *first, __le32 *last)
2222{
Mingming Cao43d23f92006-06-25 05:48:07 -07002223 ext3_fsblk_t block_to_free = 0; /* Starting block # of a run */
Mingming Caoae6ddcc2006-09-27 01:49:27 -07002224 unsigned long count = 0; /* Number of blocks in the run */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
2226 corresponding to
2227 block_to_free */
Mingming Cao43d23f92006-06-25 05:48:07 -07002228 ext3_fsblk_t nr; /* Current block # */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 __le32 *p; /* Pointer into inode/ind
2230 for current block */
2231 int err;
2232
2233 if (this_bh) { /* For indirect block */
2234 BUFFER_TRACE(this_bh, "get_write_access");
2235 err = ext3_journal_get_write_access(handle, this_bh);
2236 /* Important: if we can't update the indirect pointers
2237 * to the blocks, we can't free them. */
2238 if (err)
2239 return;
2240 }
2241
2242 for (p = first; p < last; p++) {
2243 nr = le32_to_cpu(*p);
2244 if (nr) {
2245 /* accumulate blocks to free if they're contiguous */
2246 if (count == 0) {
2247 block_to_free = nr;
2248 block_to_free_p = p;
2249 count = 1;
2250 } else if (nr == block_to_free + count) {
2251 count++;
2252 } else {
Mingming Caoae6ddcc2006-09-27 01:49:27 -07002253 ext3_clear_blocks(handle, inode, this_bh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 block_to_free,
2255 count, block_to_free_p, p);
2256 block_to_free = nr;
2257 block_to_free_p = p;
2258 count = 1;
2259 }
2260 }
2261 }
2262
2263 if (count > 0)
2264 ext3_clear_blocks(handle, inode, this_bh, block_to_free,
2265 count, block_to_free_p, p);
2266
2267 if (this_bh) {
2268 BUFFER_TRACE(this_bh, "call ext3_journal_dirty_metadata");
Duane Griffin3ccc3162008-07-25 01:46:26 -07002269
2270 /*
2271 * The buffer head should have an attached journal head at this
2272 * point. However, if the data is corrupted and an indirect
2273 * block pointed to itself, it would have been detached when
2274 * the block was cleared. Check for this instead of OOPSing.
2275 */
2276 if (bh2jh(this_bh))
2277 ext3_journal_dirty_metadata(handle, this_bh);
2278 else
2279 ext3_error(inode->i_sb, "ext3_free_data",
2280 "circular indirect block detected, "
2281 "inode=%lu, block=%llu",
2282 inode->i_ino,
2283 (unsigned long long)this_bh->b_blocknr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
2285}
2286
2287/**
2288 * ext3_free_branches - free an array of branches
2289 * @handle: JBD handle for this transaction
2290 * @inode: inode we are dealing with
2291 * @parent_bh: the buffer_head which contains *@first and *@last
2292 * @first: array of block numbers
2293 * @last: pointer immediately past the end of array
2294 * @depth: depth of the branches to free
2295 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002296 * We are freeing all blocks referred from these branches (numbers are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 * stored as little-endian 32-bit) and updating @inode->i_blocks
2298 * appropriately.
2299 */
2300static void ext3_free_branches(handle_t *handle, struct inode *inode,
2301 struct buffer_head *parent_bh,
2302 __le32 *first, __le32 *last, int depth)
2303{
Mingming Cao43d23f92006-06-25 05:48:07 -07002304 ext3_fsblk_t nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 __le32 *p;
2306
2307 if (is_handle_aborted(handle))
2308 return;
2309
2310 if (depth--) {
2311 struct buffer_head *bh;
2312 int addr_per_block = EXT3_ADDR_PER_BLOCK(inode->i_sb);
2313 p = last;
2314 while (--p >= first) {
2315 nr = le32_to_cpu(*p);
2316 if (!nr)
2317 continue; /* A hole */
2318
2319 /* Go read the buffer for the next level down */
2320 bh = sb_bread(inode->i_sb, nr);
2321
2322 /*
2323 * A read failure? Report error and clear slot
2324 * (should be rare).
2325 */
2326 if (!bh) {
2327 ext3_error(inode->i_sb, "ext3_free_branches",
Eric Sandeeneee194e2006-09-27 01:49:30 -07002328 "Read failure, inode=%lu, block="E3FSBLK,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 inode->i_ino, nr);
2330 continue;
2331 }
2332
2333 /* This zaps the entire block. Bottom up. */
2334 BUFFER_TRACE(bh, "free child branches");
2335 ext3_free_branches(handle, inode, bh,
2336 (__le32*)bh->b_data,
2337 (__le32*)bh->b_data + addr_per_block,
2338 depth);
2339
2340 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 * Everything below this this pointer has been
2342 * released. Now let this top-of-subtree go.
2343 *
2344 * We want the freeing of this indirect block to be
2345 * atomic in the journal with the updating of the
2346 * bitmap block which owns it. So make some room in
2347 * the journal.
2348 *
2349 * We zero the parent pointer *after* freeing its
2350 * pointee in the bitmaps, so if extend_transaction()
2351 * for some reason fails to put the bitmap changes and
2352 * the release into the same transaction, recovery
2353 * will merely complain about releasing a free block,
2354 * rather than leaking blocks.
2355 */
2356 if (is_handle_aborted(handle))
2357 return;
2358 if (try_to_extend_transaction(handle, inode)) {
2359 ext3_mark_inode_dirty(handle, inode);
Jan Kara00171d32009-08-11 19:06:10 +02002360 truncate_restart_transaction(handle, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 }
2362
Jan Karaf25f6242010-07-12 21:04:31 +02002363 /*
2364 * We've probably journalled the indirect block several
2365 * times during the truncate. But it's no longer
2366 * needed and we now drop it from the transaction via
2367 * journal_revoke().
2368 *
2369 * That's easy if it's exclusively part of this
2370 * transaction. But if it's part of the committing
2371 * transaction then journal_forget() will simply
2372 * brelse() it. That means that if the underlying
2373 * block is reallocated in ext3_get_block(),
2374 * unmap_underlying_metadata() will find this block
2375 * and will try to get rid of it. damn, damn. Thus
2376 * we don't allow a block to be reallocated until
2377 * a transaction freeing it has fully committed.
2378 *
2379 * We also have to make sure journal replay after a
2380 * crash does not overwrite non-journaled data blocks
2381 * with old metadata when the block got reallocated for
2382 * data. Thus we have to store a revoke record for a
2383 * block in the same transaction in which we free the
2384 * block.
2385 */
2386 ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
2387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 ext3_free_blocks(handle, inode, nr, 1);
2389
2390 if (parent_bh) {
2391 /*
2392 * The block which we have just freed is
2393 * pointed to by an indirect block: journal it
2394 */
2395 BUFFER_TRACE(parent_bh, "get_write_access");
2396 if (!ext3_journal_get_write_access(handle,
2397 parent_bh)){
2398 *p = 0;
2399 BUFFER_TRACE(parent_bh,
2400 "call ext3_journal_dirty_metadata");
Mingming Caoae6ddcc2006-09-27 01:49:27 -07002401 ext3_journal_dirty_metadata(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 parent_bh);
2403 }
2404 }
2405 }
2406 } else {
2407 /* We have reached the bottom of the tree. */
2408 BUFFER_TRACE(parent_bh, "free data blocks");
2409 ext3_free_data(handle, inode, parent_bh, first, last);
2410 }
2411}
2412
Duane Griffinae76dd92008-07-25 01:46:23 -07002413int ext3_can_truncate(struct inode *inode)
2414{
Duane Griffinae76dd92008-07-25 01:46:23 -07002415 if (S_ISREG(inode->i_mode))
2416 return 1;
2417 if (S_ISDIR(inode->i_mode))
2418 return 1;
2419 if (S_ISLNK(inode->i_mode))
2420 return !ext3_inode_is_fast_symlink(inode);
2421 return 0;
2422}
2423
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424/*
2425 * ext3_truncate()
2426 *
2427 * We block out ext3_get_block() block instantiations across the entire
2428 * transaction, and VFS/VM ensures that ext3_truncate() cannot run
2429 * simultaneously on behalf of the same inode.
2430 *
2431 * As we work through the truncate and commmit bits of it to the journal there
2432 * is one core, guiding principle: the file's tree must always be consistent on
2433 * disk. We must be able to restart the truncate after a crash.
2434 *
2435 * The file's tree may be transiently inconsistent in memory (although it
2436 * probably isn't), but whenever we close off and commit a journal transaction,
2437 * the contents of (the filesystem + the journal) must be consistent and
2438 * restartable. It's pretty simple, really: bottom up, right to left (although
2439 * left-to-right works OK too).
2440 *
2441 * Note that at recovery time, journal replay occurs *before* the restart of
2442 * truncate against the orphan inode list.
2443 *
2444 * The committed inode has the new, desired i_size (which is the same as
2445 * i_disksize in this case). After a crash, ext3_orphan_cleanup() will see
2446 * that this inode's truncate did not complete and it will again call
2447 * ext3_truncate() to have another go. So there will be instantiated blocks
2448 * to the right of the truncation point in a crashed ext3 filesystem. But
2449 * that's fine - as long as they are linked from the inode, the post-crash
2450 * ext3_truncate() run will find them and release them.
2451 */
Andrew Mortond6859bf2006-03-26 01:38:03 -08002452void ext3_truncate(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
2454 handle_t *handle;
2455 struct ext3_inode_info *ei = EXT3_I(inode);
2456 __le32 *i_data = ei->i_data;
2457 int addr_per_block = EXT3_ADDR_PER_BLOCK(inode->i_sb);
2458 struct address_space *mapping = inode->i_mapping;
2459 int offsets[4];
2460 Indirect chain[4];
2461 Indirect *partial;
2462 __le32 nr = 0;
2463 int n;
2464 long last_block;
2465 unsigned blocksize = inode->i_sb->s_blocksize;
2466 struct page *page;
2467
Lukas Czerner785c4bc2011-05-23 18:33:01 +02002468 trace_ext3_truncate_enter(inode);
2469
Duane Griffinae76dd92008-07-25 01:46:23 -07002470 if (!ext3_can_truncate(inode))
Jan Karaef436182009-06-17 16:26:24 -07002471 goto out_notrans;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Theodore Ts'of7ab34e2009-04-03 01:34:35 -04002473 if (inode->i_size == 0 && ext3_should_writeback_data(inode))
Jan Kara9df93932010-01-06 21:58:48 +01002474 ext3_set_inode_state(inode, EXT3_STATE_FLUSH_ON_CLOSE);
Theodore Ts'of7ab34e2009-04-03 01:34:35 -04002475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 /*
2477 * We have to lock the EOF page here, because lock_page() nests
2478 * outside journal_start().
2479 */
2480 if ((inode->i_size & (blocksize - 1)) == 0) {
2481 /* Block boundary? Nothing to do */
2482 page = NULL;
2483 } else {
2484 page = grab_cache_page(mapping,
2485 inode->i_size >> PAGE_CACHE_SHIFT);
2486 if (!page)
Jan Karaef436182009-06-17 16:26:24 -07002487 goto out_notrans;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
2489
2490 handle = start_transaction(inode);
2491 if (IS_ERR(handle)) {
2492 if (page) {
2493 clear_highpage(page);
2494 flush_dcache_page(page);
2495 unlock_page(page);
2496 page_cache_release(page);
2497 }
Jan Karaef436182009-06-17 16:26:24 -07002498 goto out_notrans;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 }
2500
2501 last_block = (inode->i_size + blocksize-1)
2502 >> EXT3_BLOCK_SIZE_BITS(inode->i_sb);
2503
2504 if (page)
2505 ext3_block_truncate_page(handle, page, mapping, inode->i_size);
2506
2507 n = ext3_block_to_path(inode, last_block, offsets, NULL);
2508 if (n == 0)
2509 goto out_stop; /* error */
2510
2511 /*
2512 * OK. This truncate is going to happen. We add the inode to the
2513 * orphan list, so that if this truncate spans multiple transactions,
2514 * and we crash, we will resume the truncate when the filesystem
2515 * recovers. It also marks the inode dirty, to catch the new size.
2516 *
2517 * Implication: the file must always be in a sane, consistent
2518 * truncatable state while each transaction commits.
2519 */
2520 if (ext3_orphan_add(handle, inode))
2521 goto out_stop;
2522
2523 /*
2524 * The orphan list entry will now protect us from any crash which
2525 * occurs before the truncate completes, so it is now safe to propagate
2526 * the new, shorter inode size (held for now in i_size) into the
2527 * on-disk inode. We do this via i_disksize, which is the value which
2528 * ext3 *really* writes onto the disk inode.
2529 */
2530 ei->i_disksize = inode->i_size;
2531
2532 /*
2533 * From here we block out all ext3_get_block() callers who want to
2534 * modify the block allocation tree.
2535 */
Arjan van de Ven97461512006-03-23 03:00:42 -08002536 mutex_lock(&ei->truncate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
2538 if (n == 1) { /* direct blocks */
2539 ext3_free_data(handle, inode, NULL, i_data+offsets[0],
2540 i_data + EXT3_NDIR_BLOCKS);
2541 goto do_indirects;
2542 }
2543
2544 partial = ext3_find_shared(inode, n, offsets, chain, &nr);
2545 /* Kill the top of shared branch (not detached) */
2546 if (nr) {
2547 if (partial == chain) {
2548 /* Shared branch grows from the inode */
2549 ext3_free_branches(handle, inode, NULL,
2550 &nr, &nr+1, (chain+n-1) - partial);
2551 *partial->p = 0;
2552 /*
2553 * We mark the inode dirty prior to restart,
2554 * and prior to stop. No need for it here.
2555 */
2556 } else {
2557 /* Shared branch grows from an indirect block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 ext3_free_branches(handle, inode, partial->bh,
2559 partial->p,
2560 partial->p+1, (chain+n-1) - partial);
2561 }
2562 }
2563 /* Clear the ends of indirect blocks on the shared branch */
2564 while (partial > chain) {
2565 ext3_free_branches(handle, inode, partial->bh, partial->p + 1,
2566 (__le32*)partial->bh->b_data+addr_per_block,
2567 (chain+n-1) - partial);
2568 BUFFER_TRACE(partial->bh, "call brelse");
2569 brelse (partial->bh);
2570 partial--;
2571 }
2572do_indirects:
2573 /* Kill the remaining (whole) subtrees */
2574 switch (offsets[0]) {
Andrew Mortond6859bf2006-03-26 01:38:03 -08002575 default:
2576 nr = i_data[EXT3_IND_BLOCK];
2577 if (nr) {
2578 ext3_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
2579 i_data[EXT3_IND_BLOCK] = 0;
2580 }
2581 case EXT3_IND_BLOCK:
2582 nr = i_data[EXT3_DIND_BLOCK];
2583 if (nr) {
2584 ext3_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
2585 i_data[EXT3_DIND_BLOCK] = 0;
2586 }
2587 case EXT3_DIND_BLOCK:
2588 nr = i_data[EXT3_TIND_BLOCK];
2589 if (nr) {
2590 ext3_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
2591 i_data[EXT3_TIND_BLOCK] = 0;
2592 }
2593 case EXT3_TIND_BLOCK:
2594 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 }
2596
2597 ext3_discard_reservation(inode);
2598
Arjan van de Ven97461512006-03-23 03:00:42 -08002599 mutex_unlock(&ei->truncate_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
2601 ext3_mark_inode_dirty(handle, inode);
2602
Andrew Mortond6859bf2006-03-26 01:38:03 -08002603 /*
2604 * In a multi-transaction truncate, we only make the final transaction
2605 * synchronous
2606 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 if (IS_SYNC(inode))
2608 handle->h_sync = 1;
2609out_stop:
2610 /*
2611 * If this was a simple ftruncate(), and the file will remain alive
2612 * then we need to clear up the orphan record which we created above.
2613 * However, if this was a real unlink then we were called by
Al Viroac14a952010-06-06 07:08:19 -04002614 * ext3_evict_inode(), and we allow that function to clean up the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 * orphan info for us.
2616 */
2617 if (inode->i_nlink)
2618 ext3_orphan_del(handle, inode);
2619
2620 ext3_journal_stop(handle);
Lukas Czerner785c4bc2011-05-23 18:33:01 +02002621 trace_ext3_truncate_exit(inode);
Jan Karaef436182009-06-17 16:26:24 -07002622 return;
2623out_notrans:
2624 /*
2625 * Delete the inode from orphan list so that it doesn't stay there
2626 * forever and trigger assertion on umount.
2627 */
2628 if (inode->i_nlink)
2629 ext3_orphan_del(NULL, inode);
Lukas Czerner785c4bc2011-05-23 18:33:01 +02002630 trace_ext3_truncate_exit(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631}
2632
Mingming Cao43d23f92006-06-25 05:48:07 -07002633static ext3_fsblk_t ext3_get_inode_block(struct super_block *sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 unsigned long ino, struct ext3_iloc *iloc)
2635{
Akinobu Mitae0e369a2008-04-28 02:16:08 -07002636 unsigned long block_group;
Mingming Cao43d23f92006-06-25 05:48:07 -07002637 unsigned long offset;
2638 ext3_fsblk_t block;
Akinobu Mitae0e369a2008-04-28 02:16:08 -07002639 struct ext3_group_desc *gdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Neil Brown2ccb48e2006-07-30 03:03:01 -07002641 if (!ext3_valid_inum(sb, ino)) {
2642 /*
2643 * This error is already checked for in namei.c unless we are
2644 * looking at an NFS filehandle, in which case no error
2645 * report is needed
2646 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 return 0;
2648 }
Neil Brown2ccb48e2006-07-30 03:03:01 -07002649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
Akinobu Mitae0e369a2008-04-28 02:16:08 -07002651 gdp = ext3_get_group_desc(sb, block_group, NULL);
2652 if (!gdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 /*
2655 * Figure out the offset within the block group inode table
2656 */
2657 offset = ((ino - 1) % EXT3_INODES_PER_GROUP(sb)) *
2658 EXT3_INODE_SIZE(sb);
Akinobu Mitae0e369a2008-04-28 02:16:08 -07002659 block = le32_to_cpu(gdp->bg_inode_table) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 (offset >> EXT3_BLOCK_SIZE_BITS(sb));
2661
2662 iloc->block_group = block_group;
2663 iloc->offset = offset & (EXT3_BLOCK_SIZE(sb) - 1);
2664 return block;
2665}
2666
2667/*
2668 * ext3_get_inode_loc returns with an extra refcount against the inode's
2669 * underlying buffer_head on success. If 'in_mem' is true, we have all
2670 * data in memory that is needed to recreate the on-disk version of this
2671 * inode.
2672 */
2673static int __ext3_get_inode_loc(struct inode *inode,
2674 struct ext3_iloc *iloc, int in_mem)
2675{
Mingming Cao43d23f92006-06-25 05:48:07 -07002676 ext3_fsblk_t block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 struct buffer_head *bh;
2678
2679 block = ext3_get_inode_block(inode->i_sb, inode->i_ino, iloc);
2680 if (!block)
2681 return -EIO;
2682
2683 bh = sb_getblk(inode->i_sb, block);
2684 if (!bh) {
2685 ext3_error (inode->i_sb, "ext3_get_inode_loc",
2686 "unable to read inode block - "
Mingming Cao43d23f92006-06-25 05:48:07 -07002687 "inode=%lu, block="E3FSBLK,
2688 inode->i_ino, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 return -EIO;
2690 }
2691 if (!buffer_uptodate(bh)) {
2692 lock_buffer(bh);
Hidehiro Kawai95450f52008-07-25 01:46:24 -07002693
2694 /*
2695 * If the buffer has the write error flag, we have failed
2696 * to write out another inode in the same block. In this
2697 * case, we don't have to read the block because we may
2698 * read the old inode data successfully.
2699 */
2700 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
2701 set_buffer_uptodate(bh);
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 if (buffer_uptodate(bh)) {
2704 /* someone brought it uptodate while we waited */
2705 unlock_buffer(bh);
2706 goto has_buffer;
2707 }
2708
2709 /*
2710 * If we have all information of the inode in memory and this
2711 * is the only valid inode in the block, we need not read the
2712 * block.
2713 */
2714 if (in_mem) {
2715 struct buffer_head *bitmap_bh;
2716 struct ext3_group_desc *desc;
2717 int inodes_per_buffer;
2718 int inode_offset, i;
2719 int block_group;
2720 int start;
2721
2722 block_group = (inode->i_ino - 1) /
2723 EXT3_INODES_PER_GROUP(inode->i_sb);
2724 inodes_per_buffer = bh->b_size /
2725 EXT3_INODE_SIZE(inode->i_sb);
2726 inode_offset = ((inode->i_ino - 1) %
2727 EXT3_INODES_PER_GROUP(inode->i_sb));
2728 start = inode_offset & ~(inodes_per_buffer - 1);
2729
2730 /* Is the inode bitmap in cache? */
2731 desc = ext3_get_group_desc(inode->i_sb,
2732 block_group, NULL);
2733 if (!desc)
2734 goto make_io;
2735
2736 bitmap_bh = sb_getblk(inode->i_sb,
2737 le32_to_cpu(desc->bg_inode_bitmap));
2738 if (!bitmap_bh)
2739 goto make_io;
2740
2741 /*
2742 * If the inode bitmap isn't in cache then the
2743 * optimisation may end up performing two reads instead
2744 * of one, so skip it.
2745 */
2746 if (!buffer_uptodate(bitmap_bh)) {
2747 brelse(bitmap_bh);
2748 goto make_io;
2749 }
2750 for (i = start; i < start + inodes_per_buffer; i++) {
2751 if (i == inode_offset)
2752 continue;
2753 if (ext3_test_bit(i, bitmap_bh->b_data))
2754 break;
2755 }
2756 brelse(bitmap_bh);
2757 if (i == start + inodes_per_buffer) {
2758 /* all other inodes are free, so skip I/O */
2759 memset(bh->b_data, 0, bh->b_size);
2760 set_buffer_uptodate(bh);
2761 unlock_buffer(bh);
2762 goto has_buffer;
2763 }
2764 }
2765
2766make_io:
2767 /*
2768 * There are other valid inodes in the buffer, this inode
2769 * has in-inode xattrs, or we don't have this inode in memory.
2770 * Read the block from disk.
2771 */
Lukas Czerner785c4bc2011-05-23 18:33:01 +02002772 trace_ext3_load_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 get_bh(bh);
2774 bh->b_end_io = end_buffer_read_sync;
Jens Axboecaa38fb2006-07-23 01:41:26 +02002775 submit_bh(READ_META, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 wait_on_buffer(bh);
2777 if (!buffer_uptodate(bh)) {
2778 ext3_error(inode->i_sb, "ext3_get_inode_loc",
2779 "unable to read inode block - "
Mingming Cao43d23f92006-06-25 05:48:07 -07002780 "inode=%lu, block="E3FSBLK,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 inode->i_ino, block);
2782 brelse(bh);
2783 return -EIO;
2784 }
2785 }
2786has_buffer:
2787 iloc->bh = bh;
2788 return 0;
2789}
2790
2791int ext3_get_inode_loc(struct inode *inode, struct ext3_iloc *iloc)
2792{
2793 /* We have all inode data except xattrs in memory here. */
2794 return __ext3_get_inode_loc(inode, iloc,
Jan Kara9df93932010-01-06 21:58:48 +01002795 !ext3_test_inode_state(inode, EXT3_STATE_XATTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796}
2797
2798void ext3_set_inode_flags(struct inode *inode)
2799{
2800 unsigned int flags = EXT3_I(inode)->i_flags;
2801
2802 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
2803 if (flags & EXT3_SYNC_FL)
2804 inode->i_flags |= S_SYNC;
2805 if (flags & EXT3_APPEND_FL)
2806 inode->i_flags |= S_APPEND;
2807 if (flags & EXT3_IMMUTABLE_FL)
2808 inode->i_flags |= S_IMMUTABLE;
2809 if (flags & EXT3_NOATIME_FL)
2810 inode->i_flags |= S_NOATIME;
2811 if (flags & EXT3_DIRSYNC_FL)
2812 inode->i_flags |= S_DIRSYNC;
2813}
2814
Jan Kara28be5ab2007-05-08 00:30:33 -07002815/* Propagate flags from i_flags to EXT3_I(inode)->i_flags */
2816void ext3_get_inode_flags(struct ext3_inode_info *ei)
2817{
2818 unsigned int flags = ei->vfs_inode.i_flags;
2819
2820 ei->i_flags &= ~(EXT3_SYNC_FL|EXT3_APPEND_FL|
2821 EXT3_IMMUTABLE_FL|EXT3_NOATIME_FL|EXT3_DIRSYNC_FL);
2822 if (flags & S_SYNC)
2823 ei->i_flags |= EXT3_SYNC_FL;
2824 if (flags & S_APPEND)
2825 ei->i_flags |= EXT3_APPEND_FL;
2826 if (flags & S_IMMUTABLE)
2827 ei->i_flags |= EXT3_IMMUTABLE_FL;
2828 if (flags & S_NOATIME)
2829 ei->i_flags |= EXT3_NOATIME_FL;
2830 if (flags & S_DIRSYNC)
2831 ei->i_flags |= EXT3_DIRSYNC_FL;
2832}
2833
David Howells473043d2008-02-07 00:15:36 -08002834struct inode *ext3_iget(struct super_block *sb, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835{
2836 struct ext3_iloc iloc;
2837 struct ext3_inode *raw_inode;
David Howells473043d2008-02-07 00:15:36 -08002838 struct ext3_inode_info *ei;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 struct buffer_head *bh;
David Howells473043d2008-02-07 00:15:36 -08002840 struct inode *inode;
Jan Karafe8bc912009-10-16 19:26:15 +02002841 journal_t *journal = EXT3_SB(sb)->s_journal;
2842 transaction_t *transaction;
David Howells473043d2008-02-07 00:15:36 -08002843 long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 int block;
2845
David Howells473043d2008-02-07 00:15:36 -08002846 inode = iget_locked(sb, ino);
2847 if (!inode)
2848 return ERR_PTR(-ENOMEM);
2849 if (!(inode->i_state & I_NEW))
2850 return inode;
2851
2852 ei = EXT3_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 ei->i_block_alloc_info = NULL;
2854
David Howells473043d2008-02-07 00:15:36 -08002855 ret = __ext3_get_inode_loc(inode, &iloc, 0);
2856 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 goto bad_inode;
2858 bh = iloc.bh;
2859 raw_inode = ext3_raw_inode(&iloc);
2860 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
2861 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
2862 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
2863 if(!(test_opt (inode->i_sb, NO_UID32))) {
2864 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
2865 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
2866 }
2867 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
2868 inode->i_size = le32_to_cpu(raw_inode->i_size);
Markus Rechberger4d7bf112007-05-08 00:23:39 -07002869 inode->i_atime.tv_sec = (signed)le32_to_cpu(raw_inode->i_atime);
2870 inode->i_ctime.tv_sec = (signed)le32_to_cpu(raw_inode->i_ctime);
2871 inode->i_mtime.tv_sec = (signed)le32_to_cpu(raw_inode->i_mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = inode->i_mtime.tv_nsec = 0;
2873
Linus Torvaldsde329822010-03-29 14:30:19 -07002874 ei->i_state_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 ei->i_dir_start_lookup = 0;
2876 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
2877 /* We now have enough fields to check if the inode was active or not.
2878 * This is needed because nfsd might try to access dead inodes
2879 * the test is that same one that e2fsck uses
2880 * NeilBrown 1999oct15
2881 */
2882 if (inode->i_nlink == 0) {
2883 if (inode->i_mode == 0 ||
2884 !(EXT3_SB(inode->i_sb)->s_mount_state & EXT3_ORPHAN_FS)) {
2885 /* this inode is deleted */
2886 brelse (bh);
David Howells473043d2008-02-07 00:15:36 -08002887 ret = -ESTALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 goto bad_inode;
2889 }
2890 /* The only unlinked inodes we let through here have
2891 * valid i_mode and are being read by the orphan
2892 * recovery code: that's fine, we're about to complete
2893 * the process of deleting those. */
2894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
2896 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
2897#ifdef EXT3_FRAGMENTS
2898 ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
2899 ei->i_frag_no = raw_inode->i_frag;
2900 ei->i_frag_size = raw_inode->i_fsize;
2901#endif
2902 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
2903 if (!S_ISREG(inode->i_mode)) {
2904 ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
2905 } else {
2906 inode->i_size |=
2907 ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
2908 }
2909 ei->i_disksize = inode->i_size;
2910 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
2911 ei->i_block_group = iloc.block_group;
2912 /*
2913 * NOTE! The in-memory inode i_data array is in little-endian order
2914 * even on big-endian machines: we do NOT byteswap the block numbers!
2915 */
2916 for (block = 0; block < EXT3_N_BLOCKS; block++)
2917 ei->i_data[block] = raw_inode->i_block[block];
2918 INIT_LIST_HEAD(&ei->i_orphan);
2919
Jan Karafe8bc912009-10-16 19:26:15 +02002920 /*
2921 * Set transaction id's of transactions that have to be committed
2922 * to finish f[data]sync. We set them to currently running transaction
2923 * as we cannot be sure that the inode or some of its metadata isn't
2924 * part of the transaction - the inode could have been reclaimed and
2925 * now it is reread from disk.
2926 */
2927 if (journal) {
2928 tid_t tid;
2929
2930 spin_lock(&journal->j_state_lock);
2931 if (journal->j_running_transaction)
2932 transaction = journal->j_running_transaction;
2933 else
2934 transaction = journal->j_committing_transaction;
2935 if (transaction)
2936 tid = transaction->t_tid;
2937 else
2938 tid = journal->j_commit_sequence;
2939 spin_unlock(&journal->j_state_lock);
2940 atomic_set(&ei->i_sync_tid, tid);
2941 atomic_set(&ei->i_datasync_tid, tid);
2942 }
2943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 if (inode->i_ino >= EXT3_FIRST_INO(inode->i_sb) + 1 &&
2945 EXT3_INODE_SIZE(inode->i_sb) > EXT3_GOOD_OLD_INODE_SIZE) {
2946 /*
2947 * When mke2fs creates big inodes it does not zero out
2948 * the unused bytes above EXT3_GOOD_OLD_INODE_SIZE,
2949 * so ignore those first few inodes.
2950 */
2951 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
2952 if (EXT3_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
Kirill Korotaeve4a10a32007-06-23 17:16:48 -07002953 EXT3_INODE_SIZE(inode->i_sb)) {
2954 brelse (bh);
David Howells473043d2008-02-07 00:15:36 -08002955 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 goto bad_inode;
Kirill Korotaeve4a10a32007-06-23 17:16:48 -07002957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 if (ei->i_extra_isize == 0) {
2959 /* The extra space is currently unused. Use it. */
2960 ei->i_extra_isize = sizeof(struct ext3_inode) -
2961 EXT3_GOOD_OLD_INODE_SIZE;
2962 } else {
2963 __le32 *magic = (void *)raw_inode +
2964 EXT3_GOOD_OLD_INODE_SIZE +
2965 ei->i_extra_isize;
2966 if (*magic == cpu_to_le32(EXT3_XATTR_MAGIC))
Jan Kara9df93932010-01-06 21:58:48 +01002967 ext3_set_inode_state(inode, EXT3_STATE_XATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 }
2969 } else
2970 ei->i_extra_isize = 0;
2971
2972 if (S_ISREG(inode->i_mode)) {
2973 inode->i_op = &ext3_file_inode_operations;
2974 inode->i_fop = &ext3_file_operations;
2975 ext3_set_aops(inode);
2976 } else if (S_ISDIR(inode->i_mode)) {
2977 inode->i_op = &ext3_dir_inode_operations;
2978 inode->i_fop = &ext3_dir_operations;
2979 } else if (S_ISLNK(inode->i_mode)) {
Duane Griffinb5ed3112008-12-19 20:47:14 +00002980 if (ext3_inode_is_fast_symlink(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 inode->i_op = &ext3_fast_symlink_inode_operations;
Duane Griffinb5ed3112008-12-19 20:47:14 +00002982 nd_terminate_link(ei->i_data, inode->i_size,
2983 sizeof(ei->i_data) - 1);
2984 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 inode->i_op = &ext3_symlink_inode_operations;
2986 ext3_set_aops(inode);
2987 }
2988 } else {
2989 inode->i_op = &ext3_special_inode_operations;
2990 if (raw_inode->i_block[0])
2991 init_special_inode(inode, inode->i_mode,
2992 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
Mingming Caoae6ddcc2006-09-27 01:49:27 -07002993 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 init_special_inode(inode, inode->i_mode,
2995 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
2996 }
2997 brelse (iloc.bh);
2998 ext3_set_inode_flags(inode);
David Howells473043d2008-02-07 00:15:36 -08002999 unlock_new_inode(inode);
3000 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
3002bad_inode:
David Howells473043d2008-02-07 00:15:36 -08003003 iget_failed(inode);
3004 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005}
3006
3007/*
3008 * Post the struct inode info into an on-disk inode location in the
3009 * buffer-cache. This gobbles the caller's reference to the
3010 * buffer_head in the inode location struct.
3011 *
3012 * The caller must have write access to iloc->bh.
3013 */
Mingming Caoae6ddcc2006-09-27 01:49:27 -07003014static int ext3_do_update_inode(handle_t *handle,
3015 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 struct ext3_iloc *iloc)
3017{
3018 struct ext3_inode *raw_inode = ext3_raw_inode(iloc);
3019 struct ext3_inode_info *ei = EXT3_I(inode);
3020 struct buffer_head *bh = iloc->bh;
3021 int err = 0, rc, block;
3022
Chris Mason4f003fd2009-09-08 00:22:14 +02003023again:
3024 /* we can't allow multiple procs in here at once, its a bit racey */
3025 lock_buffer(bh);
3026
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 /* For fields not not tracking in the in-memory inode,
3028 * initialise them to zero for new inodes. */
Jan Kara9df93932010-01-06 21:58:48 +01003029 if (ext3_test_inode_state(inode, EXT3_STATE_NEW))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
3031
Jan Kara28be5ab2007-05-08 00:30:33 -07003032 ext3_get_inode_flags(ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
3034 if(!(test_opt(inode->i_sb, NO_UID32))) {
3035 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
3036 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
3037/*
3038 * Fix up interoperability with old kernels. Otherwise, old inodes get
3039 * re-used with the upper 16 bits of the uid/gid intact
3040 */
3041 if(!ei->i_dtime) {
3042 raw_inode->i_uid_high =
3043 cpu_to_le16(high_16_bits(inode->i_uid));
3044 raw_inode->i_gid_high =
3045 cpu_to_le16(high_16_bits(inode->i_gid));
3046 } else {
3047 raw_inode->i_uid_high = 0;
3048 raw_inode->i_gid_high = 0;
3049 }
3050 } else {
3051 raw_inode->i_uid_low =
3052 cpu_to_le16(fs_high2lowuid(inode->i_uid));
3053 raw_inode->i_gid_low =
3054 cpu_to_le16(fs_high2lowgid(inode->i_gid));
3055 raw_inode->i_uid_high = 0;
3056 raw_inode->i_gid_high = 0;
3057 }
3058 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
3059 raw_inode->i_size = cpu_to_le32(ei->i_disksize);
3060 raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
3061 raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
3062 raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
3063 raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
3064 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
3065 raw_inode->i_flags = cpu_to_le32(ei->i_flags);
3066#ifdef EXT3_FRAGMENTS
3067 raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
3068 raw_inode->i_frag = ei->i_frag_no;
3069 raw_inode->i_fsize = ei->i_frag_size;
3070#endif
3071 raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
3072 if (!S_ISREG(inode->i_mode)) {
3073 raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
3074 } else {
3075 raw_inode->i_size_high =
3076 cpu_to_le32(ei->i_disksize >> 32);
3077 if (ei->i_disksize > 0x7fffffffULL) {
3078 struct super_block *sb = inode->i_sb;
3079 if (!EXT3_HAS_RO_COMPAT_FEATURE(sb,
3080 EXT3_FEATURE_RO_COMPAT_LARGE_FILE) ||
3081 EXT3_SB(sb)->s_es->s_rev_level ==
3082 cpu_to_le32(EXT3_GOOD_OLD_REV)) {
3083 /* If this is the first large file
3084 * created, add a flag to the superblock.
3085 */
Chris Mason4f003fd2009-09-08 00:22:14 +02003086 unlock_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 err = ext3_journal_get_write_access(handle,
3088 EXT3_SB(sb)->s_sbh);
3089 if (err)
3090 goto out_brelse;
Chris Mason4f003fd2009-09-08 00:22:14 +02003091
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 ext3_update_dynamic_rev(sb);
3093 EXT3_SET_RO_COMPAT_FEATURE(sb,
3094 EXT3_FEATURE_RO_COMPAT_LARGE_FILE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 handle->h_sync = 1;
3096 err = ext3_journal_dirty_metadata(handle,
3097 EXT3_SB(sb)->s_sbh);
Chris Mason4f003fd2009-09-08 00:22:14 +02003098 /* get our lock and start over */
3099 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 }
3101 }
3102 }
3103 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
3104 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
3105 if (old_valid_dev(inode->i_rdev)) {
3106 raw_inode->i_block[0] =
3107 cpu_to_le32(old_encode_dev(inode->i_rdev));
3108 raw_inode->i_block[1] = 0;
3109 } else {
3110 raw_inode->i_block[0] = 0;
3111 raw_inode->i_block[1] =
3112 cpu_to_le32(new_encode_dev(inode->i_rdev));
3113 raw_inode->i_block[2] = 0;
3114 }
3115 } else for (block = 0; block < EXT3_N_BLOCKS; block++)
3116 raw_inode->i_block[block] = ei->i_data[block];
3117
Andreas Gruenbacherff87b372005-07-07 17:57:00 -07003118 if (ei->i_extra_isize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
3120
3121 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
Chris Mason4f003fd2009-09-08 00:22:14 +02003122 unlock_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 rc = ext3_journal_dirty_metadata(handle, bh);
3124 if (!err)
3125 err = rc;
Jan Kara9df93932010-01-06 21:58:48 +01003126 ext3_clear_inode_state(inode, EXT3_STATE_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
Jan Karafe8bc912009-10-16 19:26:15 +02003128 atomic_set(&ei->i_sync_tid, handle->h_transaction->t_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129out_brelse:
3130 brelse (bh);
3131 ext3_std_error(inode->i_sb, err);
3132 return err;
3133}
3134
3135/*
3136 * ext3_write_inode()
3137 *
3138 * We are called from a few places:
3139 *
3140 * - Within generic_file_write() for O_SYNC files.
3141 * Here, there will be no transaction running. We wait for any running
3142 * trasnaction to commit.
3143 *
3144 * - Within sys_sync(), kupdate and such.
3145 * We wait on commit, if tol to.
3146 *
3147 * - Within prune_icache() (PF_MEMALLOC == true)
3148 * Here we simply return. We can't afford to block kswapd on the
3149 * journal commit.
3150 *
3151 * In all cases it is actually safe for us to return without doing anything,
3152 * because the inode has been copied into a raw inode buffer in
3153 * ext3_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
3154 * knfsd.
3155 *
3156 * Note that we are absolutely dependent upon all inode dirtiers doing the
3157 * right thing: they *must* call mark_inode_dirty() after dirtying info in
3158 * which we are interested.
3159 *
3160 * It would be a bug for them to not do this. The code:
3161 *
3162 * mark_inode_dirty(inode)
3163 * stuff();
3164 * inode->i_size = expr;
3165 *
3166 * is in error because a kswapd-driven write_inode() could occur while
3167 * `stuff()' is running, and the new i_size will be lost. Plus the inode
3168 * will no longer be on the superblock's dirty inode list.
3169 */
Christoph Hellwiga9185b42010-03-05 09:21:37 +01003170int ext3_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171{
3172 if (current->flags & PF_MEMALLOC)
3173 return 0;
3174
3175 if (ext3_journal_current_handle()) {
Jose R. Santos9ad163a2007-10-18 23:39:23 -07003176 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 dump_stack();
3178 return -EIO;
3179 }
3180
Christoph Hellwiga9185b42010-03-05 09:21:37 +01003181 if (wbc->sync_mode != WB_SYNC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 return 0;
3183
3184 return ext3_force_commit(inode->i_sb);
3185}
3186
3187/*
3188 * ext3_setattr()
3189 *
3190 * Called from notify_change.
3191 *
3192 * We want to trap VFS attempts to truncate the file as soon as
3193 * possible. In particular, we want to make sure that when the VFS
3194 * shrinks i_size, we put the inode on the orphan list and modify
3195 * i_disksize immediately, so that during the subsequent flushing of
3196 * dirty pages and freeing of disk blocks, we can guarantee that any
3197 * commit will leave the blocks being flushed in an unused state on
3198 * disk. (On recovery, the inode will get truncated and the blocks will
3199 * be freed, so we have a strong guarantee that no future commit will
Mingming Caoae6ddcc2006-09-27 01:49:27 -07003200 * leave these blocks visible to the user.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 *
3202 * Called with inode->sem down.
3203 */
3204int ext3_setattr(struct dentry *dentry, struct iattr *attr)
3205{
3206 struct inode *inode = dentry->d_inode;
3207 int error, rc = 0;
3208 const unsigned int ia_valid = attr->ia_valid;
3209
3210 error = inode_change_ok(inode, attr);
3211 if (error)
3212 return error;
3213
Dmitry Monakhov12755622010-04-08 22:04:20 +04003214 if (is_quota_modification(inode, attr))
Christoph Hellwig871a2932010-03-03 09:05:07 -05003215 dquot_initialize(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
3217 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
3218 handle_t *handle;
3219
3220 /* (user+group)*(old+new) structure, inode write (sb,
3221 * inode block, ? - but truncate inode update has it) */
Dmitry Monakhovc4590012009-12-09 03:05:30 +03003222 handle = ext3_journal_start(inode, EXT3_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+
3223 EXT3_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)+3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 if (IS_ERR(handle)) {
3225 error = PTR_ERR(handle);
3226 goto err_out;
3227 }
Christoph Hellwigb43fa822010-03-03 09:05:03 -05003228 error = dquot_transfer(inode, attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 if (error) {
3230 ext3_journal_stop(handle);
3231 return error;
3232 }
3233 /* Update corresponding info in inode so that everything is in
3234 * one transaction */
3235 if (attr->ia_valid & ATTR_UID)
3236 inode->i_uid = attr->ia_uid;
3237 if (attr->ia_valid & ATTR_GID)
3238 inode->i_gid = attr->ia_gid;
3239 error = ext3_mark_inode_dirty(handle, inode);
3240 ext3_journal_stop(handle);
3241 }
3242
3243 if (S_ISREG(inode->i_mode) &&
3244 attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
3245 handle_t *handle;
3246
3247 handle = ext3_journal_start(inode, 3);
3248 if (IS_ERR(handle)) {
3249 error = PTR_ERR(handle);
3250 goto err_out;
3251 }
3252
3253 error = ext3_orphan_add(handle, inode);
3254 EXT3_I(inode)->i_disksize = attr->ia_size;
3255 rc = ext3_mark_inode_dirty(handle, inode);
3256 if (!error)
3257 error = rc;
3258 ext3_journal_stop(handle);
3259 }
3260
Christoph Hellwig10257742010-06-04 11:30:02 +02003261 if ((attr->ia_valid & ATTR_SIZE) &&
3262 attr->ia_size != i_size_read(inode)) {
Jan Kara40680f2f2011-05-24 22:24:47 +02003263 truncate_setsize(inode, attr->ia_size);
3264 ext3_truncate(inode);
Christoph Hellwig10257742010-06-04 11:30:02 +02003265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
Christoph Hellwig10257742010-06-04 11:30:02 +02003267 setattr_copy(inode, attr);
3268 mark_inode_dirty(inode);
3269
3270 if (ia_valid & ATTR_MODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 rc = ext3_acl_chmod(inode);
3272
3273err_out:
3274 ext3_std_error(inode->i_sb, error);
3275 if (!error)
3276 error = rc;
3277 return error;
3278}
3279
3280
3281/*
Andrew Mortond6859bf2006-03-26 01:38:03 -08003282 * How many blocks doth make a writepage()?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 *
3284 * With N blocks per page, it may be:
3285 * N data blocks
3286 * 2 indirect block
3287 * 2 dindirect
3288 * 1 tindirect
3289 * N+5 bitmap blocks (from the above)
3290 * N+5 group descriptor summary blocks
3291 * 1 inode block
3292 * 1 superblock.
3293 * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quote files
3294 *
3295 * 3 * (N + 5) + 2 + 2 * EXT3_SINGLEDATA_TRANS_BLOCKS
3296 *
3297 * With ordered or writeback data it's the same, less the N data blocks.
3298 *
3299 * If the inode's direct blocks can hold an integral number of pages then a
3300 * page cannot straddle two indirect blocks, and we can only touch one indirect
3301 * and dindirect block, and the "5" above becomes "3".
3302 *
3303 * This still overestimates under most circumstances. If we were to pass the
3304 * start and end offsets in here as well we could do block_to_path() on each
3305 * block and work out the exact number of indirects which are touched. Pah.
3306 */
3307
3308static int ext3_writepage_trans_blocks(struct inode *inode)
3309{
3310 int bpp = ext3_journal_blocks_per_page(inode);
3311 int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
3312 int ret;
3313
3314 if (ext3_should_journal_data(inode))
3315 ret = 3 * (bpp + indirects) + 2;
3316 else
Yongqiang Yang523334b2011-03-24 08:48:39 +08003317 ret = 2 * (bpp + indirects) + indirects + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
3319#ifdef CONFIG_QUOTA
Christoph Hellwig871a2932010-03-03 09:05:07 -05003320 /* We know that structure was already allocated during dquot_initialize so
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 * we will be updating only the data blocks + inodes */
Dmitry Monakhovc4590012009-12-09 03:05:30 +03003322 ret += EXT3_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323#endif
3324
3325 return ret;
3326}
3327
3328/*
3329 * The caller must have previously called ext3_reserve_inode_write().
3330 * Give this, we know that the caller already has write access to iloc->bh.
3331 */
3332int ext3_mark_iloc_dirty(handle_t *handle,
3333 struct inode *inode, struct ext3_iloc *iloc)
3334{
3335 int err = 0;
3336
3337 /* the do_update_inode consumes one bh->b_count */
3338 get_bh(iloc->bh);
3339
3340 /* ext3_do_update_inode() does journal_dirty_metadata */
3341 err = ext3_do_update_inode(handle, inode, iloc);
3342 put_bh(iloc->bh);
3343 return err;
3344}
3345
Mingming Caoae6ddcc2006-09-27 01:49:27 -07003346/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 * On success, We end up with an outstanding reference count against
Mingming Caoae6ddcc2006-09-27 01:49:27 -07003348 * iloc->bh. This _must_ be cleaned up later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 */
3350
3351int
Mingming Caoae6ddcc2006-09-27 01:49:27 -07003352ext3_reserve_inode_write(handle_t *handle, struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 struct ext3_iloc *iloc)
3354{
3355 int err = 0;
3356 if (handle) {
3357 err = ext3_get_inode_loc(inode, iloc);
3358 if (!err) {
3359 BUFFER_TRACE(iloc->bh, "get_write_access");
3360 err = ext3_journal_get_write_access(handle, iloc->bh);
3361 if (err) {
3362 brelse(iloc->bh);
3363 iloc->bh = NULL;
3364 }
3365 }
3366 }
3367 ext3_std_error(inode->i_sb, err);
3368 return err;
3369}
3370
3371/*
Andrew Mortond6859bf2006-03-26 01:38:03 -08003372 * What we do here is to mark the in-core inode as clean with respect to inode
3373 * dirtiness (it may still be data-dirty).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 * This means that the in-core inode may be reaped by prune_icache
3375 * without having to perform any I/O. This is a very good thing,
3376 * because *any* task may call prune_icache - even ones which
3377 * have a transaction open against a different journal.
3378 *
3379 * Is this cheating? Not really. Sure, we haven't written the
3380 * inode out, but prune_icache isn't a user-visible syncing function.
3381 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
3382 * we start and wait on commits.
3383 *
3384 * Is this efficient/effective? Well, we're being nice to the system
3385 * by cleaning up our inodes proactively so they can be reaped
3386 * without I/O. But we are potentially leaving up to five seconds'
3387 * worth of inodes floating about which prune_icache wants us to
3388 * write out. One way to fix that would be to get prune_icache()
3389 * to do a write_super() to free up some memory. It has the desired
3390 * effect.
3391 */
3392int ext3_mark_inode_dirty(handle_t *handle, struct inode *inode)
3393{
3394 struct ext3_iloc iloc;
3395 int err;
3396
3397 might_sleep();
Lukas Czerner785c4bc2011-05-23 18:33:01 +02003398 trace_ext3_mark_inode_dirty(inode, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 err = ext3_reserve_inode_write(handle, inode, &iloc);
3400 if (!err)
3401 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
3402 return err;
3403}
3404
3405/*
Andrew Mortond6859bf2006-03-26 01:38:03 -08003406 * ext3_dirty_inode() is called from __mark_inode_dirty()
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 *
3408 * We're really interested in the case where a file is being extended.
3409 * i_size has been changed by generic_commit_write() and we thus need
3410 * to include the updated inode in the current transaction.
3411 *
Christoph Hellwig5dd40562010-03-03 09:05:00 -05003412 * Also, dquot_alloc_space() will always dirty the inode when blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 * are allocated to the file.
3414 *
3415 * If the inode is marked synchronous, we don't honour that here - doing
3416 * so would cause a commit on atime updates, which we don't bother doing.
3417 * We handle synchronous inodes at the highest possible level.
3418 */
Christoph Hellwigaa385722011-05-27 06:53:02 -04003419void ext3_dirty_inode(struct inode *inode, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420{
3421 handle_t *current_handle = ext3_journal_current_handle();
3422 handle_t *handle;
3423
3424 handle = ext3_journal_start(inode, 2);
3425 if (IS_ERR(handle))
3426 goto out;
3427 if (current_handle &&
3428 current_handle->h_transaction != handle->h_transaction) {
3429 /* This task has a transaction open against a different fs */
3430 printk(KERN_EMERG "%s: transactions do not match!\n",
Harvey Harrisone05b6b52008-04-28 02:16:15 -07003431 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 } else {
3433 jbd_debug(5, "marking dirty. outer handle=%p\n",
3434 current_handle);
3435 ext3_mark_inode_dirty(handle, inode);
3436 }
3437 ext3_journal_stop(handle);
3438out:
3439 return;
3440}
3441
Andrew Mortond6859bf2006-03-26 01:38:03 -08003442#if 0
Mingming Caoae6ddcc2006-09-27 01:49:27 -07003443/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 * Bind an inode's backing buffer_head into this transaction, to prevent
3445 * it from being flushed to disk early. Unlike
3446 * ext3_reserve_inode_write, this leaves behind no bh reference and
3447 * returns no iloc structure, so the caller needs to repeat the iloc
3448 * lookup to mark the inode dirty later.
3449 */
Andrew Mortond6859bf2006-03-26 01:38:03 -08003450static int ext3_pin_inode(handle_t *handle, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451{
3452 struct ext3_iloc iloc;
3453
3454 int err = 0;
3455 if (handle) {
3456 err = ext3_get_inode_loc(inode, &iloc);
3457 if (!err) {
3458 BUFFER_TRACE(iloc.bh, "get_write_access");
3459 err = journal_get_write_access(handle, iloc.bh);
3460 if (!err)
Mingming Caoae6ddcc2006-09-27 01:49:27 -07003461 err = ext3_journal_dirty_metadata(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 iloc.bh);
3463 brelse(iloc.bh);
3464 }
3465 }
3466 ext3_std_error(inode->i_sb, err);
3467 return err;
3468}
3469#endif
3470
3471int ext3_change_inode_journal_flag(struct inode *inode, int val)
3472{
3473 journal_t *journal;
3474 handle_t *handle;
3475 int err;
3476
3477 /*
3478 * We have to be very careful here: changing a data block's
3479 * journaling status dynamically is dangerous. If we write a
3480 * data block to the journal, change the status and then delete
3481 * that block, we risk forgetting to revoke the old log record
3482 * from the journal and so a subsequent replay can corrupt data.
3483 * So, first we make sure that the journal is empty and that
3484 * nobody is changing anything.
3485 */
3486
3487 journal = EXT3_JOURNAL(inode);
Dave Hansene3a68e32007-07-15 23:41:14 -07003488 if (is_journal_aborted(journal))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 return -EROFS;
3490
3491 journal_lock_updates(journal);
3492 journal_flush(journal);
3493
3494 /*
3495 * OK, there are no updates running now, and all cached data is
3496 * synced to disk. We are now in a completely consistent state
3497 * which doesn't have anything in the journal, and we know that
3498 * no filesystem updates are running, so it is safe to modify
3499 * the inode's in-core data-journaling state flag now.
3500 */
3501
3502 if (val)
3503 EXT3_I(inode)->i_flags |= EXT3_JOURNAL_DATA_FL;
3504 else
3505 EXT3_I(inode)->i_flags &= ~EXT3_JOURNAL_DATA_FL;
3506 ext3_set_aops(inode);
3507
3508 journal_unlock_updates(journal);
3509
3510 /* Finally we can mark the inode as dirty. */
3511
3512 handle = ext3_journal_start(inode, 1);
3513 if (IS_ERR(handle))
3514 return PTR_ERR(handle);
3515
3516 err = ext3_mark_inode_dirty(handle, inode);
3517 handle->h_sync = 1;
3518 ext3_journal_stop(handle);
3519 ext3_std_error(inode->i_sb, err);
3520
3521 return err;
3522}