2 * linux/fs/ext3/inode.c
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)
11 * linux/fs/minix/inode.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
15 * Goal-directed block allocation by Stephen Tweedie
16 * (sct@redhat.com), 1993, 1998
17 * Big-endian to little-endian byte-swapping/bitmaps by
18 * David S. Miller (davem@caip.rutgers.edu), 1995
19 * 64-bit file support on 64-bit platforms by Jakub Jelinek
20 * (jj@sunsite.ms.mff.cuni.cz)
22 * Assorted race fixes, rewrite of ext3_get_block() by Al Viro, 2000
25 #include <linux/module.h>
27 #include <linux/time.h>
28 #include <linux/ext3_jbd.h>
29 #include <linux/jbd.h>
30 #include <linux/smp_lock.h>
31 #include <linux/highuid.h>
32 #include <linux/pagemap.h>
33 #include <linux/quotaops.h>
34 #include <linux/string.h>
35 #include <linux/buffer_head.h>
36 #include <linux/writeback.h>
37 #include <linux/mpage.h>
38 #include <linux/uio.h>
43 * Test whether an inode is a fast symlink.
45 static inline int ext3_inode_is_fast_symlink(struct inode *inode)
47 int ea_blocks = EXT3_I(inode)->i_file_acl ?
48 (inode->i_sb->s_blocksize >> 9) : 0;
50 return (S_ISLNK(inode->i_mode) &&
51 inode->i_blocks - ea_blocks == 0);
54 /* The ext3 forget function must perform a revoke if we are freeing data
55 * which has been journaled. Metadata (eg. indirect blocks) must be
56 * revoked in all cases.
58 * "bh" may be NULL: a metadata block may have been freed from memory
59 * but there may still be a record of it in the journal, and that record
60 * still needs to be revoked.
63 int ext3_forget(handle_t *handle, int is_metadata,
64 struct inode *inode, struct buffer_head *bh,
69 BUFFER_TRACE(bh, "enter");
71 jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
73 bh, is_metadata, inode->i_mode,
74 test_opt(inode->i_sb, DATA_FLAGS));
76 /* Never use the revoke function if we are doing full data
77 * journaling: there is no need to, and a V1 superblock won't
78 * support it. Otherwise, only skip the revoke on un-journaled
81 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ||
82 (!is_metadata && !ext3_should_journal_data(inode))) {
84 BUFFER_TRACE(bh, "call journal_forget");
85 ext3_journal_forget(handle, bh);
91 * data!=journal && (is_metadata || should_journal_data(inode))
93 BUFFER_TRACE(bh, "call ext3_journal_revoke");
94 err = ext3_journal_revoke(handle, blocknr, bh);
96 ext3_abort(inode->i_sb, __FUNCTION__,
97 "error %d when attempting revoke", err);
98 BUFFER_TRACE(bh, "exit");
103 * Work out how many blocks we need to progress with the next chunk of a
104 * truncate transaction.
107 static unsigned long blocks_for_truncate(struct inode *inode)
109 unsigned long needed;
111 needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
113 /* Give ourselves just enough room to cope with inodes in which
114 * i_blocks is corrupt: we've seen disk corruptions in the past
115 * which resulted in random data in an inode which looked enough
116 * like a regular file for ext3 to try to delete it. Things
117 * will go a bit crazy if that happens, but at least we should
118 * try not to panic the whole kernel. */
122 /* But we need to bound the transaction so we don't overflow the
124 if (needed > EXT3_MAX_TRANS_DATA)
125 needed = EXT3_MAX_TRANS_DATA;
127 return EXT3_DATA_TRANS_BLOCKS + needed;
131 * Truncate transactions can be complex and absolutely huge. So we need to
132 * be able to restart the transaction at a conventient checkpoint to make
133 * sure we don't overflow the journal.
135 * start_transaction gets us a new handle for a truncate transaction,
136 * and extend_transaction tries to extend the existing one a bit. If
137 * extend fails, we need to propagate the failure up and restart the
138 * transaction in the top-level truncate loop. --sct
141 static handle_t *start_transaction(struct inode *inode)
145 result = ext3_journal_start(inode, blocks_for_truncate(inode));
149 ext3_std_error(inode->i_sb, PTR_ERR(result));
154 * Try to extend this transaction for the purposes of truncation.
156 * Returns 0 if we managed to create more room. If we can't create more
157 * room, and the transaction must be restarted we return 1.
159 static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
161 if (handle->h_buffer_credits > EXT3_RESERVE_TRANS_BLOCKS)
163 if (!ext3_journal_extend(handle, blocks_for_truncate(inode)))
169 * Restart the transaction associated with *handle. This does a commit,
170 * so before we call here everything must be consistently dirtied against
173 static int ext3_journal_test_restart(handle_t *handle, struct inode *inode)
175 jbd_debug(2, "restarting handle %p\n", handle);
176 return ext3_journal_restart(handle, blocks_for_truncate(inode));
180 * Called at the last iput() if i_nlink is zero.
182 void ext3_delete_inode (struct inode * inode)
186 if (is_bad_inode(inode))
189 handle = start_transaction(inode);
190 if (IS_ERR(handle)) {
191 /* If we're going to skip the normal cleanup, we still
192 * need to make sure that the in-core orphan linked list
193 * is properly cleaned up. */
194 ext3_orphan_del(NULL, inode);
202 ext3_truncate(inode);
204 * Kill off the orphan record which ext3_truncate created.
205 * AKPM: I think this can be inside the above `if'.
206 * Note that ext3_orphan_del() has to be able to cope with the
207 * deletion of a non-existent orphan - this is because we don't
208 * know if ext3_truncate() actually created an orphan record.
209 * (Well, we could do this if we need to, but heck - it works)
211 ext3_orphan_del(handle, inode);
212 EXT3_I(inode)->i_dtime = get_seconds();
215 * One subtle ordering requirement: if anything has gone wrong
216 * (transaction abort, IO errors, whatever), then we can still
217 * do these next steps (the fs will already have been marked as
218 * having errors), but we can't free the inode if the mark_dirty
221 if (ext3_mark_inode_dirty(handle, inode))
222 /* If that failed, just do the required in-core inode clear. */
225 ext3_free_inode(handle, inode);
226 ext3_journal_stop(handle);
229 clear_inode(inode); /* We must guarantee clearing of inode... */
232 static int ext3_alloc_block (handle_t *handle,
233 struct inode * inode, unsigned long goal, int *err)
235 unsigned long result;
237 result = ext3_new_block (handle, inode, goal, err);
245 struct buffer_head *bh;
248 static inline void add_chain(Indirect *p, struct buffer_head *bh, u32 *v)
250 p->key = *(p->p = v);
254 static inline int verify_chain(Indirect *from, Indirect *to)
256 while (from <= to && from->key == *from->p)
262 * ext3_block_to_path - parse the block number into array of offsets
263 * @inode: inode in question (we are only interested in its superblock)
264 * @i_block: block number to be parsed
265 * @offsets: array to store the offsets in
266 * @boundary: set this non-zero if the referred-to block is likely to be
267 * followed (on disk) by an indirect block.
269 * To store the locations of file's data ext3 uses a data structure common
270 * for UNIX filesystems - tree of pointers anchored in the inode, with
271 * data blocks at leaves and indirect blocks in intermediate nodes.
272 * This function translates the block number into path in that tree -
273 * return value is the path length and @offsets[n] is the offset of
274 * pointer to (n+1)th node in the nth one. If @block is out of range
275 * (negative or too large) warning is printed and zero returned.
277 * Note: function doesn't find node addresses, so no IO is needed. All
278 * we need to know is the capacity of indirect blocks (taken from the
283 * Portability note: the last comparison (check that we fit into triple
284 * indirect block) is spelled differently, because otherwise on an
285 * architecture with 32-bit longs and 8Kb pages we might get into trouble
286 * if our filesystem had 8Kb blocks. We might use long long, but that would
287 * kill us on x86. Oh, well, at least the sign propagation does not matter -
288 * i_block would have to be negative in the very beginning, so we would not
292 static int ext3_block_to_path(struct inode *inode,
293 long i_block, int offsets[4], int *boundary)
295 int ptrs = EXT3_ADDR_PER_BLOCK(inode->i_sb);
296 int ptrs_bits = EXT3_ADDR_PER_BLOCK_BITS(inode->i_sb);
297 const long direct_blocks = EXT3_NDIR_BLOCKS,
298 indirect_blocks = ptrs,
299 double_blocks = (1 << (ptrs_bits * 2));
304 ext3_warning (inode->i_sb, "ext3_block_to_path", "block < 0");
305 } else if (i_block < direct_blocks) {
306 offsets[n++] = i_block;
307 final = direct_blocks;
308 } else if ( (i_block -= direct_blocks) < indirect_blocks) {
309 offsets[n++] = EXT3_IND_BLOCK;
310 offsets[n++] = i_block;
312 } else if ((i_block -= indirect_blocks) < double_blocks) {
313 offsets[n++] = EXT3_DIND_BLOCK;
314 offsets[n++] = i_block >> ptrs_bits;
315 offsets[n++] = i_block & (ptrs - 1);
317 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
318 offsets[n++] = EXT3_TIND_BLOCK;
319 offsets[n++] = i_block >> (ptrs_bits * 2);
320 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
321 offsets[n++] = i_block & (ptrs - 1);
324 ext3_warning (inode->i_sb, "ext3_block_to_path", "block > big");
327 *boundary = (i_block & (ptrs - 1)) == (final - 1);
332 * ext3_get_branch - read the chain of indirect blocks leading to data
333 * @inode: inode in question
334 * @depth: depth of the chain (1 - direct pointer, etc.)
335 * @offsets: offsets of pointers in inode/indirect blocks
336 * @chain: place to store the result
337 * @err: here we store the error value
339 * Function fills the array of triples <key, p, bh> and returns %NULL
340 * if everything went OK or the pointer to the last filled triple
341 * (incomplete one) otherwise. Upon the return chain[i].key contains
342 * the number of (i+1)-th block in the chain (as it is stored in memory,
343 * i.e. little-endian 32-bit), chain[i].p contains the address of that
344 * number (it points into struct inode for i==0 and into the bh->b_data
345 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
346 * block for i>0 and NULL for i==0. In other words, it holds the block
347 * numbers of the chain, addresses they were taken from (and where we can
348 * verify that chain did not change) and buffer_heads hosting these
351 * Function stops when it stumbles upon zero pointer (absent block)
352 * (pointer to last triple returned, *@err == 0)
353 * or when it gets an IO error reading an indirect block
354 * (ditto, *@err == -EIO)
355 * or when it notices that chain had been changed while it was reading
356 * (ditto, *@err == -EAGAIN)
357 * or when it reads all @depth-1 indirect blocks successfully and finds
358 * the whole chain, all way to the data (returns %NULL, *err == 0).
360 static Indirect *ext3_get_branch(struct inode *inode, int depth, int *offsets,
361 Indirect chain[4], int *err)
363 struct super_block *sb = inode->i_sb;
365 struct buffer_head *bh;
368 /* i_data is not going away, no lock needed */
369 add_chain (chain, NULL, EXT3_I(inode)->i_data + *offsets);
373 bh = sb_bread(sb, le32_to_cpu(p->key));
376 /* Reader: pointers */
377 if (!verify_chain(chain, p))
379 add_chain(++p, bh, (u32*)bh->b_data + *++offsets);
397 * ext3_find_near - find a place for allocation with sufficient locality
399 * @ind: descriptor of indirect block.
401 * This function returns the prefered place for block allocation.
402 * It is used when heuristic for sequential allocation fails.
404 * + if there is a block to the left of our position - allocate near it.
405 * + if pointer will live in indirect block - allocate near that block.
406 * + if pointer will live in inode - allocate in the same
409 * In the latter case we colour the starting block by the callers PID to
410 * prevent it from clashing with concurrent allocations for a different inode
411 * in the same block group. The PID is used here so that functionally related
412 * files will be close-by on-disk.
414 * Caller must make sure that @ind is valid and will stay that way.
417 static unsigned long ext3_find_near(struct inode *inode, Indirect *ind)
419 struct ext3_inode_info *ei = EXT3_I(inode);
420 u32 *start = ind->bh ? (u32*) ind->bh->b_data : ei->i_data;
422 unsigned long bg_start;
423 unsigned long colour;
425 /* Try to find previous block */
426 for (p = ind->p - 1; p >= start; p--)
428 return le32_to_cpu(*p);
430 /* No such thing, so let's try location of indirect block */
432 return ind->bh->b_blocknr;
435 * It is going to be refered from inode itself? OK, just put it into
436 * the same cylinder group then.
438 bg_start = (ei->i_block_group * EXT3_BLOCKS_PER_GROUP(inode->i_sb)) +
439 le32_to_cpu(EXT3_SB(inode->i_sb)->s_es->s_first_data_block);
440 colour = (current->pid % 16) *
441 (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
442 return bg_start + colour;
446 * ext3_find_goal - find a prefered place for allocation.
448 * @block: block we want
449 * @chain: chain of indirect blocks
450 * @partial: pointer to the last triple within a chain
451 * @goal: place to store the result.
453 * Normally this function find the prefered place for block allocation,
454 * stores it in *@goal and returns zero. If the branch had been changed
455 * under us we return -EAGAIN.
458 static int ext3_find_goal(struct inode *inode, long block, Indirect chain[4],
459 Indirect *partial, unsigned long *goal)
461 struct ext3_inode_info *ei = EXT3_I(inode);
462 /* Writer: ->i_next_alloc* */
463 if (block == ei->i_next_alloc_block + 1) {
464 ei->i_next_alloc_block++;
465 ei->i_next_alloc_goal++;
468 /* Reader: pointers, ->i_next_alloc* */
469 if (verify_chain(chain, partial)) {
471 * try the heuristic for sequential allocation,
472 * failing that at least try to get decent locality.
474 if (block == ei->i_next_alloc_block)
475 *goal = ei->i_next_alloc_goal;
477 *goal = ext3_find_near(inode, partial);
485 * ext3_alloc_branch - allocate and set up a chain of blocks.
487 * @num: depth of the chain (number of blocks to allocate)
488 * @offsets: offsets (in the blocks) to store the pointers to next.
489 * @branch: place to store the chain in.
491 * This function allocates @num blocks, zeroes out all but the last one,
492 * links them into chain and (if we are synchronous) writes them to disk.
493 * In other words, it prepares a branch that can be spliced onto the
494 * inode. It stores the information about that chain in the branch[], in
495 * the same format as ext3_get_branch() would do. We are calling it after
496 * we had read the existing part of chain and partial points to the last
497 * triple of that (one with zero ->key). Upon the exit we have the same
498 * picture as after the successful ext3_get_block(), excpet that in one
499 * place chain is disconnected - *branch->p is still zero (we did not
500 * set the last link), but branch->key contains the number that should
501 * be placed into *branch->p to fill that gap.
503 * If allocation fails we free all blocks we've allocated (and forget
504 * their buffer_heads) and return the error value the from failed
505 * ext3_alloc_block() (normally -ENOSPC). Otherwise we set the chain
506 * as described above and return 0.
509 static int ext3_alloc_branch(handle_t *handle, struct inode *inode,
515 int blocksize = inode->i_sb->s_blocksize;
519 int parent = ext3_alloc_block(handle, inode, goal, &err);
521 branch[0].key = cpu_to_le32(parent);
523 for (n = 1; n < num; n++) {
524 struct buffer_head *bh;
525 /* Allocate the next block */
526 int nr = ext3_alloc_block(handle, inode, parent, &err);
529 branch[n].key = cpu_to_le32(nr);
533 * Get buffer_head for parent block, zero it out
534 * and set the pointer to new one, then send
537 bh = sb_getblk(inode->i_sb, parent);
540 BUFFER_TRACE(bh, "call get_create_access");
541 err = ext3_journal_get_create_access(handle, bh);
548 memset(bh->b_data, 0, blocksize);
549 branch[n].p = (u32*) bh->b_data + offsets[n];
550 *branch[n].p = branch[n].key;
551 BUFFER_TRACE(bh, "marking uptodate");
552 set_buffer_uptodate(bh);
555 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
556 err = ext3_journal_dirty_metadata(handle, bh);
566 /* Allocation failed, free what we already allocated */
567 for (i = 1; i < keys; i++) {
568 BUFFER_TRACE(branch[i].bh, "call journal_forget");
569 ext3_journal_forget(handle, branch[i].bh);
571 for (i = 0; i < keys; i++)
572 ext3_free_blocks(handle, inode, le32_to_cpu(branch[i].key), 1);
577 * ext3_splice_branch - splice the allocated branch onto inode.
579 * @block: (logical) number of block we are adding
580 * @chain: chain of indirect blocks (with a missing link - see
582 * @where: location of missing link
583 * @num: number of blocks we are adding
585 * This function verifies that chain (up to the missing link) had not
586 * changed, fills the missing link and does all housekeeping needed in
587 * inode (->i_blocks, etc.). In case of success we end up with the full
588 * chain to new block and return 0. Otherwise (== chain had been changed)
589 * we free the new blocks (forgetting their buffer_heads, indeed) and
593 static int ext3_splice_branch(handle_t *handle, struct inode *inode, long block,
594 Indirect chain[4], Indirect *where, int num)
598 struct ext3_inode_info *ei = EXT3_I(inode);
601 * If we're splicing into a [td]indirect block (as opposed to the
602 * inode) then we need to get write access to the [td]indirect block
606 BUFFER_TRACE(where->bh, "get_write_access");
607 err = ext3_journal_get_write_access(handle, where->bh);
611 /* Verify that place we are splicing to is still there and vacant */
613 /* Writer: pointers, ->i_next_alloc* */
614 if (!verify_chain(chain, where-1) || *where->p)
620 *where->p = where->key;
621 ei->i_next_alloc_block = block;
622 ei->i_next_alloc_goal = le32_to_cpu(where[num-1].key);
625 /* We are done with atomic stuff, now do the rest of housekeeping */
627 inode->i_ctime = CURRENT_TIME;
628 ext3_mark_inode_dirty(handle, inode);
630 /* had we spliced it onto indirect block? */
633 * akpm: If we spliced it onto an indirect block, we haven't
634 * altered the inode. Note however that if it is being spliced
635 * onto an indirect block at the very end of the file (the
636 * file is growing) then we *will* alter the inode to reflect
637 * the new i_size. But that is not done here - it is done in
638 * generic_commit_write->__mark_inode_dirty->ext3_dirty_inode.
640 jbd_debug(5, "splicing indirect only\n");
641 BUFFER_TRACE(where->bh, "call ext3_journal_dirty_metadata");
642 err = ext3_journal_dirty_metadata(handle, where->bh);
647 * OK, we spliced it into the inode itself on a direct block.
648 * Inode was dirtied above.
650 jbd_debug(5, "splicing direct\n");
656 * AKPM: if where[i].bh isn't part of the current updating
657 * transaction then we explode nastily. Test this code path.
659 jbd_debug(1, "the chain changed: try again\n");
663 for (i = 1; i < num; i++) {
664 BUFFER_TRACE(where[i].bh, "call journal_forget");
665 ext3_journal_forget(handle, where[i].bh);
667 /* For the normal collision cleanup case, we free up the blocks.
668 * On genuine filesystem errors we don't even think about doing
671 for (i = 0; i < num; i++)
672 ext3_free_blocks(handle, inode,
673 le32_to_cpu(where[i].key), 1);
678 * Allocation strategy is simple: if we have to allocate something, we will
679 * have to go the whole way to leaf. So let's do it before attaching anything
680 * to tree, set linkage between the newborn blocks, write them if sync is
681 * required, recheck the path, free and repeat if check fails, otherwise
682 * set the last missing link (that will protect us from any truncate-generated
683 * removals - all blocks on the path are immune now) and possibly force the
684 * write on the parent block.
685 * That has a nice additional property: no special recovery from the failed
686 * allocations is needed - we simply release blocks and do not touch anything
687 * reachable from inode.
689 * akpm: `handle' can be NULL if create == 0.
691 * The BKL may not be held on entry here. Be sure to take it early.
695 ext3_get_block_handle(handle_t *handle, struct inode *inode, sector_t iblock,
696 struct buffer_head *bh_result, int create, int extend_disksize)
705 int depth = ext3_block_to_path(inode, iblock, offsets, &boundary);
706 struct ext3_inode_info *ei = EXT3_I(inode);
708 J_ASSERT(handle != NULL || create == 0);
714 partial = ext3_get_branch(inode, depth, offsets, chain, &err);
716 /* Simplest case - block found, no allocation needed */
718 clear_buffer_new(bh_result);
720 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
722 set_buffer_boundary(bh_result);
723 /* Clean up and exit */
724 partial = chain+depth-1; /* the whole chain */
728 /* Next simple case - plain lookup or failed read of indirect block */
729 if (!create || err == -EIO) {
731 while (partial > chain) {
732 BUFFER_TRACE(partial->bh, "call brelse");
736 BUFFER_TRACE(bh_result, "returned");
742 * Indirect block might be removed by truncate while we were
743 * reading it. Handling of that case (forget what we've got and
744 * reread) is taken out of the main path.
750 down(&ei->truncate_sem);
751 if (ext3_find_goal(inode, iblock, chain, partial, &goal) < 0) {
752 up(&ei->truncate_sem);
756 left = (chain + depth) - partial;
759 * Block out ext3_truncate while we alter the tree
761 err = ext3_alloc_branch(handle, inode, left, goal,
762 offsets+(partial-chain), partial);
764 /* The ext3_splice_branch call will free and forget any buffers
765 * on the new chain if there is a failure, but that risks using
766 * up transaction credits, especially for bitmaps where the
767 * credits cannot be returned. Can we handle this somehow? We
768 * may need to return -EAGAIN upwards in the worst case. --sct */
770 err = ext3_splice_branch(handle, inode, iblock, chain,
772 /* i_disksize growing is protected by truncate_sem
773 * don't forget to protect it if you're about to implement
774 * concurrent ext3_get_block() -bzzz */
775 if (!err && extend_disksize && inode->i_size > ei->i_disksize)
776 ei->i_disksize = inode->i_size;
777 up(&ei->truncate_sem);
783 set_buffer_new(bh_result);
787 while (partial > chain) {
788 jbd_debug(1, "buffer chain changed, retrying\n");
789 BUFFER_TRACE(partial->bh, "brelsing");
796 static int ext3_get_block(struct inode *inode, sector_t iblock,
797 struct buffer_head *bh_result, int create)
799 handle_t *handle = 0;
803 handle = ext3_journal_current_handle();
804 J_ASSERT(handle != 0);
806 ret = ext3_get_block_handle(handle, inode, iblock,
807 bh_result, create, 1);
811 #define DIO_CREDITS (EXT3_RESERVE_TRANS_BLOCKS + 32)
814 ext3_direct_io_get_blocks(struct inode *inode, sector_t iblock,
815 unsigned long max_blocks, struct buffer_head *bh_result,
818 handle_t *handle = journal_current_handle();
821 if (handle && handle->h_buffer_credits <= EXT3_RESERVE_TRANS_BLOCKS) {
823 * Getting low on buffer credits...
825 if (!ext3_journal_extend(handle, DIO_CREDITS)) {
827 * Couldn't extend the transaction. Start a new one
829 ret = ext3_journal_restart(handle, DIO_CREDITS);
833 ret = ext3_get_block_handle(handle, inode, iblock,
834 bh_result, create, 0);
836 bh_result->b_size = (1 << inode->i_blkbits);
842 * `handle' can be NULL if create is zero
844 struct buffer_head *ext3_getblk(handle_t *handle, struct inode * inode,
845 long block, int create, int * errp)
847 struct buffer_head dummy;
850 J_ASSERT(handle != NULL || create == 0);
853 dummy.b_blocknr = -1000;
854 buffer_trace_init(&dummy.b_history);
855 *errp = ext3_get_block_handle(handle, inode, block, &dummy, create, 1);
856 if (!*errp && buffer_mapped(&dummy)) {
857 struct buffer_head *bh;
858 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
859 if (buffer_new(&dummy)) {
860 J_ASSERT(create != 0);
861 J_ASSERT(handle != 0);
863 /* Now that we do not always journal data, we
864 should keep in mind whether this should
865 always journal the new buffer as metadata.
866 For now, regular file writes use
867 ext3_get_block instead, so it's not a
870 BUFFER_TRACE(bh, "call get_create_access");
871 fatal = ext3_journal_get_create_access(handle, bh);
872 if (!fatal && !buffer_uptodate(bh)) {
873 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
874 set_buffer_uptodate(bh);
877 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
878 err = ext3_journal_dirty_metadata(handle, bh);
882 BUFFER_TRACE(bh, "not a new buffer");
894 struct buffer_head *ext3_bread(handle_t *handle, struct inode * inode,
895 int block, int create, int *err)
897 struct buffer_head * bh;
900 prev_blocks = inode->i_blocks;
902 bh = ext3_getblk (handle, inode, block, create, err);
905 if (buffer_uptodate(bh))
907 ll_rw_block (READ, 1, &bh);
909 if (buffer_uptodate(bh))
916 static int walk_page_buffers( handle_t *handle,
917 struct buffer_head *head,
921 int (*fn)( handle_t *handle,
922 struct buffer_head *bh))
924 struct buffer_head *bh;
925 unsigned block_start, block_end;
926 unsigned blocksize = head->b_size;
928 struct buffer_head *next;
930 for ( bh = head, block_start = 0;
931 ret == 0 && (bh != head || !block_start);
932 block_start = block_end, bh = next)
934 next = bh->b_this_page;
935 block_end = block_start + blocksize;
936 if (block_end <= from || block_start >= to) {
937 if (partial && !buffer_uptodate(bh))
941 err = (*fn)(handle, bh);
949 * To preserve ordering, it is essential that the hole instantiation and
950 * the data write be encapsulated in a single transaction. We cannot
951 * close off a transaction and start a new one between the ext3_get_block()
952 * and the commit_write(). So doing the journal_start at the start of
953 * prepare_write() is the right place.
955 * Also, this function can nest inside ext3_writepage() ->
956 * block_write_full_page(). In that case, we *know* that ext3_writepage()
957 * has generated enough buffer credits to do the whole page. So we won't
958 * block on the journal in that case, which is good, because the caller may
961 * By accident, ext3 can be reentered when a transaction is open via
962 * quota file writes. If we were to commit the transaction while thus
963 * reentered, there can be a deadlock - we would be holding a quota
964 * lock, and the commit would never complete if another thread had a
965 * transaction open and was blocking on the quota lock - a ranking
968 * So what we do is to rely on the fact that journal_stop/journal_start
969 * will _not_ run commit under these circumstances because handle->h_ref
970 * is elevated. We'll still have enough credits for the tiny quotafile
974 static int do_journal_get_write_access(handle_t *handle,
975 struct buffer_head *bh)
977 if (!buffer_mapped(bh) || buffer_freed(bh))
979 return ext3_journal_get_write_access(handle, bh);
982 static int ext3_prepare_write(struct file *file, struct page *page,
983 unsigned from, unsigned to)
985 struct inode *inode = page->mapping->host;
986 int ret, needed_blocks = ext3_writepage_trans_blocks(inode);
991 handle = ext3_journal_start(inode, needed_blocks);
992 if (IS_ERR(handle)) {
993 ret = PTR_ERR(handle);
996 ret = block_prepare_write(page, from, to, ext3_get_block);
998 goto prepare_write_failed;
1000 if (ext3_should_journal_data(inode)) {
1001 ret = walk_page_buffers(handle, page_buffers(page),
1002 from, to, NULL, do_journal_get_write_access);
1004 prepare_write_failed:
1006 ext3_journal_stop(handle);
1007 if (ret == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
1014 ext3_journal_dirty_data(handle_t *handle, struct buffer_head *bh)
1016 int err = journal_dirty_data(handle, bh);
1018 ext3_journal_abort_handle(__FUNCTION__, __FUNCTION__,
1023 /* For commit_write() in data=journal mode */
1024 static int commit_write_fn(handle_t *handle, struct buffer_head *bh)
1026 if (!buffer_mapped(bh) || buffer_freed(bh))
1028 set_buffer_uptodate(bh);
1029 return ext3_journal_dirty_metadata(handle, bh);
1033 * We need to pick up the new inode size which generic_commit_write gave us
1034 * `file' can be NULL - eg, when called from page_symlink().
1036 * ext3 never places buffers on inode->i_mapping->private_list. metadata
1037 * buffers are managed internally.
1040 static int ext3_ordered_commit_write(struct file *file, struct page *page,
1041 unsigned from, unsigned to)
1043 handle_t *handle = ext3_journal_current_handle();
1044 struct inode *inode = page->mapping->host;
1047 ret = walk_page_buffers(handle, page_buffers(page),
1048 from, to, NULL, ext3_journal_dirty_data);
1052 * generic_commit_write() will run mark_inode_dirty() if i_size
1053 * changes. So let's piggyback the i_disksize mark_inode_dirty
1058 new_i_size = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1059 if (new_i_size > EXT3_I(inode)->i_disksize)
1060 EXT3_I(inode)->i_disksize = new_i_size;
1061 ret = generic_commit_write(file, page, from, to);
1063 ret2 = ext3_journal_stop(handle);
1069 static int ext3_writeback_commit_write(struct file *file, struct page *page,
1070 unsigned from, unsigned to)
1072 handle_t *handle = ext3_journal_current_handle();
1073 struct inode *inode = page->mapping->host;
1077 new_i_size = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1078 if (new_i_size > EXT3_I(inode)->i_disksize)
1079 EXT3_I(inode)->i_disksize = new_i_size;
1080 ret = generic_commit_write(file, page, from, to);
1081 ret2 = ext3_journal_stop(handle);
1087 static int ext3_journalled_commit_write(struct file *file,
1088 struct page *page, unsigned from, unsigned to)
1090 handle_t *handle = ext3_journal_current_handle();
1091 struct inode *inode = page->mapping->host;
1097 * Here we duplicate the generic_commit_write() functionality
1099 pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1101 ret = walk_page_buffers(handle, page_buffers(page), from,
1102 to, &partial, commit_write_fn);
1104 SetPageUptodate(page);
1105 if (pos > inode->i_size)
1106 i_size_write(inode, pos);
1107 EXT3_I(inode)->i_state |= EXT3_STATE_JDATA;
1108 if (inode->i_size > EXT3_I(inode)->i_disksize) {
1109 EXT3_I(inode)->i_disksize = inode->i_size;
1110 ret2 = ext3_mark_inode_dirty(handle, inode);
1114 ret2 = ext3_journal_stop(handle);
1121 * bmap() is special. It gets used by applications such as lilo and by
1122 * the swapper to find the on-disk block of a specific piece of data.
1124 * Naturally, this is dangerous if the block concerned is still in the
1125 * journal. If somebody makes a swapfile on an ext3 data-journaling
1126 * filesystem and enables swap, then they may get a nasty shock when the
1127 * data getting swapped to that swapfile suddenly gets overwritten by
1128 * the original zero's written out previously to the journal and
1129 * awaiting writeback in the kernel's buffer cache.
1131 * So, if we see any bmap calls here on a modified, data-journaled file,
1132 * take extra steps to flush any blocks which might be in the cache.
1134 static sector_t ext3_bmap(struct address_space *mapping, sector_t block)
1136 struct inode *inode = mapping->host;
1140 if (EXT3_I(inode)->i_state & EXT3_STATE_JDATA) {
1142 * This is a REALLY heavyweight approach, but the use of
1143 * bmap on dirty files is expected to be extremely rare:
1144 * only if we run lilo or swapon on a freshly made file
1145 * do we expect this to happen.
1147 * (bmap requires CAP_SYS_RAWIO so this does not
1148 * represent an unprivileged user DOS attack --- we'd be
1149 * in trouble if mortal users could trigger this path at
1152 * NB. EXT3_STATE_JDATA is not set on files other than
1153 * regular files. If somebody wants to bmap a directory
1154 * or symlink and gets confused because the buffer
1155 * hasn't yet been flushed to disk, they deserve
1156 * everything they get.
1159 EXT3_I(inode)->i_state &= ~EXT3_STATE_JDATA;
1160 journal = EXT3_JOURNAL(inode);
1161 journal_lock_updates(journal);
1162 err = journal_flush(journal);
1163 journal_unlock_updates(journal);
1169 return generic_block_bmap(mapping,block,ext3_get_block);
1172 static int bget_one(handle_t *handle, struct buffer_head *bh)
1178 static int bput_one(handle_t *handle, struct buffer_head *bh)
1184 static int journal_dirty_data_fn(handle_t *handle, struct buffer_head *bh)
1186 if (buffer_mapped(bh))
1187 return ext3_journal_dirty_data(handle, bh);
1192 * Note that we always start a transaction even if we're not journalling
1193 * data. This is to preserve ordering: any hole instantiation within
1194 * __block_write_full_page -> ext3_get_block() should be journalled
1195 * along with the data so we don't crash and then get metadata which
1196 * refers to old data.
1198 * In all journalling modes block_write_full_page() will start the I/O.
1202 * ext3_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
1207 * ext3_file_write() -> generic_file_write() -> __alloc_pages() -> ...
1209 * Same applies to ext3_get_block(). We will deadlock on various things like
1210 * lock_journal and i_truncate_sem.
1212 * Setting PF_MEMALLOC here doesn't work - too many internal memory
1215 * 16May01: If we're reentered then journal_current_handle() will be
1216 * non-zero. We simply *return*.
1218 * 1 July 2001: @@@ FIXME:
1219 * In journalled data mode, a data buffer may be metadata against the
1220 * current transaction. But the same file is part of a shared mapping
1221 * and someone does a writepage() on it.
1223 * We will move the buffer onto the async_data list, but *after* it has
1224 * been dirtied. So there's a small window where we have dirty data on
1227 * Note that this only applies to the last partial page in the file. The
1228 * bit which block_write_full_page() uses prepare/commit for. (That's
1229 * broken code anyway: it's wrong for msync()).
1231 * It's a rare case: affects the final partial page, for journalled data
1232 * where the file is subject to bith write() and writepage() in the same
1233 * transction. To fix it we'll need a custom block_write_full_page().
1234 * We'll probably need that anyway for journalling writepage() output.
1236 * We don't honour synchronous mounts for writepage(). That would be
1237 * disastrous. Any write() or metadata operation will sync the fs for
1240 * AKPM2: if all the page's buffers are mapped to disk and !data=journal,
1241 * we don't need to open a transaction here.
1243 static int ext3_ordered_writepage(struct page *page,
1244 struct writeback_control *wbc)
1246 struct inode *inode = page->mapping->host;
1247 struct buffer_head *page_bufs;
1248 handle_t *handle = NULL;
1252 J_ASSERT(PageLocked(page));
1255 * We give up here if we're reentered, because it might be for a
1256 * different filesystem.
1258 if (ext3_journal_current_handle())
1261 handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
1263 if (IS_ERR(handle)) {
1264 ret = PTR_ERR(handle);
1268 if (!page_has_buffers(page)) {
1269 create_empty_buffers(page, inode->i_sb->s_blocksize,
1270 (1 << BH_Dirty)|(1 << BH_Uptodate));
1272 page_bufs = page_buffers(page);
1273 walk_page_buffers(handle, page_bufs, 0,
1274 PAGE_CACHE_SIZE, NULL, bget_one);
1276 ret = block_write_full_page(page, ext3_get_block, wbc);
1279 * The page can become unlocked at any point now, and
1280 * truncate can then come in and change things. So we
1281 * can't touch *page from now on. But *page_bufs is
1282 * safe due to elevated refcount.
1286 * And attach them to the current transaction. But only if
1287 * block_write_full_page() succeeded. Otherwise they are unmapped,
1288 * and generally junk.
1291 err = walk_page_buffers(handle, page_bufs, 0, PAGE_CACHE_SIZE,
1292 NULL, journal_dirty_data_fn);
1296 walk_page_buffers(handle, page_bufs, 0,
1297 PAGE_CACHE_SIZE, NULL, bput_one);
1298 err = ext3_journal_stop(handle);
1304 redirty_page_for_writepage(wbc, page);
1309 static int ext3_writeback_writepage(struct page *page,
1310 struct writeback_control *wbc)
1312 struct inode *inode = page->mapping->host;
1313 handle_t *handle = NULL;
1317 if (ext3_journal_current_handle())
1320 handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
1321 if (IS_ERR(handle)) {
1322 ret = PTR_ERR(handle);
1326 ret = block_write_full_page(page, ext3_get_block, wbc);
1327 err = ext3_journal_stop(handle);
1333 redirty_page_for_writepage(wbc, page);
1338 static int ext3_journalled_writepage(struct page *page,
1339 struct writeback_control *wbc)
1341 struct inode *inode = page->mapping->host;
1342 handle_t *handle = NULL;
1346 if (ext3_journal_current_handle())
1349 handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
1350 if (IS_ERR(handle)) {
1351 ret = PTR_ERR(handle);
1355 if (!page_has_buffers(page) || PageChecked(page)) {
1357 * It's mmapped pagecache. Add buffers and journal it. There
1358 * doesn't seem much point in redirtying the page here.
1360 ClearPageChecked(page);
1361 ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE,
1365 ret = walk_page_buffers(handle, page_buffers(page), 0,
1366 PAGE_CACHE_SIZE, NULL, do_journal_get_write_access);
1368 err = walk_page_buffers(handle, page_buffers(page), 0,
1369 PAGE_CACHE_SIZE, NULL, commit_write_fn);
1372 EXT3_I(inode)->i_state |= EXT3_STATE_JDATA;
1376 * It may be a page full of checkpoint-mode buffers. We don't
1377 * really know unless we go poke around in the buffer_heads.
1378 * But block_write_full_page will do the right thing.
1380 ret = block_write_full_page(page, ext3_get_block, wbc);
1382 err = ext3_journal_stop(handle);
1389 redirty_page_for_writepage(wbc, page);
1395 static int ext3_readpage(struct file *file, struct page *page)
1397 return mpage_readpage(page, ext3_get_block);
1401 ext3_readpages(struct file *file, struct address_space *mapping,
1402 struct list_head *pages, unsigned nr_pages)
1404 return mpage_readpages(mapping, pages, nr_pages, ext3_get_block);
1407 static int ext3_invalidatepage(struct page *page, unsigned long offset)
1409 journal_t *journal = EXT3_JOURNAL(page->mapping->host);
1412 * If it's a full truncate we just forget about the pending dirtying
1415 ClearPageChecked(page);
1417 return journal_invalidatepage(journal, page, offset);
1420 static int ext3_releasepage(struct page *page, int wait)
1422 journal_t *journal = EXT3_JOURNAL(page->mapping->host);
1424 WARN_ON(PageChecked(page));
1425 return journal_try_to_free_buffers(journal, page, wait);
1429 * If the O_DIRECT write will extend the file then add this inode to the
1430 * orphan list. So recovery will truncate it back to the original size
1431 * if the machine crashes during the write.
1433 * If the O_DIRECT write is intantiating holes inside i_size and the machine
1434 * crashes then stale disk data _may_ be exposed inside the file.
1436 static ssize_t ext3_direct_IO(int rw, struct kiocb *iocb,
1437 const struct iovec *iov, loff_t offset,
1438 unsigned long nr_segs)
1440 struct file *file = iocb->ki_filp;
1441 struct inode *inode = file->f_mapping->host;
1442 struct ext3_inode_info *ei = EXT3_I(inode);
1443 handle_t *handle = NULL;
1446 size_t count = iov_length(iov, nr_segs);
1449 loff_t final_size = offset + count;
1451 handle = ext3_journal_start(inode, DIO_CREDITS);
1452 if (IS_ERR(handle)) {
1453 ret = PTR_ERR(handle);
1456 if (final_size > inode->i_size) {
1457 ret = ext3_orphan_add(handle, inode);
1461 ei->i_disksize = inode->i_size;
1465 ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
1467 ext3_direct_io_get_blocks, NULL);
1474 ext3_orphan_del(handle, inode);
1475 if (orphan && ret > 0) {
1476 loff_t end = offset + ret;
1477 if (end > inode->i_size) {
1478 ei->i_disksize = end;
1479 i_size_write(inode, end);
1480 err = ext3_mark_inode_dirty(handle, inode);
1485 err = ext3_journal_stop(handle);
1494 * Pages can be marked dirty completely asynchronously from ext3's journalling
1495 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
1496 * much here because ->set_page_dirty is called under VFS locks. The page is
1497 * not necessarily locked.
1499 * We cannot just dirty the page and leave attached buffers clean, because the
1500 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
1501 * or jbddirty because all the journalling code will explode.
1503 * So what we do is to mark the page "pending dirty" and next time writepage
1504 * is called, propagate that into the buffers appropriately.
1506 static int ext3_journalled_set_page_dirty(struct page *page)
1508 SetPageChecked(page);
1509 return __set_page_dirty_nobuffers(page);
1512 static struct address_space_operations ext3_ordered_aops = {
1513 .readpage = ext3_readpage,
1514 .readpages = ext3_readpages,
1515 .writepage = ext3_ordered_writepage,
1516 .sync_page = block_sync_page,
1517 .prepare_write = ext3_prepare_write,
1518 .commit_write = ext3_ordered_commit_write,
1520 .invalidatepage = ext3_invalidatepage,
1521 .releasepage = ext3_releasepage,
1522 .direct_IO = ext3_direct_IO,
1525 static struct address_space_operations ext3_writeback_aops = {
1526 .readpage = ext3_readpage,
1527 .readpages = ext3_readpages,
1528 .writepage = ext3_writeback_writepage,
1529 .sync_page = block_sync_page,
1530 .prepare_write = ext3_prepare_write,
1531 .commit_write = ext3_writeback_commit_write,
1533 .invalidatepage = ext3_invalidatepage,
1534 .releasepage = ext3_releasepage,
1535 .direct_IO = ext3_direct_IO,
1538 static struct address_space_operations ext3_journalled_aops = {
1539 .readpage = ext3_readpage,
1540 .readpages = ext3_readpages,
1541 .writepage = ext3_journalled_writepage,
1542 .sync_page = block_sync_page,
1543 .prepare_write = ext3_prepare_write,
1544 .commit_write = ext3_journalled_commit_write,
1545 .set_page_dirty = ext3_journalled_set_page_dirty,
1547 .invalidatepage = ext3_invalidatepage,
1548 .releasepage = ext3_releasepage,
1551 void ext3_set_aops(struct inode *inode)
1553 if (ext3_should_order_data(inode))
1554 inode->i_mapping->a_ops = &ext3_ordered_aops;
1555 else if (ext3_should_writeback_data(inode))
1556 inode->i_mapping->a_ops = &ext3_writeback_aops;
1558 inode->i_mapping->a_ops = &ext3_journalled_aops;
1562 * ext3_block_truncate_page() zeroes out a mapping from file offset `from'
1563 * up to the end of the block which corresponds to `from'.
1564 * This required during truncate. We need to physically zero the tail end
1565 * of that block so it doesn't yield old data if the file is later grown.
1567 static int ext3_block_truncate_page(handle_t *handle, struct page *page,
1568 struct address_space *mapping, loff_t from)
1570 unsigned long index = from >> PAGE_CACHE_SHIFT;
1571 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1572 unsigned blocksize, iblock, length, pos;
1573 struct inode *inode = mapping->host;
1574 struct buffer_head *bh;
1578 blocksize = inode->i_sb->s_blocksize;
1579 length = blocksize - (offset & (blocksize - 1));
1580 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1582 if (!page_has_buffers(page))
1583 create_empty_buffers(page, blocksize, 0);
1585 /* Find the buffer that contains "offset" */
1586 bh = page_buffers(page);
1588 while (offset >= pos) {
1589 bh = bh->b_this_page;
1595 if (buffer_freed(bh)) {
1596 BUFFER_TRACE(bh, "freed: skip");
1600 if (!buffer_mapped(bh)) {
1601 BUFFER_TRACE(bh, "unmapped");
1602 ext3_get_block(inode, iblock, bh, 0);
1603 /* unmapped? It's a hole - nothing to do */
1604 if (!buffer_mapped(bh)) {
1605 BUFFER_TRACE(bh, "still unmapped");
1610 /* Ok, it's mapped. Make sure it's up-to-date */
1611 if (PageUptodate(page))
1612 set_buffer_uptodate(bh);
1614 if (!buffer_uptodate(bh)) {
1616 ll_rw_block(READ, 1, &bh);
1618 /* Uhhuh. Read error. Complain and punt. */
1619 if (!buffer_uptodate(bh))
1623 if (ext3_should_journal_data(inode)) {
1624 BUFFER_TRACE(bh, "get write access");
1625 err = ext3_journal_get_write_access(handle, bh);
1630 kaddr = kmap_atomic(page, KM_USER0);
1631 memset(kaddr + offset, 0, length);
1632 flush_dcache_page(page);
1633 kunmap_atomic(kaddr, KM_USER0);
1635 BUFFER_TRACE(bh, "zeroed end of block");
1638 if (ext3_should_journal_data(inode)) {
1639 err = ext3_journal_dirty_metadata(handle, bh);
1641 if (ext3_should_order_data(inode))
1642 err = ext3_journal_dirty_data(handle, bh);
1643 mark_buffer_dirty(bh);
1648 page_cache_release(page);
1653 * Probably it should be a library function... search for first non-zero word
1654 * or memcmp with zero_page, whatever is better for particular architecture.
1657 static inline int all_zeroes(u32 *p, u32 *q)
1666 * ext3_find_shared - find the indirect blocks for partial truncation.
1667 * @inode: inode in question
1668 * @depth: depth of the affected branch
1669 * @offsets: offsets of pointers in that branch (see ext3_block_to_path)
1670 * @chain: place to store the pointers to partial indirect blocks
1671 * @top: place to the (detached) top of branch
1673 * This is a helper function used by ext3_truncate().
1675 * When we do truncate() we may have to clean the ends of several
1676 * indirect blocks but leave the blocks themselves alive. Block is
1677 * partially truncated if some data below the new i_size is refered
1678 * from it (and it is on the path to the first completely truncated
1679 * data block, indeed). We have to free the top of that path along
1680 * with everything to the right of the path. Since no allocation
1681 * past the truncation point is possible until ext3_truncate()
1682 * finishes, we may safely do the latter, but top of branch may
1683 * require special attention - pageout below the truncation point
1684 * might try to populate it.
1686 * We atomically detach the top of branch from the tree, store the
1687 * block number of its root in *@top, pointers to buffer_heads of
1688 * partially truncated blocks - in @chain[].bh and pointers to
1689 * their last elements that should not be removed - in
1690 * @chain[].p. Return value is the pointer to last filled element
1693 * The work left to caller to do the actual freeing of subtrees:
1694 * a) free the subtree starting from *@top
1695 * b) free the subtrees whose roots are stored in
1696 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
1697 * c) free the subtrees growing from the inode past the @chain[0].
1698 * (no partially truncated stuff there). */
1700 static Indirect *ext3_find_shared(struct inode *inode,
1706 Indirect *partial, *p;
1710 /* Make k index the deepest non-null offest + 1 */
1711 for (k = depth; k > 1 && !offsets[k-1]; k--)
1713 partial = ext3_get_branch(inode, k, offsets, chain, &err);
1714 /* Writer: pointers */
1716 partial = chain + k-1;
1718 * If the branch acquired continuation since we've looked at it -
1719 * fine, it should all survive and (new) top doesn't belong to us.
1721 if (!partial->key && *partial->p)
1724 for (p=partial; p>chain && all_zeroes((u32*)p->bh->b_data,p->p); p--)
1727 * OK, we've found the last block that must survive. The rest of our
1728 * branch should be detached before unlocking. However, if that rest
1729 * of branch is all ours and does not grow immediately from the inode
1730 * it's easier to cheat and just decrement partial->p.
1732 if (p == chain + k - 1 && p > chain) {
1736 /* Nope, don't do this in ext3. Must leave the tree intact */
1745 brelse(partial->bh);
1753 * Zero a number of block pointers in either an inode or an indirect block.
1754 * If we restart the transaction we must again get write access to the
1755 * indirect block for further modification.
1757 * We release `count' blocks on disk, but (last - first) may be greater
1758 * than `count' because there can be holes in there.
1761 ext3_clear_blocks(handle_t *handle, struct inode *inode, struct buffer_head *bh,
1762 unsigned long block_to_free, unsigned long count,
1763 u32 *first, u32 *last)
1766 if (try_to_extend_transaction(handle, inode)) {
1768 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1769 ext3_journal_dirty_metadata(handle, bh);
1771 ext3_mark_inode_dirty(handle, inode);
1772 ext3_journal_test_restart(handle, inode);
1774 BUFFER_TRACE(bh, "retaking write access");
1775 ext3_journal_get_write_access(handle, bh);
1780 * Any buffers which are on the journal will be in memory. We find
1781 * them on the hash table so journal_revoke() will run journal_forget()
1782 * on them. We've already detached each block from the file, so
1783 * bforget() in journal_forget() should be safe.
1785 * AKPM: turn on bforget in journal_forget()!!!
1787 for (p = first; p < last; p++) {
1788 u32 nr = le32_to_cpu(*p);
1790 struct buffer_head *bh;
1793 bh = sb_find_get_block(inode->i_sb, nr);
1794 ext3_forget(handle, 0, inode, bh, nr);
1798 ext3_free_blocks(handle, inode, block_to_free, count);
1802 * ext3_free_data - free a list of data blocks
1803 * @handle: handle for this transaction
1804 * @inode: inode we are dealing with
1805 * @this_bh: indirect buffer_head which contains *@first and *@last
1806 * @first: array of block numbers
1807 * @last: points immediately past the end of array
1809 * We are freeing all blocks refered from that array (numbers are stored as
1810 * little-endian 32-bit) and updating @inode->i_blocks appropriately.
1812 * We accumulate contiguous runs of blocks to free. Conveniently, if these
1813 * blocks are contiguous then releasing them at one time will only affect one
1814 * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
1815 * actually use a lot of journal space.
1817 * @this_bh will be %NULL if @first and @last point into the inode's direct
1820 static void ext3_free_data(handle_t *handle, struct inode *inode,
1821 struct buffer_head *this_bh, u32 *first, u32 *last)
1823 unsigned long block_to_free = 0; /* Starting block # of a run */
1824 unsigned long count = 0; /* Number of blocks in the run */
1825 u32 *block_to_free_p = NULL; /* Pointer into inode/ind
1828 unsigned long nr; /* Current block # */
1829 u32 *p; /* Pointer into inode/ind
1830 for current block */
1833 if (this_bh) { /* For indirect block */
1834 BUFFER_TRACE(this_bh, "get_write_access");
1835 err = ext3_journal_get_write_access(handle, this_bh);
1836 /* Important: if we can't update the indirect pointers
1837 * to the blocks, we can't free them. */
1842 for (p = first; p < last; p++) {
1843 nr = le32_to_cpu(*p);
1845 /* accumulate blocks to free if they're contiguous */
1848 block_to_free_p = p;
1850 } else if (nr == block_to_free + count) {
1853 ext3_clear_blocks(handle, inode, this_bh,
1855 count, block_to_free_p, p);
1857 block_to_free_p = p;
1864 ext3_clear_blocks(handle, inode, this_bh, block_to_free,
1865 count, block_to_free_p, p);
1868 BUFFER_TRACE(this_bh, "call ext3_journal_dirty_metadata");
1869 ext3_journal_dirty_metadata(handle, this_bh);
1874 * ext3_free_branches - free an array of branches
1875 * @handle: JBD handle for this transaction
1876 * @inode: inode we are dealing with
1877 * @parent_bh: the buffer_head which contains *@first and *@last
1878 * @first: array of block numbers
1879 * @last: pointer immediately past the end of array
1880 * @depth: depth of the branches to free
1882 * We are freeing all blocks refered from these branches (numbers are
1883 * stored as little-endian 32-bit) and updating @inode->i_blocks
1886 static void ext3_free_branches(handle_t *handle, struct inode *inode,
1887 struct buffer_head *parent_bh,
1888 u32 *first, u32 *last, int depth)
1893 if (is_handle_aborted(handle))
1897 struct buffer_head *bh;
1898 int addr_per_block = EXT3_ADDR_PER_BLOCK(inode->i_sb);
1900 while (--p >= first) {
1901 nr = le32_to_cpu(*p);
1903 continue; /* A hole */
1905 /* Go read the buffer for the next level down */
1906 bh = sb_bread(inode->i_sb, nr);
1909 * A read failure? Report error and clear slot
1913 ext3_error(inode->i_sb, "ext3_free_branches",
1914 "Read failure, inode=%ld, block=%ld",
1919 /* This zaps the entire block. Bottom up. */
1920 BUFFER_TRACE(bh, "free child branches");
1921 ext3_free_branches(handle, inode, bh, (u32*)bh->b_data,
1922 (u32*)bh->b_data + addr_per_block,
1926 * We've probably journalled the indirect block several
1927 * times during the truncate. But it's no longer
1928 * needed and we now drop it from the transaction via
1931 * That's easy if it's exclusively part of this
1932 * transaction. But if it's part of the committing
1933 * transaction then journal_forget() will simply
1934 * brelse() it. That means that if the underlying
1935 * block is reallocated in ext3_get_block(),
1936 * unmap_underlying_metadata() will find this block
1937 * and will try to get rid of it. damn, damn.
1939 * If this block has already been committed to the
1940 * journal, a revoke record will be written. And
1941 * revoke records must be emitted *before* clearing
1942 * this block's bit in the bitmaps.
1944 ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
1947 * Everything below this this pointer has been
1948 * released. Now let this top-of-subtree go.
1950 * We want the freeing of this indirect block to be
1951 * atomic in the journal with the updating of the
1952 * bitmap block which owns it. So make some room in
1955 * We zero the parent pointer *after* freeing its
1956 * pointee in the bitmaps, so if extend_transaction()
1957 * for some reason fails to put the bitmap changes and
1958 * the release into the same transaction, recovery
1959 * will merely complain about releasing a free block,
1960 * rather than leaking blocks.
1962 if (is_handle_aborted(handle))
1964 if (try_to_extend_transaction(handle, inode)) {
1965 ext3_mark_inode_dirty(handle, inode);
1966 ext3_journal_test_restart(handle, inode);
1969 ext3_free_blocks(handle, inode, nr, 1);
1973 * The block which we have just freed is
1974 * pointed to by an indirect block: journal it
1976 BUFFER_TRACE(parent_bh, "get_write_access");
1977 if (!ext3_journal_get_write_access(handle,
1980 BUFFER_TRACE(parent_bh,
1981 "call ext3_journal_dirty_metadata");
1982 ext3_journal_dirty_metadata(handle,
1988 /* We have reached the bottom of the tree. */
1989 BUFFER_TRACE(parent_bh, "free data blocks");
1990 ext3_free_data(handle, inode, parent_bh, first, last);
1997 * We block out ext3_get_block() block instantiations across the entire
1998 * transaction, and VFS/VM ensures that ext3_truncate() cannot run
1999 * simultaneously on behalf of the same inode.
2001 * As we work through the truncate and commmit bits of it to the journal there
2002 * is one core, guiding principle: the file's tree must always be consistent on
2003 * disk. We must be able to restart the truncate after a crash.
2005 * The file's tree may be transiently inconsistent in memory (although it
2006 * probably isn't), but whenever we close off and commit a journal transaction,
2007 * the contents of (the filesystem + the journal) must be consistent and
2008 * restartable. It's pretty simple, really: bottom up, right to left (although
2009 * left-to-right works OK too).
2011 * Note that at recovery time, journal replay occurs *before* the restart of
2012 * truncate against the orphan inode list.
2014 * The committed inode has the new, desired i_size (which is the same as
2015 * i_disksize in this case). After a crash, ext3_orphan_cleanup() will see
2016 * that this inode's truncate did not complete and it will again call
2017 * ext3_truncate() to have another go. So there will be instantiated blocks
2018 * to the right of the truncation point in a crashed ext3 filesystem. But
2019 * that's fine - as long as they are linked from the inode, the post-crash
2020 * ext3_truncate() run will find them and release them.
2023 void ext3_truncate(struct inode * inode)
2026 struct ext3_inode_info *ei = EXT3_I(inode);
2027 u32 *i_data = ei->i_data;
2028 int addr_per_block = EXT3_ADDR_PER_BLOCK(inode->i_sb);
2029 struct address_space *mapping = inode->i_mapping;
2036 unsigned blocksize = inode->i_sb->s_blocksize;
2039 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2040 S_ISLNK(inode->i_mode)))
2042 if (ext3_inode_is_fast_symlink(inode))
2044 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2047 ext3_discard_reservation(inode);
2050 * We have to lock the EOF page here, because lock_page() nests
2051 * outside journal_start().
2053 if ((inode->i_size & (blocksize - 1)) == 0) {
2054 /* Block boundary? Nothing to do */
2057 page = grab_cache_page(mapping,
2058 inode->i_size >> PAGE_CACHE_SHIFT);
2063 handle = start_transaction(inode);
2064 if (IS_ERR(handle)) {
2066 clear_highpage(page);
2067 flush_dcache_page(page);
2069 page_cache_release(page);
2071 return; /* AKPM: return what? */
2074 last_block = (inode->i_size + blocksize-1)
2075 >> EXT3_BLOCK_SIZE_BITS(inode->i_sb);
2078 ext3_block_truncate_page(handle, page, mapping, inode->i_size);
2080 n = ext3_block_to_path(inode, last_block, offsets, NULL);
2082 goto out_stop; /* error */
2085 * OK. This truncate is going to happen. We add the inode to the
2086 * orphan list, so that if this truncate spans multiple transactions,
2087 * and we crash, we will resume the truncate when the filesystem
2088 * recovers. It also marks the inode dirty, to catch the new size.
2090 * Implication: the file must always be in a sane, consistent
2091 * truncatable state while each transaction commits.
2093 if (ext3_orphan_add(handle, inode))
2097 * The orphan list entry will now protect us from any crash which
2098 * occurs before the truncate completes, so it is now safe to propagate
2099 * the new, shorter inode size (held for now in i_size) into the
2100 * on-disk inode. We do this via i_disksize, which is the value which
2101 * ext3 *really* writes onto the disk inode.
2103 ei->i_disksize = inode->i_size;
2106 * From here we block out all ext3_get_block() callers who want to
2107 * modify the block allocation tree.
2109 down(&ei->truncate_sem);
2111 if (n == 1) { /* direct blocks */
2112 ext3_free_data(handle, inode, NULL, i_data+offsets[0],
2113 i_data + EXT3_NDIR_BLOCKS);
2117 partial = ext3_find_shared(inode, n, offsets, chain, &nr);
2118 /* Kill the top of shared branch (not detached) */
2120 if (partial == chain) {
2121 /* Shared branch grows from the inode */
2122 ext3_free_branches(handle, inode, NULL,
2123 &nr, &nr+1, (chain+n-1) - partial);
2126 * We mark the inode dirty prior to restart,
2127 * and prior to stop. No need for it here.
2130 /* Shared branch grows from an indirect block */
2131 BUFFER_TRACE(partial->bh, "get_write_access");
2132 ext3_free_branches(handle, inode, partial->bh,
2134 partial->p+1, (chain+n-1) - partial);
2137 /* Clear the ends of indirect blocks on the shared branch */
2138 while (partial > chain) {
2139 ext3_free_branches(handle, inode, partial->bh, partial->p + 1,
2140 (u32*)partial->bh->b_data + addr_per_block,
2141 (chain+n-1) - partial);
2142 BUFFER_TRACE(partial->bh, "call brelse");
2143 brelse (partial->bh);
2147 /* Kill the remaining (whole) subtrees */
2148 switch (offsets[0]) {
2150 nr = i_data[EXT3_IND_BLOCK];
2152 ext3_free_branches(handle, inode, NULL,
2154 i_data[EXT3_IND_BLOCK] = 0;
2156 case EXT3_IND_BLOCK:
2157 nr = i_data[EXT3_DIND_BLOCK];
2159 ext3_free_branches(handle, inode, NULL,
2161 i_data[EXT3_DIND_BLOCK] = 0;
2163 case EXT3_DIND_BLOCK:
2164 nr = i_data[EXT3_TIND_BLOCK];
2166 ext3_free_branches(handle, inode, NULL,
2168 i_data[EXT3_TIND_BLOCK] = 0;
2170 case EXT3_TIND_BLOCK:
2173 up(&ei->truncate_sem);
2174 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2175 ext3_mark_inode_dirty(handle, inode);
2177 /* In a multi-transaction truncate, we only make the final
2178 * transaction synchronous */
2183 * If this was a simple ftruncate(), and the file will remain alive
2184 * then we need to clear up the orphan record which we created above.
2185 * However, if this was a real unlink then we were called by
2186 * ext3_delete_inode(), and we allow that function to clean up the
2187 * orphan info for us.
2190 ext3_orphan_del(handle, inode);
2192 ext3_journal_stop(handle);
2195 static unsigned long ext3_get_inode_block(struct super_block *sb,
2196 unsigned long ino, struct ext3_iloc *iloc)
2198 unsigned long desc, group_desc, block_group;
2199 unsigned long offset, block;
2200 struct buffer_head *bh;
2201 struct ext3_group_desc * gdp;
2204 if ((ino != EXT3_ROOT_INO &&
2205 ino != EXT3_JOURNAL_INO &&
2206 ino != EXT3_RESIZE_INO &&
2207 ino < EXT3_FIRST_INO(sb)) ||
2209 EXT3_SB(sb)->s_es->s_inodes_count)) {
2210 ext3_error (sb, "ext3_get_inode_block",
2211 "bad inode number: %lu", ino);
2214 block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
2215 if (block_group >= EXT3_SB(sb)->s_groups_count) {
2216 ext3_error (sb, "ext3_get_inode_block",
2217 "group >= groups count");
2220 group_desc = block_group >> EXT3_DESC_PER_BLOCK_BITS(sb);
2221 desc = block_group & (EXT3_DESC_PER_BLOCK(sb) - 1);
2222 bh = EXT3_SB(sb)->s_group_desc[group_desc];
2224 ext3_error (sb, "ext3_get_inode_block",
2225 "Descriptor not loaded");
2229 gdp = (struct ext3_group_desc *) bh->b_data;
2231 * Figure out the offset within the block group inode table
2233 offset = ((ino - 1) % EXT3_INODES_PER_GROUP(sb)) *
2234 EXT3_INODE_SIZE(sb);
2235 block = le32_to_cpu(gdp[desc].bg_inode_table) +
2236 (offset >> EXT3_BLOCK_SIZE_BITS(sb));
2238 iloc->block_group = block_group;
2239 iloc->offset = offset & (EXT3_BLOCK_SIZE(sb) - 1);
2244 * ext3_get_inode_loc returns with an extra refcount against the inode's
2245 * underlying buffer_head on success. If `in_mem' is false then we're purely
2246 * trying to determine the inode's location on-disk and no read need be
2249 static int ext3_get_inode_loc(struct inode *inode,
2250 struct ext3_iloc *iloc, int in_mem)
2252 unsigned long block;
2253 struct buffer_head *bh;
2255 block = ext3_get_inode_block(inode->i_sb, inode->i_ino, iloc);
2259 bh = sb_getblk(inode->i_sb, block);
2261 ext3_error (inode->i_sb, "ext3_get_inode_loc",
2262 "unable to read inode block - "
2263 "inode=%lu, block=%lu", inode->i_ino, block);
2266 if (!buffer_uptodate(bh)) {
2268 if (buffer_uptodate(bh)) {
2269 /* someone brought it uptodate while we waited */
2274 /* we can't skip I/O if inode is on a disk only */
2276 struct buffer_head *bitmap_bh;
2277 struct ext3_group_desc *desc;
2278 int inodes_per_buffer;
2279 int inode_offset, i;
2284 * If this is the only valid inode in the block we
2285 * need not read the block.
2287 block_group = (inode->i_ino - 1) /
2288 EXT3_INODES_PER_GROUP(inode->i_sb);
2289 inodes_per_buffer = bh->b_size /
2290 EXT3_INODE_SIZE(inode->i_sb);
2291 inode_offset = ((inode->i_ino - 1) %
2292 EXT3_INODES_PER_GROUP(inode->i_sb));
2293 start = inode_offset & ~(inodes_per_buffer - 1);
2295 /* Is the inode bitmap in cache? */
2296 desc = ext3_get_group_desc(inode->i_sb,
2301 bitmap_bh = sb_getblk(inode->i_sb,
2302 le32_to_cpu(desc->bg_inode_bitmap));
2307 * If the inode bitmap isn't in cache then the
2308 * optimisation may end up performing two reads instead
2309 * of one, so skip it.
2311 if (!buffer_uptodate(bitmap_bh)) {
2315 for (i = start; i < start + inodes_per_buffer; i++) {
2316 if (i == inode_offset)
2318 if (ext3_test_bit(i, bitmap_bh->b_data))
2322 if (i == start + inodes_per_buffer) {
2323 /* all other inodes are free, so skip I/O */
2324 memset(bh->b_data, 0, bh->b_size);
2325 set_buffer_uptodate(bh);
2333 * There are another valid inodes in the buffer so we must
2334 * read the block from disk
2337 bh->b_end_io = end_buffer_read_sync;
2338 submit_bh(READ, bh);
2340 if (!buffer_uptodate(bh)) {
2341 ext3_error(inode->i_sb, "ext3_get_inode_loc",
2342 "unable to read inode block - "
2343 "inode=%lu, block=%lu",
2344 inode->i_ino, block);
2354 void ext3_set_inode_flags(struct inode *inode)
2356 unsigned int flags = EXT3_I(inode)->i_flags;
2358 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
2359 if (flags & EXT3_SYNC_FL)
2360 inode->i_flags |= S_SYNC;
2361 if (flags & EXT3_APPEND_FL)
2362 inode->i_flags |= S_APPEND;
2363 if (flags & EXT3_IMMUTABLE_FL)
2364 inode->i_flags |= S_IMMUTABLE;
2365 if (flags & EXT3_NOATIME_FL)
2366 inode->i_flags |= S_NOATIME;
2367 if (flags & EXT3_DIRSYNC_FL)
2368 inode->i_flags |= S_DIRSYNC;
2371 void ext3_read_inode(struct inode * inode)
2373 struct ext3_iloc iloc;
2374 struct ext3_inode *raw_inode;
2375 struct ext3_inode_info *ei = EXT3_I(inode);
2376 struct buffer_head *bh;
2379 #ifdef CONFIG_EXT3_FS_POSIX_ACL
2380 ei->i_acl = EXT3_ACL_NOT_CACHED;
2381 ei->i_default_acl = EXT3_ACL_NOT_CACHED;
2383 if (ext3_get_inode_loc(inode, &iloc, 0))
2386 raw_inode = ext3_raw_inode(&iloc);
2387 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
2388 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
2389 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
2390 if(!(test_opt (inode->i_sb, NO_UID32))) {
2391 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
2392 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
2394 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
2395 inode->i_size = le32_to_cpu(raw_inode->i_size);
2396 inode->i_atime.tv_sec = le32_to_cpu(raw_inode->i_atime);
2397 inode->i_ctime.tv_sec = le32_to_cpu(raw_inode->i_ctime);
2398 inode->i_mtime.tv_sec = le32_to_cpu(raw_inode->i_mtime);
2399 inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = inode->i_mtime.tv_nsec = 0;
2402 ei->i_next_alloc_block = 0;
2403 ei->i_next_alloc_goal = 0;
2404 ei->i_dir_start_lookup = 0;
2405 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
2406 /* We now have enough fields to check if the inode was active or not.
2407 * This is needed because nfsd might try to access dead inodes
2408 * the test is that same one that e2fsck uses
2409 * NeilBrown 1999oct15
2411 if (inode->i_nlink == 0) {
2412 if (inode->i_mode == 0 ||
2413 !(EXT3_SB(inode->i_sb)->s_mount_state & EXT3_ORPHAN_FS)) {
2414 /* this inode is deleted */
2418 /* The only unlinked inodes we let through here have
2419 * valid i_mode and are being read by the orphan
2420 * recovery code: that's fine, we're about to complete
2421 * the process of deleting those. */
2423 inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size
2424 * (for stat), not the fs block
2426 inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
2427 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
2428 #ifdef EXT3_FRAGMENTS
2429 ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
2430 ei->i_frag_no = raw_inode->i_frag;
2431 ei->i_frag_size = raw_inode->i_fsize;
2433 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
2434 if (!S_ISREG(inode->i_mode)) {
2435 ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
2438 ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
2440 ei->i_disksize = inode->i_size;
2441 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
2442 ei->i_block_group = iloc.block_group;
2443 ei->i_rsv_window.rsv_start = 0;
2444 ei->i_rsv_window.rsv_end= 0;
2445 atomic_set(&ei->i_rsv_window.rsv_goal_size, EXT3_DEFAULT_RESERVE_BLOCKS);
2446 INIT_LIST_HEAD(&ei->i_rsv_window.rsv_list);
2448 * NOTE! The in-memory inode i_data array is in little-endian order
2449 * even on big-endian machines: we do NOT byteswap the block numbers!
2451 for (block = 0; block < EXT3_N_BLOCKS; block++)
2452 ei->i_data[block] = raw_inode->i_block[block];
2453 INIT_LIST_HEAD(&ei->i_orphan);
2455 if (S_ISREG(inode->i_mode)) {
2456 inode->i_op = &ext3_file_inode_operations;
2457 inode->i_fop = &ext3_file_operations;
2458 ext3_set_aops(inode);
2459 } else if (S_ISDIR(inode->i_mode)) {
2460 inode->i_op = &ext3_dir_inode_operations;
2461 inode->i_fop = &ext3_dir_operations;
2462 } else if (S_ISLNK(inode->i_mode)) {
2463 if (ext3_inode_is_fast_symlink(inode))
2464 inode->i_op = &ext3_fast_symlink_inode_operations;
2466 inode->i_op = &ext3_symlink_inode_operations;
2467 ext3_set_aops(inode);
2470 inode->i_op = &ext3_special_inode_operations;
2471 if (raw_inode->i_block[0])
2472 init_special_inode(inode, inode->i_mode,
2473 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
2475 init_special_inode(inode, inode->i_mode,
2476 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
2479 ext3_set_inode_flags(inode);
2483 make_bad_inode(inode);
2488 * Post the struct inode info into an on-disk inode location in the
2489 * buffer-cache. This gobbles the caller's reference to the
2490 * buffer_head in the inode location struct.
2492 * The caller must have write access to iloc->bh.
2494 static int ext3_do_update_inode(handle_t *handle,
2495 struct inode *inode,
2496 struct ext3_iloc *iloc)
2498 struct ext3_inode *raw_inode = ext3_raw_inode(iloc);
2499 struct ext3_inode_info *ei = EXT3_I(inode);
2500 struct buffer_head *bh = iloc->bh;
2501 int err = 0, rc, block;
2503 /* For fields not not tracking in the in-memory inode,
2504 * initialise them to zero for new inodes. */
2505 if (ei->i_state & EXT3_STATE_NEW)
2506 memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
2508 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
2509 if(!(test_opt(inode->i_sb, NO_UID32))) {
2510 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
2511 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
2513 * Fix up interoperability with old kernels. Otherwise, old inodes get
2514 * re-used with the upper 16 bits of the uid/gid intact
2517 raw_inode->i_uid_high =
2518 cpu_to_le16(high_16_bits(inode->i_uid));
2519 raw_inode->i_gid_high =
2520 cpu_to_le16(high_16_bits(inode->i_gid));
2522 raw_inode->i_uid_high = 0;
2523 raw_inode->i_gid_high = 0;
2526 raw_inode->i_uid_low =
2527 cpu_to_le16(fs_high2lowuid(inode->i_uid));
2528 raw_inode->i_gid_low =
2529 cpu_to_le16(fs_high2lowgid(inode->i_gid));
2530 raw_inode->i_uid_high = 0;
2531 raw_inode->i_gid_high = 0;
2533 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
2534 raw_inode->i_size = cpu_to_le32(ei->i_disksize);
2535 raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
2536 raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
2537 raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
2538 raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
2539 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
2540 raw_inode->i_flags = cpu_to_le32(ei->i_flags);
2541 #ifdef EXT3_FRAGMENTS
2542 raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
2543 raw_inode->i_frag = ei->i_frag_no;
2544 raw_inode->i_fsize = ei->i_frag_size;
2546 raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
2547 if (!S_ISREG(inode->i_mode)) {
2548 raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
2550 raw_inode->i_size_high =
2551 cpu_to_le32(ei->i_disksize >> 32);
2552 if (ei->i_disksize > 0x7fffffffULL) {
2553 struct super_block *sb = inode->i_sb;
2554 if (!EXT3_HAS_RO_COMPAT_FEATURE(sb,
2555 EXT3_FEATURE_RO_COMPAT_LARGE_FILE) ||
2556 EXT3_SB(sb)->s_es->s_rev_level ==
2557 cpu_to_le32(EXT3_GOOD_OLD_REV)) {
2558 /* If this is the first large file
2559 * created, add a flag to the superblock.
2561 err = ext3_journal_get_write_access(handle,
2562 EXT3_SB(sb)->s_sbh);
2565 ext3_update_dynamic_rev(sb);
2566 EXT3_SET_RO_COMPAT_FEATURE(sb,
2567 EXT3_FEATURE_RO_COMPAT_LARGE_FILE);
2570 err = ext3_journal_dirty_metadata(handle,
2571 EXT3_SB(sb)->s_sbh);
2575 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
2576 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
2577 if (old_valid_dev(inode->i_rdev)) {
2578 raw_inode->i_block[0] =
2579 cpu_to_le32(old_encode_dev(inode->i_rdev));
2580 raw_inode->i_block[1] = 0;
2582 raw_inode->i_block[0] = 0;
2583 raw_inode->i_block[1] =
2584 cpu_to_le32(new_encode_dev(inode->i_rdev));
2585 raw_inode->i_block[2] = 0;
2587 } else for (block = 0; block < EXT3_N_BLOCKS; block++)
2588 raw_inode->i_block[block] = ei->i_data[block];
2590 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
2591 rc = ext3_journal_dirty_metadata(handle, bh);
2594 ei->i_state &= ~EXT3_STATE_NEW;
2598 ext3_std_error(inode->i_sb, err);
2603 * ext3_write_inode()
2605 * We are called from a few places:
2607 * - Within generic_file_write() for O_SYNC files.
2608 * Here, there will be no transaction running. We wait for any running
2609 * trasnaction to commit.
2611 * - Within sys_sync(), kupdate and such.
2612 * We wait on commit, if tol to.
2614 * - Within prune_icache() (PF_MEMALLOC == true)
2615 * Here we simply return. We can't afford to block kswapd on the
2618 * In all cases it is actually safe for us to return without doing anything,
2619 * because the inode has been copied into a raw inode buffer in
2620 * ext3_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
2623 * Note that we are absolutely dependent upon all inode dirtiers doing the
2624 * right thing: they *must* call mark_inode_dirty() after dirtying info in
2625 * which we are interested.
2627 * It would be a bug for them to not do this. The code:
2629 * mark_inode_dirty(inode)
2631 * inode->i_size = expr;
2633 * is in error because a kswapd-driven write_inode() could occur while
2634 * `stuff()' is running, and the new i_size will be lost. Plus the inode
2635 * will no longer be on the superblock's dirty inode list.
2637 void ext3_write_inode(struct inode *inode, int wait)
2639 if (current->flags & PF_MEMALLOC)
2642 if (ext3_journal_current_handle()) {
2643 jbd_debug(0, "called recursively, non-PF_MEMALLOC!\n");
2651 ext3_force_commit(inode->i_sb);
2657 * Called from notify_change.
2659 * We want to trap VFS attempts to truncate the file as soon as
2660 * possible. In particular, we want to make sure that when the VFS
2661 * shrinks i_size, we put the inode on the orphan list and modify
2662 * i_disksize immediately, so that during the subsequent flushing of
2663 * dirty pages and freeing of disk blocks, we can guarantee that any
2664 * commit will leave the blocks being flushed in an unused state on
2665 * disk. (On recovery, the inode will get truncated and the blocks will
2666 * be freed, so we have a strong guarantee that no future commit will
2667 * leave these blocks visible to the user.)
2669 * Called with inode->sem down.
2671 int ext3_setattr(struct dentry *dentry, struct iattr *attr)
2673 struct inode *inode = dentry->d_inode;
2675 const unsigned int ia_valid = attr->ia_valid;
2677 error = inode_change_ok(inode, attr);
2681 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
2682 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
2685 /* (user+group)*(old+new) structure, inode write (sb,
2686 * inode block, ? - but truncate inode update has it) */
2687 handle = ext3_journal_start(inode, 4*EXT3_QUOTA_INIT_BLOCKS+3);
2688 if (IS_ERR(handle)) {
2689 error = PTR_ERR(handle);
2692 error = DQUOT_TRANSFER(inode, attr) ? -EDQUOT : 0;
2694 ext3_journal_stop(handle);
2697 /* Update corresponding info in inode so that everything is in
2698 * one transaction */
2699 if (attr->ia_valid & ATTR_UID)
2700 inode->i_uid = attr->ia_uid;
2701 if (attr->ia_valid & ATTR_GID)
2702 inode->i_gid = attr->ia_gid;
2703 error = ext3_mark_inode_dirty(handle, inode);
2704 ext3_journal_stop(handle);
2707 if (S_ISREG(inode->i_mode) &&
2708 attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
2711 handle = ext3_journal_start(inode, 3);
2712 if (IS_ERR(handle)) {
2713 error = PTR_ERR(handle);
2717 error = ext3_orphan_add(handle, inode);
2718 EXT3_I(inode)->i_disksize = attr->ia_size;
2719 rc = ext3_mark_inode_dirty(handle, inode);
2722 ext3_journal_stop(handle);
2725 rc = inode_setattr(inode, attr);
2727 /* If inode_setattr's call to ext3_truncate failed to get a
2728 * transaction handle at all, we need to clean up the in-core
2729 * orphan list manually. */
2731 ext3_orphan_del(NULL, inode);
2733 if (!rc && (ia_valid & ATTR_MODE))
2734 rc = ext3_acl_chmod(inode);
2737 ext3_std_error(inode->i_sb, error);
2745 * akpm: how many blocks doth make a writepage()?
2747 * With N blocks per page, it may be:
2752 * N+5 bitmap blocks (from the above)
2753 * N+5 group descriptor summary blocks
2756 * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quote files
2758 * 3 * (N + 5) + 2 + 2 * EXT3_SINGLEDATA_TRANS_BLOCKS
2760 * With ordered or writeback data it's the same, less the N data blocks.
2762 * If the inode's direct blocks can hold an integral number of pages then a
2763 * page cannot straddle two indirect blocks, and we can only touch one indirect
2764 * and dindirect block, and the "5" above becomes "3".
2766 * This still overestimates under most circumstances. If we were to pass the
2767 * start and end offsets in here as well we could do block_to_path() on each
2768 * block and work out the exact number of indirects which are touched. Pah.
2771 int ext3_writepage_trans_blocks(struct inode *inode)
2773 int bpp = ext3_journal_blocks_per_page(inode);
2774 int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
2777 if (ext3_should_journal_data(inode))
2778 ret = 3 * (bpp + indirects) + 2;
2780 ret = 2 * (bpp + indirects) + 2;
2783 /* We know that structure was already allocated during DQUOT_INIT so
2784 * we will be updating only the data blocks + inodes */
2785 ret += 2*EXT3_QUOTA_TRANS_BLOCKS;
2792 * The caller must have previously called ext3_reserve_inode_write().
2793 * Give this, we know that the caller already has write access to iloc->bh.
2795 int ext3_mark_iloc_dirty(handle_t *handle,
2796 struct inode *inode, struct ext3_iloc *iloc)
2800 /* the do_update_inode consumes one bh->b_count */
2803 /* ext3_do_update_inode() does journal_dirty_metadata */
2804 err = ext3_do_update_inode(handle, inode, iloc);
2810 * On success, We end up with an outstanding reference count against
2811 * iloc->bh. This _must_ be cleaned up later.
2815 ext3_reserve_inode_write(handle_t *handle, struct inode *inode,
2816 struct ext3_iloc *iloc)
2820 err = ext3_get_inode_loc(inode, iloc, 1);
2822 BUFFER_TRACE(iloc->bh, "get_write_access");
2823 err = ext3_journal_get_write_access(handle, iloc->bh);
2830 ext3_std_error(inode->i_sb, err);
2835 * akpm: What we do here is to mark the in-core inode as clean
2836 * with respect to inode dirtiness (it may still be data-dirty).
2837 * This means that the in-core inode may be reaped by prune_icache
2838 * without having to perform any I/O. This is a very good thing,
2839 * because *any* task may call prune_icache - even ones which
2840 * have a transaction open against a different journal.
2842 * Is this cheating? Not really. Sure, we haven't written the
2843 * inode out, but prune_icache isn't a user-visible syncing function.
2844 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
2845 * we start and wait on commits.
2847 * Is this efficient/effective? Well, we're being nice to the system
2848 * by cleaning up our inodes proactively so they can be reaped
2849 * without I/O. But we are potentially leaving up to five seconds'
2850 * worth of inodes floating about which prune_icache wants us to
2851 * write out. One way to fix that would be to get prune_icache()
2852 * to do a write_super() to free up some memory. It has the desired
2855 int ext3_mark_inode_dirty(handle_t *handle, struct inode *inode)
2857 struct ext3_iloc iloc;
2860 err = ext3_reserve_inode_write(handle, inode, &iloc);
2862 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
2867 * akpm: ext3_dirty_inode() is called from __mark_inode_dirty()
2869 * We're really interested in the case where a file is being extended.
2870 * i_size has been changed by generic_commit_write() and we thus need
2871 * to include the updated inode in the current transaction.
2873 * Also, DQUOT_ALLOC_SPACE() will always dirty the inode when blocks
2874 * are allocated to the file.
2876 * If the inode is marked synchronous, we don't honour that here - doing
2877 * so would cause a commit on atime updates, which we don't bother doing.
2878 * We handle synchronous inodes at the highest possible level.
2880 void ext3_dirty_inode(struct inode *inode)
2882 handle_t *current_handle = ext3_journal_current_handle();
2885 handle = ext3_journal_start(inode, 2);
2888 if (current_handle &&
2889 current_handle->h_transaction != handle->h_transaction) {
2890 /* This task has a transaction open against a different fs */
2891 printk(KERN_EMERG "%s: transactions do not match!\n",
2894 jbd_debug(5, "marking dirty. outer handle=%p\n",
2896 ext3_mark_inode_dirty(handle, inode);
2898 ext3_journal_stop(handle);
2905 * Bind an inode's backing buffer_head into this transaction, to prevent
2906 * it from being flushed to disk early. Unlike
2907 * ext3_reserve_inode_write, this leaves behind no bh reference and
2908 * returns no iloc structure, so the caller needs to repeat the iloc
2909 * lookup to mark the inode dirty later.
2912 ext3_pin_inode(handle_t *handle, struct inode *inode)
2914 struct ext3_iloc iloc;
2918 err = ext3_get_inode_loc(inode, &iloc, 1);
2920 BUFFER_TRACE(iloc.bh, "get_write_access");
2921 err = journal_get_write_access(handle, iloc.bh);
2923 err = ext3_journal_dirty_metadata(handle,
2928 ext3_std_error(inode->i_sb, err);
2933 int ext3_change_inode_journal_flag(struct inode *inode, int val)
2940 * We have to be very careful here: changing a data block's
2941 * journaling status dynamically is dangerous. If we write a
2942 * data block to the journal, change the status and then delete
2943 * that block, we risk forgetting to revoke the old log record
2944 * from the journal and so a subsequent replay can corrupt data.
2945 * So, first we make sure that the journal is empty and that
2946 * nobody is changing anything.
2949 journal = EXT3_JOURNAL(inode);
2950 if (is_journal_aborted(journal) || IS_RDONLY(inode))
2953 journal_lock_updates(journal);
2954 journal_flush(journal);
2957 * OK, there are no updates running now, and all cached data is
2958 * synced to disk. We are now in a completely consistent state
2959 * which doesn't have anything in the journal, and we know that
2960 * no filesystem updates are running, so it is safe to modify
2961 * the inode's in-core data-journaling state flag now.
2965 EXT3_I(inode)->i_flags |= EXT3_JOURNAL_DATA_FL;
2967 EXT3_I(inode)->i_flags &= ~EXT3_JOURNAL_DATA_FL;
2968 ext3_set_aops(inode);
2970 journal_unlock_updates(journal);
2972 /* Finally we can mark the inode as dirty. */
2974 handle = ext3_journal_start(inode, 1);
2976 return PTR_ERR(handle);
2978 err = ext3_mark_inode_dirty(handle, inode);
2980 ext3_journal_stop(handle);
2981 ext3_std_error(inode->i_sb, err);