ext3: Convert ext3 to new truncate calling convention
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ext3 / inode.c
blobb4051c9ac5f22c1d187031bb5ad1bd3a75044ce3
1 /*
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)
9 * from
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>
26 #include <linux/fs.h>
27 #include <linux/time.h>
28 #include <linux/ext3_jbd.h>
29 #include <linux/jbd.h>
30 #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>
38 #include <linux/bio.h>
39 #include <linux/fiemap.h>
40 #include <linux/namei.h>
41 #include <trace/events/ext3.h>
42 #include "xattr.h"
43 #include "acl.h"
45 static int ext3_writepage_trans_blocks(struct inode *inode);
48 * Test whether an inode is a fast symlink.
50 static int ext3_inode_is_fast_symlink(struct inode *inode)
52 int ea_blocks = EXT3_I(inode)->i_file_acl ?
53 (inode->i_sb->s_blocksize >> 9) : 0;
55 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
59 * The ext3 forget function must perform a revoke if we are freeing data
60 * which has been journaled. Metadata (eg. indirect blocks) must be
61 * revoked in all cases.
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.
67 int ext3_forget(handle_t *handle, int is_metadata, struct inode *inode,
68 struct buffer_head *bh, ext3_fsblk_t blocknr)
70 int err;
72 might_sleep();
74 trace_ext3_forget(inode, is_metadata, blocknr);
75 BUFFER_TRACE(bh, "enter");
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));
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. */
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);
93 return 0;
97 * data!=journal && (is_metadata || should_journal_data(inode))
99 BUFFER_TRACE(bh, "call ext3_journal_revoke");
100 err = ext3_journal_revoke(handle, blocknr, bh);
101 if (err)
102 ext3_abort(inode->i_sb, __func__,
103 "error %d when attempting revoke", err);
104 BUFFER_TRACE(bh, "exit");
105 return err;
109 * Work out how many blocks we need to proceed with the next chunk of a
110 * truncate transaction.
112 static unsigned long blocks_for_truncate(struct inode *inode)
114 unsigned long needed;
116 needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
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;
127 /* But we need to bound the transaction so we don't overflow the
128 * journal. */
129 if (needed > EXT3_MAX_TRANS_DATA)
130 needed = EXT3_MAX_TRANS_DATA;
132 return EXT3_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
136 * 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.
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
143 * transaction in the top-level truncate loop. --sct
145 static handle_t *start_transaction(struct inode *inode)
147 handle_t *result;
149 result = ext3_journal_start(inode, blocks_for_truncate(inode));
150 if (!IS_ERR(result))
151 return result;
153 ext3_std_error(inode->i_sb, PTR_ERR(result));
154 return result;
158 * Try to extend this transaction for the purposes of truncation.
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.
163 static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
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;
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.
177 static int truncate_restart_transaction(handle_t *handle, struct inode *inode)
179 int ret;
181 jbd_debug(2, "restarting handle %p\n", handle);
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.
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;
195 * Called at inode eviction from icache
197 void ext3_evict_inode (struct inode *inode)
199 struct ext3_block_alloc_info *rsv;
200 handle_t *handle;
201 int want_delete = 0;
203 trace_ext3_evict_inode(inode);
204 if (!inode->i_nlink && !is_bad_inode(inode)) {
205 dquot_initialize(inode);
206 want_delete = 1;
209 truncate_inode_pages(&inode->i_data, 0);
211 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);
217 if (!want_delete)
218 goto no_delete;
220 handle = start_transaction(inode);
221 if (IS_ERR(handle)) {
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.
227 ext3_orphan_del(NULL, inode);
228 goto no_delete;
231 if (IS_SYNC(inode))
232 handle->h_sync = 1;
233 inode->i_size = 0;
234 if (inode->i_blocks)
235 ext3_truncate(inode);
237 * 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.
242 ext3_orphan_del(handle, inode);
243 EXT3_I(inode)->i_dtime = get_seconds();
246 * 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
250 * fails.
252 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);
261 ext3_free_inode(handle, inode);
263 ext3_journal_stop(handle);
264 return;
265 no_delete:
266 end_writeback(inode);
267 dquot_drop(inode);
270 typedef struct {
271 __le32 *p;
272 __le32 key;
273 struct buffer_head *bh;
274 } Indirect;
276 static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
278 p->key = *(p->p = v);
279 p->bh = bh;
282 static int verify_chain(Indirect *from, Indirect *to)
284 while (from <= to && from->key == *from->p)
285 from++;
286 return (from > to);
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.
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.
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).
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.
320 static int ext3_block_to_path(struct inode *inode,
321 long i_block, int offsets[4], int *boundary)
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;
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 {
352 ext3_warning(inode->i_sb, "ext3_block_to_path", "block > big");
354 if (boundary)
355 *boundary = final - 1 - (i_block & (ptrs - 1));
356 return n;
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
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.
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).
388 static Indirect *ext3_get_branch(struct inode *inode, int depth, int *offsets,
389 Indirect chain[4], int *err)
391 struct super_block *sb = inode->i_sb;
392 Indirect *p = chain;
393 struct buffer_head *bh;
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;
412 return NULL;
414 changed:
415 brelse(bh);
416 *err = -EAGAIN;
417 goto no_block;
418 failure:
419 *err = -EIO;
420 no_block:
421 return p;
425 * ext3_find_near - find a place for allocation with sufficient locality
426 * @inode: owner
427 * @ind: descriptor of indirect block.
429 * This function returns the preferred place for block allocation.
430 * 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
435 * cylinder group.
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.
442 * Caller must make sure that @ind is valid and will stay that way.
444 static ext3_fsblk_t ext3_find_near(struct inode *inode, Indirect *ind)
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;
449 ext3_fsblk_t bg_start;
450 ext3_grpblk_t colour;
452 /* Try to find previous block */
453 for (p = ind->p - 1; p >= start; p--) {
454 if (*p)
455 return le32_to_cpu(*p);
458 /* No such thing, so let's try location of indirect block */
459 if (ind->bh)
460 return ind->bh->b_blocknr;
463 * It is going to be referred to from the inode itself? OK, just put it
464 * into the same cylinder group then.
466 bg_start = ext3_group_first_block_no(inode->i_sb, ei->i_block_group);
467 colour = (current->pid % 16) *
468 (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
469 return bg_start + colour;
473 * ext3_find_goal - find a preferred place for allocation.
474 * @inode: owner
475 * @block: block we want
476 * @partial: pointer to the last triple within a chain
478 * Normally this function find the preferred place for block allocation,
479 * returns it.
482 static ext3_fsblk_t ext3_find_goal(struct inode *inode, long block,
483 Indirect *partial)
485 struct ext3_block_alloc_info *block_i;
487 block_i = EXT3_I(inode)->i_block_alloc_info;
490 * try the heuristic for sequential allocation,
491 * failing that at least try to get decent locality.
493 if (block_i && (block == block_i->last_alloc_logical_block + 1)
494 && (block_i->last_alloc_physical_block != 0)) {
495 return block_i->last_alloc_physical_block + 1;
498 return ext3_find_near(inode, partial);
502 * ext3_blks_to_allocate - Look up the block map and count the number
503 * of direct blocks need to be allocated for the given branch.
505 * @branch: chain of indirect blocks
506 * @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
510 * return the total number of blocks to be allocate, including the
511 * direct and indirect blocks.
513 static int ext3_blks_to_allocate(Indirect *branch, int k, unsigned long blks,
514 int blocks_to_boundary)
516 unsigned long count = 0;
519 * Simple case, [t,d]Indirect block(s) has not allocated yet
520 * then it's clear blocks on that path have not allocated
522 if (k > 0) {
523 /* right now we don't handle cross boundary allocation */
524 if (blks < blocks_to_boundary + 1)
525 count += blks;
526 else
527 count += blocks_to_boundary + 1;
528 return count;
531 count++;
532 while (count < blks && count <= blocks_to_boundary &&
533 le32_to_cpu(*(branch[0].p + count)) == 0) {
534 count++;
536 return count;
540 * 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
544 * @indirect_blks: the number of blocks need to allocate for indirect
545 * blocks
546 * @blks: number of blocks need to allocated for direct blocks
547 * @new_blocks: on return it will store the new block numbers for
548 * the indirect blocks(if needed) and the first direct block,
549 * @err: here we store the error value
551 * return the number of direct blocks allocated
553 static int ext3_alloc_blocks(handle_t *handle, struct inode *inode,
554 ext3_fsblk_t goal, int indirect_blks, int blks,
555 ext3_fsblk_t new_blocks[4], int *err)
557 int target, i;
558 unsigned long count = 0;
559 int index = 0;
560 ext3_fsblk_t current_block = 0;
561 int ret = 0;
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)
571 target = blks + indirect_blks;
573 while (1) {
574 count = target;
575 /* allocating blocks for indirect blocks and direct blocks */
576 current_block = ext3_new_blocks(handle,inode,goal,&count,err);
577 if (*err)
578 goto failed_out;
580 target -= count;
581 /* allocate blocks for indirect blocks */
582 while (index < indirect_blks && count) {
583 new_blocks[index++] = current_block++;
584 count--;
587 if (count > 0)
588 break;
591 /* save the new block number for the first direct block */
592 new_blocks[index] = current_block;
594 /* total number of blocks allocated for direct blocks */
595 ret = count;
596 *err = 0;
597 return ret;
598 failed_out:
599 for (i = 0; i <index; i++)
600 ext3_free_blocks(handle, inode, new_blocks[i], 1);
601 return ret;
605 * ext3_alloc_branch - allocate and set up a chain of blocks.
606 * @handle: handle for this transaction
607 * @inode: owner
608 * @indirect_blks: number of allocated indirect blocks
609 * @blks: number of allocated direct blocks
610 * @goal: preferred place for allocation
611 * @offsets: offsets (in the blocks) to store the pointers to next.
612 * @branch: place to store the chain in.
614 * This function allocates blocks, zeroes out all but the last one,
615 * 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
621 * picture as after the successful ext3_get_block(), except that in one
622 * 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.
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.
631 static int ext3_alloc_branch(handle_t *handle, struct inode *inode,
632 int indirect_blks, int *blks, ext3_fsblk_t goal,
633 int *offsets, Indirect *branch)
635 int blocksize = inode->i_sb->s_blocksize;
636 int i, n = 0;
637 int err = 0;
638 struct buffer_head *bh;
639 int num;
640 ext3_fsblk_t new_blocks[4];
641 ext3_fsblk_t current_block;
643 num = ext3_alloc_blocks(handle, inode, goal, indirect_blks,
644 *blks, new_blocks, &err);
645 if (err)
646 return err;
648 branch[0].key = cpu_to_le32(new_blocks[0]);
650 * metadata blocks and data blocks are allocated.
652 for (n = 1; n <= indirect_blks; n++) {
654 * Get buffer_head for parent block, zero it out
655 * and set the pointer to new one, then send
656 * parent to disk.
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) {
664 unlock_buffer(bh);
665 brelse(bh);
666 goto failed;
669 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];
676 * End of chain, update the last new metablock of
677 * the chain to point to the new allocated
678 * data blocks numbers
680 for (i=1; i < num; i++)
681 *(branch[n].p + i) = cpu_to_le32(++current_block);
683 BUFFER_TRACE(bh, "marking uptodate");
684 set_buffer_uptodate(bh);
685 unlock_buffer(bh);
687 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
688 err = ext3_journal_dirty_metadata(handle, bh);
689 if (err)
690 goto failed;
692 *blks = num;
693 return err;
694 failed:
695 /* Allocation failed, free what we already allocated */
696 for (i = 1; i <= n ; i++) {
697 BUFFER_TRACE(branch[i].bh, "call journal_forget");
698 ext3_journal_forget(handle, branch[i].bh);
700 for (i = 0; i <indirect_blks; i++)
701 ext3_free_blocks(handle, inode, new_blocks[i], 1);
703 ext3_free_blocks(handle, inode, new_blocks[i], num);
705 return err;
709 * ext3_splice_branch - splice the allocated branch onto inode.
710 * @handle: handle for this transaction
711 * @inode: owner
712 * @block: (logical) number of block we are adding
713 * @where: location of missing link
714 * @num: number of indirect blocks we are adding
715 * @blks: number of direct blocks we are adding
717 * 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.
721 static int ext3_splice_branch(handle_t *handle, struct inode *inode,
722 long block, Indirect *where, int num, int blks)
724 int i;
725 int err = 0;
726 struct ext3_block_alloc_info *block_i;
727 ext3_fsblk_t current_block;
728 struct ext3_inode_info *ei = EXT3_I(inode);
730 block_i = ei->i_block_alloc_info;
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.
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;
742 /* That's it */
744 *where->p = where->key;
747 * Update the host buffer_head or inode to point to more just allocated
748 * direct blocks blocks
750 if (num == 0 && blks > 1) {
751 current_block = le32_to_cpu(where->key) + 1;
752 for (i = 1; i < blks; i++)
753 *(where->p + i ) = cpu_to_le32(current_block++);
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
761 if (block_i) {
762 block_i->last_alloc_logical_block = block + blks - 1;
763 block_i->last_alloc_physical_block =
764 le32_to_cpu(where[num].key) + blks - 1;
767 /* We are done with atomic stuff, now do the rest of housekeeping */
769 inode->i_ctime = CURRENT_TIME_SEC;
770 ext3_mark_inode_dirty(handle, inode);
771 /* ext3_mark_inode_dirty already updated i_sync_tid */
772 atomic_set(&ei->i_datasync_tid, handle->h_transaction->t_tid);
774 /* had we spliced it onto indirect block? */
775 if (where->bh) {
777 * If we spliced it onto an indirect block, we haven't
778 * 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.
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);
787 if (err)
788 goto err_out;
789 } else {
791 * OK, we spliced it into the inode itself on a direct block.
792 * Inode was dirtied above.
794 jbd_debug(5, "splicing direct\n");
796 return err;
798 err_out:
799 for (i = 1; i <= num; i++) {
800 BUFFER_TRACE(where[i].bh, "call journal_forget");
801 ext3_journal_forget(handle, where[i].bh);
802 ext3_free_blocks(handle,inode,le32_to_cpu(where[i-1].key),1);
804 ext3_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks);
806 return err;
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.
821 * `handle' can be NULL if create == 0.
823 * The BKL may not be held on entry here. Be sure to take it early.
824 * return > 0, # of blocks mapped or allocated.
825 * return = 0, if plain lookup failed.
826 * return < 0, error case.
828 int ext3_get_blocks_handle(handle_t *handle, struct inode *inode,
829 sector_t iblock, unsigned long maxblocks,
830 struct buffer_head *bh_result,
831 int create)
833 int err = -EIO;
834 int offsets[4];
835 Indirect chain[4];
836 Indirect *partial;
837 ext3_fsblk_t goal;
838 int indirect_blks;
839 int blocks_to_boundary = 0;
840 int depth;
841 struct ext3_inode_info *ei = EXT3_I(inode);
842 int count = 0;
843 ext3_fsblk_t first_block = 0;
846 trace_ext3_get_blocks_enter(inode, iblock, maxblocks, create);
847 J_ASSERT(handle != NULL || create == 0);
848 depth = ext3_block_to_path(inode,iblock,offsets,&blocks_to_boundary);
850 if (depth == 0)
851 goto out;
853 partial = ext3_get_branch(inode, depth, offsets, chain, &err);
855 /* Simplest case - block found, no allocation needed */
856 if (!partial) {
857 first_block = le32_to_cpu(chain[depth - 1].key);
858 clear_buffer_new(bh_result);
859 count++;
860 /*map more blocks*/
861 while (count < maxblocks && count <= blocks_to_boundary) {
862 ext3_fsblk_t blk;
864 if (!verify_chain(chain, chain + depth - 1)) {
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.
872 err = -EAGAIN;
873 count = 0;
874 break;
876 blk = le32_to_cpu(*(chain[depth-1].p + count));
878 if (blk == first_block + count)
879 count++;
880 else
881 break;
883 if (err != -EAGAIN)
884 goto got_it;
887 /* Next simple case - plain lookup or failed read of indirect block */
888 if (!create || err == -EIO)
889 goto cleanup;
892 * Block out ext3_truncate while we alter the tree
894 mutex_lock(&ei->truncate_mutex);
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.
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.
908 if (err == -EAGAIN || !verify_chain(chain, partial)) {
909 while (partial > chain) {
910 brelse(partial->bh);
911 partial--;
913 partial = ext3_get_branch(inode, depth, offsets, chain, &err);
914 if (!partial) {
915 count++;
916 mutex_unlock(&ei->truncate_mutex);
917 if (err)
918 goto cleanup;
919 clear_buffer_new(bh_result);
920 goto got_it;
925 * Okay, we need to do block allocation. Lazily initialize the block
926 * allocation info here if necessary
928 if (S_ISREG(inode->i_mode) && (!ei->i_block_alloc_info))
929 ext3_init_block_alloc_info(inode);
931 goal = ext3_find_goal(inode, iblock, partial);
933 /* the number of blocks need to allocate for [d,t]indirect blocks */
934 indirect_blks = (chain + depth) - partial - 1;
937 * Next look up the indirect map to count the totoal number of
938 * direct blocks to allocate for this branch.
940 count = ext3_blks_to_allocate(partial, indirect_blks,
941 maxblocks, blocks_to_boundary);
942 err = ext3_alloc_branch(handle, inode, indirect_blks, &count, goal,
943 offsets + (partial - chain), partial);
946 * The ext3_splice_branch call will free and forget any buffers
947 * 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
950 * may need to return -EAGAIN upwards in the worst case. --sct
952 if (!err)
953 err = ext3_splice_branch(handle, inode, iblock,
954 partial, indirect_blks, count);
955 mutex_unlock(&ei->truncate_mutex);
956 if (err)
957 goto cleanup;
959 set_buffer_new(bh_result);
960 got_it:
961 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
962 if (count > blocks_to_boundary)
963 set_buffer_boundary(bh_result);
964 err = count;
965 /* Clean up and exit */
966 partial = chain + depth - 1; /* the whole chain */
967 cleanup:
968 while (partial > chain) {
969 BUFFER_TRACE(partial->bh, "call brelse");
970 brelse(partial->bh);
971 partial--;
973 BUFFER_TRACE(bh_result, "returned");
974 out:
975 trace_ext3_get_blocks_exit(inode, iblock,
976 depth ? le32_to_cpu(chain[depth-1].key) : 0,
977 count, err);
978 return err;
981 /* Maximum number of blocks we map for direct IO at once. */
982 #define DIO_MAX_BLOCKS 4096
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.
990 #define DIO_CREDITS 25
992 static int ext3_get_block(struct inode *inode, sector_t iblock,
993 struct buffer_head *bh_result, int create)
995 handle_t *handle = ext3_journal_current_handle();
996 int ret = 0, started = 0;
997 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
999 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 +
1003 EXT3_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb));
1004 if (IS_ERR(handle)) {
1005 ret = PTR_ERR(handle);
1006 goto out;
1008 started = 1;
1011 ret = ext3_get_blocks_handle(handle, inode, iblock,
1012 max_blocks, bh_result, create);
1013 if (ret > 0) {
1014 bh_result->b_size = (ret << inode->i_blkbits);
1015 ret = 0;
1017 if (started)
1018 ext3_journal_stop(handle);
1019 out:
1020 return ret;
1023 int ext3_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1024 u64 start, u64 len)
1026 return generic_block_fiemap(inode, fieinfo, start, len,
1027 ext3_get_block);
1031 * `handle' can be NULL if create is zero
1033 struct buffer_head *ext3_getblk(handle_t *handle, struct inode *inode,
1034 long block, int create, int *errp)
1036 struct buffer_head dummy;
1037 int fatal = 0, err;
1039 J_ASSERT(handle != NULL || create == 0);
1041 dummy.b_state = 0;
1042 dummy.b_blocknr = -1000;
1043 buffer_trace_init(&dummy.b_history);
1044 err = ext3_get_blocks_handle(handle, inode, block, 1,
1045 &dummy, create);
1047 * ext3_get_blocks_handle() returns number of blocks
1048 * mapped. 0 in case of a HOLE.
1050 if (err > 0) {
1051 if (err > 1)
1052 WARN_ON(1);
1053 err = 0;
1055 *errp = err;
1056 if (!err && buffer_mapped(&dummy)) {
1057 struct buffer_head *bh;
1058 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
1059 if (!bh) {
1060 *errp = -EIO;
1061 goto err;
1063 if (buffer_new(&dummy)) {
1064 J_ASSERT(create != 0);
1065 J_ASSERT(handle != NULL);
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.
1074 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)) {
1078 memset(bh->b_data,0,inode->i_sb->s_blocksize);
1079 set_buffer_uptodate(bh);
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");
1089 if (fatal) {
1090 *errp = fatal;
1091 brelse(bh);
1092 bh = NULL;
1094 return bh;
1096 err:
1097 return NULL;
1100 struct buffer_head *ext3_bread(handle_t *handle, struct inode *inode,
1101 int block, int create, int *err)
1103 struct buffer_head * bh;
1105 bh = ext3_getblk(handle, inode, block, create, err);
1106 if (!bh)
1107 return bh;
1108 if (buffer_uptodate(bh))
1109 return bh;
1110 ll_rw_block(READ_META, 1, &bh);
1111 wait_on_buffer(bh);
1112 if (buffer_uptodate(bh))
1113 return bh;
1114 put_bh(bh);
1115 *err = -EIO;
1116 return NULL;
1119 static 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))
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;
1133 for ( bh = head, block_start = 0;
1134 ret == 0 && (bh != head || !block_start);
1135 block_start = block_end, bh = next)
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;
1144 err = (*fn)(handle, bh);
1145 if (!ret)
1146 ret = err;
1148 return ret;
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.
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.
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.
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
1174 * write.
1176 static int do_journal_get_write_access(handle_t *handle,
1177 struct buffer_head *bh)
1179 int dirty = buffer_dirty(bh);
1180 int ret;
1182 if (!buffer_mapped(bh) || buffer_freed(bh))
1183 return 0;
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.
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;
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.
1204 static void ext3_truncate_failed_write(struct inode *inode)
1206 truncate_inode_pages(inode->i_mapping, inode->i_size);
1207 ext3_truncate(inode);
1210 static 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)
1214 struct inode *inode = mapping->host;
1215 int ret;
1216 handle_t *handle;
1217 int retries = 0;
1218 struct page *page;
1219 pgoff_t index;
1220 unsigned from, to;
1221 /* 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;
1225 trace_ext3_write_begin(inode, pos, len, flags);
1227 index = pos >> PAGE_CACHE_SHIFT;
1228 from = pos & (PAGE_CACHE_SIZE - 1);
1229 to = from + len;
1231 retry:
1232 page = grab_cache_page_write_begin(mapping, index, flags);
1233 if (!page)
1234 return -ENOMEM;
1235 *pagep = page;
1237 handle = ext3_journal_start(inode, needed_blocks);
1238 if (IS_ERR(handle)) {
1239 unlock_page(page);
1240 page_cache_release(page);
1241 ret = PTR_ERR(handle);
1242 goto out;
1244 ret = __block_write_begin(page, pos, len, ext3_get_block);
1245 if (ret)
1246 goto write_begin_failed;
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);
1252 write_begin_failed:
1253 if (ret) {
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.
1259 * Add inode to orphan list in case we crash before truncate
1260 * finishes. Do this only if ext3_can_truncate() agrees so
1261 * that orphan processing code is happy.
1263 if (pos + len > inode->i_size && ext3_can_truncate(inode))
1264 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)
1269 ext3_truncate_failed_write(inode);
1271 if (ret == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
1272 goto retry;
1273 out:
1274 return ret;
1278 int ext3_journal_dirty_data(handle_t *handle, struct buffer_head *bh)
1280 int err = journal_dirty_data(handle, bh);
1281 if (err)
1282 ext3_journal_abort_handle(__func__, __func__,
1283 bh, handle, err);
1284 return err;
1287 /* For ordered writepage and write_end functions */
1288 static int journal_dirty_data_fn(handle_t *handle, struct buffer_head *bh)
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.
1294 if (buffer_mapped(bh) && buffer_uptodate(bh))
1295 return ext3_journal_dirty_data(handle, bh);
1296 return 0;
1299 /* For write_end() in data=journal mode */
1300 static int write_end_fn(handle_t *handle, struct buffer_head *bh)
1302 if (!buffer_mapped(bh) || buffer_freed(bh))
1303 return 0;
1304 set_buffer_uptodate(bh);
1305 return ext3_journal_dirty_metadata(handle, bh);
1309 * 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.
1314 static void update_file_sizes(struct inode *inode, loff_t pos, unsigned copied)
1316 /* 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;
1321 mark_inode_dirty(inode);
1326 * 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().
1329 * ext3 never places buffers on inode->i_mapping->private_list. metadata
1330 * buffers are managed internally.
1332 static 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)
1337 handle_t *handle = ext3_journal_current_handle();
1338 struct inode *inode = file->f_mapping->host;
1339 unsigned from, to;
1340 int ret = 0, ret2;
1342 trace_ext3_ordered_write_end(inode, pos, len, copied);
1343 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
1345 from = pos & (PAGE_CACHE_SIZE - 1);
1346 to = from + copied;
1347 ret = walk_page_buffers(handle, page_buffers(page),
1348 from, to, NULL, journal_dirty_data_fn);
1350 if (ret == 0)
1351 update_file_sizes(inode, pos, copied);
1353 * There may be allocated blocks outside of i_size because
1354 * we failed to copy some data. Prepare for truncate.
1356 if (pos + len > inode->i_size && ext3_can_truncate(inode))
1357 ext3_orphan_add(handle, inode);
1358 ret2 = ext3_journal_stop(handle);
1359 if (!ret)
1360 ret = ret2;
1361 unlock_page(page);
1362 page_cache_release(page);
1364 if (pos + len > inode->i_size)
1365 ext3_truncate_failed_write(inode);
1366 return ret ? ret : copied;
1369 static 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)
1374 handle_t *handle = ext3_journal_current_handle();
1375 struct inode *inode = file->f_mapping->host;
1376 int ret;
1378 trace_ext3_writeback_write_end(inode, pos, len, copied);
1379 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
1380 update_file_sizes(inode, pos, copied);
1382 * There may be allocated blocks outside of i_size because
1383 * we failed to copy some data. Prepare for truncate.
1385 if (pos + len > inode->i_size && ext3_can_truncate(inode))
1386 ext3_orphan_add(handle, inode);
1387 ret = ext3_journal_stop(handle);
1388 unlock_page(page);
1389 page_cache_release(page);
1391 if (pos + len > inode->i_size)
1392 ext3_truncate_failed_write(inode);
1393 return ret ? ret : copied;
1396 static 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)
1401 handle_t *handle = ext3_journal_current_handle();
1402 struct inode *inode = mapping->host;
1403 int ret = 0, ret2;
1404 int partial = 0;
1405 unsigned from, to;
1407 trace_ext3_journalled_write_end(inode, pos, len, copied);
1408 from = pos & (PAGE_CACHE_SIZE - 1);
1409 to = from + len;
1411 if (copied < len) {
1412 if (!PageUptodate(page))
1413 copied = 0;
1414 page_zero_new_buffers(page, from + copied, to);
1415 to = from + copied;
1418 ret = walk_page_buffers(handle, page_buffers(page), from,
1419 to, &partial, write_end_fn);
1420 if (!partial)
1421 SetPageUptodate(page);
1423 if (pos + copied > inode->i_size)
1424 i_size_write(inode, pos + copied);
1426 * There may be allocated blocks outside of i_size because
1427 * we failed to copy some data. Prepare for truncate.
1429 if (pos + len > inode->i_size && ext3_can_truncate(inode))
1430 ext3_orphan_add(handle, inode);
1431 ext3_set_inode_state(inode, EXT3_STATE_JDATA);
1432 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);
1435 if (!ret)
1436 ret = ret2;
1439 ret2 = ext3_journal_stop(handle);
1440 if (!ret)
1441 ret = ret2;
1442 unlock_page(page);
1443 page_cache_release(page);
1445 if (pos + len > inode->i_size)
1446 ext3_truncate_failed_write(inode);
1447 return ret ? ret : copied;
1451 * 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.
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
1459 * awaiting writeback in the kernel's buffer cache.
1461 * So, if we see any bmap calls here on a modified, data-journaled file,
1462 * take extra steps to flush any blocks which might be in the cache.
1464 static sector_t ext3_bmap(struct address_space *mapping, sector_t block)
1466 struct inode *inode = mapping->host;
1467 journal_t *journal;
1468 int err;
1470 if (ext3_test_inode_state(inode, EXT3_STATE_JDATA)) {
1472 * 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
1475 * do we expect this to happen.
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
1480 * will.)
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.
1489 ext3_clear_inode_state(inode, EXT3_STATE_JDATA);
1490 journal = EXT3_JOURNAL(inode);
1491 journal_lock_updates(journal);
1492 err = journal_flush(journal);
1493 journal_unlock_updates(journal);
1495 if (err)
1496 return 0;
1499 return generic_block_bmap(mapping,block,ext3_get_block);
1502 static int bget_one(handle_t *handle, struct buffer_head *bh)
1504 get_bh(bh);
1505 return 0;
1508 static int bput_one(handle_t *handle, struct buffer_head *bh)
1510 put_bh(bh);
1511 return 0;
1514 static int buffer_unmapped(handle_t *handle, struct buffer_head *bh)
1516 return !buffer_mapped(bh);
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.
1526 * In all journalling modes block_write_full_page() will start the I/O.
1528 * Problem:
1530 * ext3_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
1531 * ext3_writepage()
1533 * Similar for:
1535 * ext3_file_write() -> generic_file_write() -> __alloc_pages() -> ...
1537 * Same applies to ext3_get_block(). We will deadlock on various things like
1538 * lock_journal and i_truncate_mutex.
1540 * Setting PF_MEMALLOC here doesn't work - too many internal memory
1541 * allocations fail.
1543 * 16May01: If we're reentered then journal_current_handle() will be
1544 * non-zero. We simply *return*.
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.
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.
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()).
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.
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.
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.
1571 static int ext3_ordered_writepage(struct page *page,
1572 struct writeback_control *wbc)
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;
1580 J_ASSERT(PageLocked(page));
1581 WARN_ON_ONCE(IS_RDONLY(inode));
1584 * We give up here if we're reentered, because it might be for a
1585 * different filesystem.
1587 if (ext3_journal_current_handle())
1588 goto out_fail;
1590 trace_ext3_ordered_writepage(page);
1591 if (!page_has_buffers(page)) {
1592 create_empty_buffers(page, inode->i_sb->s_blocksize,
1593 (1 << BH_Dirty)|(1 << BH_Uptodate));
1594 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);
1604 handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
1606 if (IS_ERR(handle)) {
1607 ret = PTR_ERR(handle);
1608 goto out_fail;
1611 walk_page_buffers(handle, page_bufs, 0,
1612 PAGE_CACHE_SIZE, NULL, bget_one);
1614 ret = block_write_full_page(page, ext3_get_block, wbc);
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.
1624 * And attach them to the current transaction. But only if
1625 * block_write_full_page() succeeded. Otherwise they are unmapped,
1626 * and generally junk.
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;
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;
1641 out_fail:
1642 redirty_page_for_writepage(wbc, page);
1643 unlock_page(page);
1644 return ret;
1647 static int ext3_writeback_writepage(struct page *page,
1648 struct writeback_control *wbc)
1650 struct inode *inode = page->mapping->host;
1651 handle_t *handle = NULL;
1652 int ret = 0;
1653 int err;
1655 J_ASSERT(PageLocked(page));
1656 WARN_ON_ONCE(IS_RDONLY(inode));
1658 if (ext3_journal_current_handle())
1659 goto out_fail;
1661 trace_ext3_writeback_writepage(page);
1662 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);
1671 handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
1672 if (IS_ERR(handle)) {
1673 ret = PTR_ERR(handle);
1674 goto out_fail;
1677 ret = block_write_full_page(page, ext3_get_block, wbc);
1679 err = ext3_journal_stop(handle);
1680 if (!ret)
1681 ret = err;
1682 return ret;
1684 out_fail:
1685 redirty_page_for_writepage(wbc, page);
1686 unlock_page(page);
1687 return ret;
1690 static int ext3_journalled_writepage(struct page *page,
1691 struct writeback_control *wbc)
1693 struct inode *inode = page->mapping->host;
1694 handle_t *handle = NULL;
1695 int ret = 0;
1696 int err;
1698 J_ASSERT(PageLocked(page));
1699 WARN_ON_ONCE(IS_RDONLY(inode));
1701 if (ext3_journal_current_handle())
1702 goto no_write;
1704 trace_ext3_journalled_writepage(page);
1705 handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
1706 if (IS_ERR(handle)) {
1707 ret = PTR_ERR(handle);
1708 goto no_write;
1711 if (!page_has_buffers(page) || PageChecked(page)) {
1713 * It's mmapped pagecache. Add buffers and journal it. There
1714 * doesn't seem much point in redirtying the page here.
1716 ClearPageChecked(page);
1717 ret = __block_write_begin(page, 0, PAGE_CACHE_SIZE,
1718 ext3_get_block);
1719 if (ret != 0) {
1720 ext3_journal_stop(handle);
1721 goto out_unlock;
1723 ret = walk_page_buffers(handle, page_buffers(page), 0,
1724 PAGE_CACHE_SIZE, NULL, do_journal_get_write_access);
1726 err = walk_page_buffers(handle, page_buffers(page), 0,
1727 PAGE_CACHE_SIZE, NULL, write_end_fn);
1728 if (ret == 0)
1729 ret = err;
1730 ext3_set_inode_state(inode, EXT3_STATE_JDATA);
1731 unlock_page(page);
1732 } else {
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.
1738 ret = block_write_full_page(page, ext3_get_block, wbc);
1740 err = ext3_journal_stop(handle);
1741 if (!ret)
1742 ret = err;
1743 out:
1744 return ret;
1746 no_write:
1747 redirty_page_for_writepage(wbc, page);
1748 out_unlock:
1749 unlock_page(page);
1750 goto out;
1753 static int ext3_readpage(struct file *file, struct page *page)
1755 trace_ext3_readpage(page);
1756 return mpage_readpage(page, ext3_get_block);
1759 static int
1760 ext3_readpages(struct file *file, struct address_space *mapping,
1761 struct list_head *pages, unsigned nr_pages)
1763 return mpage_readpages(mapping, pages, nr_pages, ext3_get_block);
1766 static void ext3_invalidatepage(struct page *page, unsigned long offset)
1768 journal_t *journal = EXT3_JOURNAL(page->mapping->host);
1770 trace_ext3_invalidatepage(page, offset);
1773 * If it's a full truncate we just forget about the pending dirtying
1775 if (offset == 0)
1776 ClearPageChecked(page);
1778 journal_invalidatepage(journal, page, offset);
1781 static int ext3_releasepage(struct page *page, gfp_t wait)
1783 journal_t *journal = EXT3_JOURNAL(page->mapping->host);
1785 trace_ext3_releasepage(page);
1786 WARN_ON(PageChecked(page));
1787 if (!page_has_buffers(page))
1788 return 0;
1789 return journal_try_to_free_buffers(journal, page, wait);
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.
1797 * If the O_DIRECT write is intantiating holes inside i_size and the machine
1798 * 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.
1801 static ssize_t ext3_direct_IO(int rw, struct kiocb *iocb,
1802 const struct iovec *iov, loff_t offset,
1803 unsigned long nr_segs)
1805 struct file *file = iocb->ki_filp;
1806 struct inode *inode = file->f_mapping->host;
1807 struct ext3_inode_info *ei = EXT3_I(inode);
1808 handle_t *handle;
1809 ssize_t ret;
1810 int orphan = 0;
1811 size_t count = iov_length(iov, nr_segs);
1812 int retries = 0;
1814 trace_ext3_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw);
1816 if (rw == WRITE) {
1817 loff_t final_size = offset + count;
1819 if (final_size > inode->i_size) {
1820 /* 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;
1826 ret = ext3_orphan_add(handle, inode);
1827 if (ret) {
1828 ext3_journal_stop(handle);
1829 goto out;
1831 orphan = 1;
1832 ei->i_disksize = inode->i_size;
1833 ext3_journal_stop(handle);
1837 retry:
1838 ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
1839 offset, nr_segs,
1840 ext3_get_block, NULL);
1842 * In case of error extending write may have instantiated a few
1843 * blocks outside i_size. Trim these off again.
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);
1849 if (end > isize)
1850 ext3_truncate_failed_write(inode);
1852 if (ret == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
1853 goto retry;
1855 if (orphan) {
1856 int err;
1858 /* 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
1862 * but cannot extend i_size. Truncate allocated blocks
1863 * and pretend the write failed... */
1864 ext3_truncate_failed_write(inode);
1865 ret = PTR_ERR(handle);
1866 goto out;
1868 if (inode->i_nlink)
1869 ext3_orphan_del(handle, inode);
1870 if (ret > 0) {
1871 loff_t end = offset + ret;
1872 if (end > inode->i_size) {
1873 ei->i_disksize = end;
1874 i_size_write(inode, end);
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.
1882 ext3_mark_inode_dirty(handle, inode);
1885 err = ext3_journal_stop(handle);
1886 if (ret == 0)
1887 ret = err;
1889 out:
1890 trace_ext3_direct_IO_exit(inode, offset,
1891 iov_length(iov, nr_segs), rw, ret);
1892 return ret;
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.
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.
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.
1908 static int ext3_journalled_set_page_dirty(struct page *page)
1910 SetPageChecked(page);
1911 return __set_page_dirty_nobuffers(page);
1914 static const struct address_space_operations ext3_ordered_aops = {
1915 .readpage = ext3_readpage,
1916 .readpages = ext3_readpages,
1917 .writepage = ext3_ordered_writepage,
1918 .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,
1926 .error_remove_page = generic_error_remove_page,
1929 static const struct address_space_operations ext3_writeback_aops = {
1930 .readpage = ext3_readpage,
1931 .readpages = ext3_readpages,
1932 .writepage = ext3_writeback_writepage,
1933 .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,
1941 .error_remove_page = generic_error_remove_page,
1944 static const struct address_space_operations ext3_journalled_aops = {
1945 .readpage = ext3_readpage,
1946 .readpages = ext3_readpages,
1947 .writepage = ext3_journalled_writepage,
1948 .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,
1955 .error_remove_page = generic_error_remove_page,
1958 void ext3_set_aops(struct inode *inode)
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;
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.
1974 static int ext3_block_truncate_page(handle_t *handle, struct page *page,
1975 struct address_space *mapping, loff_t from)
1977 ext3_fsblk_t index = from >> PAGE_CACHE_SHIFT;
1978 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;
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);
1988 if (!page_has_buffers(page))
1989 create_empty_buffers(page, blocksize, 0);
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;
2000 err = 0;
2001 if (buffer_freed(bh)) {
2002 BUFFER_TRACE(bh, "freed: skip");
2003 goto unlock;
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;
2016 /* Ok, it's mapped. Make sure it's up-to-date */
2017 if (PageUptodate(page))
2018 set_buffer_uptodate(bh);
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;
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;
2036 zero_user(page, offset, length);
2037 BUFFER_TRACE(bh, "zeroed end of block");
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);
2048 unlock:
2049 unlock_page(page);
2050 page_cache_release(page);
2051 return err;
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?
2059 static inline int all_zeroes(__le32 *p, __le32 *q)
2061 while (p < q)
2062 if (*p++)
2063 return 0;
2064 return 1;
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
2075 * This is a helper function used by ext3_truncate().
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
2079 * partially truncated if some data below the new i_size is referred
2080 * 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.
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.
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). */
2102 static Indirect *ext3_find_shared(struct inode *inode, int depth,
2103 int offsets[4], Indirect chain[4], __le32 *top)
2105 Indirect *partial, *p;
2106 int k, err;
2108 *top = 0;
2109 /* Make k index the deepest non-null offset + 1 */
2110 for (k = depth; k > 1 && !offsets[k-1]; k--)
2112 partial = ext3_get_branch(inode, k, offsets, chain, &err);
2113 /* Writer: pointers */
2114 if (!partial)
2115 partial = chain + k-1;
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.
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--)
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.
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
2140 /* Writer: end */
2142 while(partial > p) {
2143 brelse(partial->bh);
2144 partial--;
2146 no_top:
2147 return partial;
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.
2155 * We release `count' blocks on disk, but (last - first) may be greater
2156 * than `count' because there can be holes in there.
2158 static void ext3_clear_blocks(handle_t *handle, struct inode *inode,
2159 struct buffer_head *bh, ext3_fsblk_t block_to_free,
2160 unsigned long count, __le32 *first, __le32 *last)
2162 __le32 *p;
2163 if (try_to_extend_transaction(handle, inode)) {
2164 if (bh) {
2165 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
2166 if (ext3_journal_dirty_metadata(handle, bh))
2167 return;
2169 ext3_mark_inode_dirty(handle, inode);
2170 truncate_restart_transaction(handle, inode);
2171 if (bh) {
2172 BUFFER_TRACE(bh, "retaking write access");
2173 if (ext3_journal_get_write_access(handle, bh))
2174 return;
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.
2184 * AKPM: turn on bforget in journal_forget()!!!
2186 for (p = first; p < last; p++) {
2187 u32 nr = le32_to_cpu(*p);
2188 if (nr) {
2189 struct buffer_head *bh;
2191 *p = 0;
2192 bh = sb_find_get_block(inode->i_sb, nr);
2193 ext3_forget(handle, 0, inode, bh, nr);
2197 ext3_free_blocks(handle, inode, block_to_free, count);
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
2208 * We are freeing all blocks referred from that array (numbers are stored as
2209 * little-endian 32-bit) and updating @inode->i_blocks appropriately.
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.
2216 * @this_bh will be %NULL if @first and @last point into the inode's direct
2217 * block pointers.
2219 static void ext3_free_data(handle_t *handle, struct inode *inode,
2220 struct buffer_head *this_bh,
2221 __le32 *first, __le32 *last)
2223 ext3_fsblk_t block_to_free = 0; /* Starting block # of a run */
2224 unsigned long count = 0; /* Number of blocks in the run */
2225 __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
2226 corresponding to
2227 block_to_free */
2228 ext3_fsblk_t nr; /* Current block # */
2229 __le32 *p; /* Pointer into inode/ind
2230 for current block */
2231 int err;
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;
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 {
2253 ext3_clear_blocks(handle, inode, this_bh,
2254 block_to_free,
2255 count, block_to_free_p, p);
2256 block_to_free = nr;
2257 block_to_free_p = p;
2258 count = 1;
2263 if (count > 0)
2264 ext3_clear_blocks(handle, inode, this_bh, block_to_free,
2265 count, block_to_free_p, p);
2267 if (this_bh) {
2268 BUFFER_TRACE(this_bh, "call ext3_journal_dirty_metadata");
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.
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);
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
2296 * We are freeing all blocks referred from these branches (numbers are
2297 * stored as little-endian 32-bit) and updating @inode->i_blocks
2298 * appropriately.
2300 static void ext3_free_branches(handle_t *handle, struct inode *inode,
2301 struct buffer_head *parent_bh,
2302 __le32 *first, __le32 *last, int depth)
2304 ext3_fsblk_t nr;
2305 __le32 *p;
2307 if (is_handle_aborted(handle))
2308 return;
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 */
2319 /* Go read the buffer for the next level down */
2320 bh = sb_bread(inode->i_sb, nr);
2323 * A read failure? Report error and clear slot
2324 * (should be rare).
2326 if (!bh) {
2327 ext3_error(inode->i_sb, "ext3_free_branches",
2328 "Read failure, inode=%lu, block="E3FSBLK,
2329 inode->i_ino, nr);
2330 continue;
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);
2341 * Everything below this this pointer has been
2342 * released. Now let this top-of-subtree go.
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.
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.
2356 if (is_handle_aborted(handle))
2357 return;
2358 if (try_to_extend_transaction(handle, inode)) {
2359 ext3_mark_inode_dirty(handle, inode);
2360 truncate_restart_transaction(handle, inode);
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().
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.
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.
2386 ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
2388 ext3_free_blocks(handle, inode, nr, 1);
2390 if (parent_bh) {
2392 * The block which we have just freed is
2393 * pointed to by an indirect block: journal it
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");
2401 ext3_journal_dirty_metadata(handle,
2402 parent_bh);
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);
2413 int ext3_can_truncate(struct inode *inode)
2415 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;
2425 * ext3_truncate()
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.
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.
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).
2441 * Note that at recovery time, journal replay occurs *before* the restart of
2442 * truncate against the orphan inode list.
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.
2452 void ext3_truncate(struct inode *inode)
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;
2468 trace_ext3_truncate_enter(inode);
2470 if (!ext3_can_truncate(inode))
2471 goto out_notrans;
2473 if (inode->i_size == 0 && ext3_should_writeback_data(inode))
2474 ext3_set_inode_state(inode, EXT3_STATE_FLUSH_ON_CLOSE);
2477 * We have to lock the EOF page here, because lock_page() nests
2478 * outside journal_start().
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)
2487 goto out_notrans;
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);
2498 goto out_notrans;
2501 last_block = (inode->i_size + blocksize-1)
2502 >> EXT3_BLOCK_SIZE_BITS(inode->i_sb);
2504 if (page)
2505 ext3_block_truncate_page(handle, page, mapping, inode->i_size);
2507 n = ext3_block_to_path(inode, last_block, offsets, NULL);
2508 if (n == 0)
2509 goto out_stop; /* error */
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.
2517 * Implication: the file must always be in a sane, consistent
2518 * truncatable state while each transaction commits.
2520 if (ext3_orphan_add(handle, inode))
2521 goto out_stop;
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.
2530 ei->i_disksize = inode->i_size;
2533 * From here we block out all ext3_get_block() callers who want to
2534 * modify the block allocation tree.
2536 mutex_lock(&ei->truncate_mutex);
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;
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;
2553 * We mark the inode dirty prior to restart,
2554 * and prior to stop. No need for it here.
2556 } else {
2557 /* Shared branch grows from an indirect block */
2558 ext3_free_branches(handle, inode, partial->bh,
2559 partial->p,
2560 partial->p+1, (chain+n-1) - partial);
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--;
2572 do_indirects:
2573 /* Kill the remaining (whole) subtrees */
2574 switch (offsets[0]) {
2575 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;
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;
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;
2593 case EXT3_TIND_BLOCK:
2597 ext3_discard_reservation(inode);
2599 mutex_unlock(&ei->truncate_mutex);
2600 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
2601 ext3_mark_inode_dirty(handle, inode);
2604 * In a multi-transaction truncate, we only make the final transaction
2605 * synchronous
2607 if (IS_SYNC(inode))
2608 handle->h_sync = 1;
2609 out_stop:
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
2614 * ext3_evict_inode(), and we allow that function to clean up the
2615 * orphan info for us.
2617 if (inode->i_nlink)
2618 ext3_orphan_del(handle, inode);
2620 ext3_journal_stop(handle);
2621 trace_ext3_truncate_exit(inode);
2622 return;
2623 out_notrans:
2625 * Delete the inode from orphan list so that it doesn't stay there
2626 * forever and trigger assertion on umount.
2628 if (inode->i_nlink)
2629 ext3_orphan_del(NULL, inode);
2630 trace_ext3_truncate_exit(inode);
2633 static ext3_fsblk_t ext3_get_inode_block(struct super_block *sb,
2634 unsigned long ino, struct ext3_iloc *iloc)
2636 unsigned long block_group;
2637 unsigned long offset;
2638 ext3_fsblk_t block;
2639 struct ext3_group_desc *gdp;
2641 if (!ext3_valid_inum(sb, ino)) {
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
2647 return 0;
2650 block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
2651 gdp = ext3_get_group_desc(sb, block_group, NULL);
2652 if (!gdp)
2653 return 0;
2655 * Figure out the offset within the block group inode table
2657 offset = ((ino - 1) % EXT3_INODES_PER_GROUP(sb)) *
2658 EXT3_INODE_SIZE(sb);
2659 block = le32_to_cpu(gdp->bg_inode_table) +
2660 (offset >> EXT3_BLOCK_SIZE_BITS(sb));
2662 iloc->block_group = block_group;
2663 iloc->offset = offset & (EXT3_BLOCK_SIZE(sb) - 1);
2664 return block;
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.
2673 static int __ext3_get_inode_loc(struct inode *inode,
2674 struct ext3_iloc *iloc, int in_mem)
2676 ext3_fsblk_t block;
2677 struct buffer_head *bh;
2679 block = ext3_get_inode_block(inode->i_sb, inode->i_ino, iloc);
2680 if (!block)
2681 return -EIO;
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 - "
2687 "inode=%lu, block="E3FSBLK,
2688 inode->i_ino, block);
2689 return -EIO;
2691 if (!buffer_uptodate(bh)) {
2692 lock_buffer(bh);
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.
2700 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
2701 set_buffer_uptodate(bh);
2703 if (buffer_uptodate(bh)) {
2704 /* someone brought it uptodate while we waited */
2705 unlock_buffer(bh);
2706 goto has_buffer;
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.
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;
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);
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;
2736 bitmap_bh = sb_getblk(inode->i_sb,
2737 le32_to_cpu(desc->bg_inode_bitmap));
2738 if (!bitmap_bh)
2739 goto make_io;
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.
2746 if (!buffer_uptodate(bitmap_bh)) {
2747 brelse(bitmap_bh);
2748 goto make_io;
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;
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;
2766 make_io:
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.
2772 trace_ext3_load_inode(inode);
2773 get_bh(bh);
2774 bh->b_end_io = end_buffer_read_sync;
2775 submit_bh(READ_META, bh);
2776 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 - "
2780 "inode=%lu, block="E3FSBLK,
2781 inode->i_ino, block);
2782 brelse(bh);
2783 return -EIO;
2786 has_buffer:
2787 iloc->bh = bh;
2788 return 0;
2791 int ext3_get_inode_loc(struct inode *inode, struct ext3_iloc *iloc)
2793 /* We have all inode data except xattrs in memory here. */
2794 return __ext3_get_inode_loc(inode, iloc,
2795 !ext3_test_inode_state(inode, EXT3_STATE_XATTR));
2798 void ext3_set_inode_flags(struct inode *inode)
2800 unsigned int flags = EXT3_I(inode)->i_flags;
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;
2815 /* Propagate flags from i_flags to EXT3_I(inode)->i_flags */
2816 void ext3_get_inode_flags(struct ext3_inode_info *ei)
2818 unsigned int flags = ei->vfs_inode.i_flags;
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;
2834 struct inode *ext3_iget(struct super_block *sb, unsigned long ino)
2836 struct ext3_iloc iloc;
2837 struct ext3_inode *raw_inode;
2838 struct ext3_inode_info *ei;
2839 struct buffer_head *bh;
2840 struct inode *inode;
2841 journal_t *journal = EXT3_SB(sb)->s_journal;
2842 transaction_t *transaction;
2843 long ret;
2844 int block;
2846 inode = iget_locked(sb, ino);
2847 if (!inode)
2848 return ERR_PTR(-ENOMEM);
2849 if (!(inode->i_state & I_NEW))
2850 return inode;
2852 ei = EXT3_I(inode);
2853 ei->i_block_alloc_info = NULL;
2855 ret = __ext3_get_inode_loc(inode, &iloc, 0);
2856 if (ret < 0)
2857 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;
2867 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
2868 inode->i_size = le32_to_cpu(raw_inode->i_size);
2869 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);
2872 inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = inode->i_mtime.tv_nsec = 0;
2874 ei->i_state_flags = 0;
2875 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
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);
2887 ret = -ESTALE;
2888 goto bad_inode;
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. */
2895 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;
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;
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!
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);
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.
2927 if (journal) {
2928 tid_t tid;
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);
2944 if (inode->i_ino >= EXT3_FIRST_INO(inode->i_sb) + 1 &&
2945 EXT3_INODE_SIZE(inode->i_sb) > EXT3_GOOD_OLD_INODE_SIZE) {
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.
2951 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
2952 if (EXT3_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
2953 EXT3_INODE_SIZE(inode->i_sb)) {
2954 brelse (bh);
2955 ret = -EIO;
2956 goto bad_inode;
2958 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))
2967 ext3_set_inode_state(inode, EXT3_STATE_XATTR);
2969 } else
2970 ei->i_extra_isize = 0;
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)) {
2980 if (ext3_inode_is_fast_symlink(inode)) {
2981 inode->i_op = &ext3_fast_symlink_inode_operations;
2982 nd_terminate_link(ei->i_data, inode->i_size,
2983 sizeof(ei->i_data) - 1);
2984 } else {
2985 inode->i_op = &ext3_symlink_inode_operations;
2986 ext3_set_aops(inode);
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])));
2993 else
2994 init_special_inode(inode, inode->i_mode,
2995 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
2997 brelse (iloc.bh);
2998 ext3_set_inode_flags(inode);
2999 unlock_new_inode(inode);
3000 return inode;
3002 bad_inode:
3003 iget_failed(inode);
3004 return ERR_PTR(ret);
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.
3012 * The caller must have write access to iloc->bh.
3014 static int ext3_do_update_inode(handle_t *handle,
3015 struct inode *inode,
3016 struct ext3_iloc *iloc)
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;
3023 again:
3024 /* we can't allow multiple procs in here at once, its a bit racey */
3025 lock_buffer(bh);
3027 /* For fields not not tracking in the in-memory inode,
3028 * initialise them to zero for new inodes. */
3029 if (ext3_test_inode_state(inode, EXT3_STATE_NEW))
3030 memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
3032 ext3_get_inode_flags(ei);
3033 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));
3038 * Fix up interoperability with old kernels. Otherwise, old inodes get
3039 * re-used with the upper 16 bits of the uid/gid intact
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;
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;
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.
3086 unlock_buffer(bh);
3087 err = ext3_journal_get_write_access(handle,
3088 EXT3_SB(sb)->s_sbh);
3089 if (err)
3090 goto out_brelse;
3092 ext3_update_dynamic_rev(sb);
3093 EXT3_SET_RO_COMPAT_FEATURE(sb,
3094 EXT3_FEATURE_RO_COMPAT_LARGE_FILE);
3095 handle->h_sync = 1;
3096 err = ext3_journal_dirty_metadata(handle,
3097 EXT3_SB(sb)->s_sbh);
3098 /* get our lock and start over */
3099 goto again;
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;
3115 } else for (block = 0; block < EXT3_N_BLOCKS; block++)
3116 raw_inode->i_block[block] = ei->i_data[block];
3118 if (ei->i_extra_isize)
3119 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
3121 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
3122 unlock_buffer(bh);
3123 rc = ext3_journal_dirty_metadata(handle, bh);
3124 if (!err)
3125 err = rc;
3126 ext3_clear_inode_state(inode, EXT3_STATE_NEW);
3128 atomic_set(&ei->i_sync_tid, handle->h_transaction->t_tid);
3129 out_brelse:
3130 brelse (bh);
3131 ext3_std_error(inode->i_sb, err);
3132 return err;
3136 * ext3_write_inode()
3138 * We are called from a few places:
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.
3144 * - Within sys_sync(), kupdate and such.
3145 * We wait on commit, if tol to.
3147 * - Within prune_icache() (PF_MEMALLOC == true)
3148 * Here we simply return. We can't afford to block kswapd on the
3149 * journal commit.
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.
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.
3160 * It would be a bug for them to not do this. The code:
3162 * mark_inode_dirty(inode)
3163 * stuff();
3164 * inode->i_size = expr;
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.
3170 int ext3_write_inode(struct inode *inode, struct writeback_control *wbc)
3172 if (current->flags & PF_MEMALLOC)
3173 return 0;
3175 if (ext3_journal_current_handle()) {
3176 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
3177 dump_stack();
3178 return -EIO;
3181 if (wbc->sync_mode != WB_SYNC_ALL)
3182 return 0;
3184 return ext3_force_commit(inode->i_sb);
3188 * ext3_setattr()
3190 * Called from notify_change.
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
3200 * leave these blocks visible to the user.)
3202 * Called with inode->sem down.
3204 int ext3_setattr(struct dentry *dentry, struct iattr *attr)
3206 struct inode *inode = dentry->d_inode;
3207 int error, rc = 0;
3208 const unsigned int ia_valid = attr->ia_valid;
3210 error = inode_change_ok(inode, attr);
3211 if (error)
3212 return error;
3214 if (is_quota_modification(inode, attr))
3215 dquot_initialize(inode);
3216 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;
3220 /* (user+group)*(old+new) structure, inode write (sb,
3221 * inode block, ? - but truncate inode update has it) */
3222 handle = ext3_journal_start(inode, EXT3_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+
3223 EXT3_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)+3);
3224 if (IS_ERR(handle)) {
3225 error = PTR_ERR(handle);
3226 goto err_out;
3228 error = dquot_transfer(inode, attr);
3229 if (error) {
3230 ext3_journal_stop(handle);
3231 return error;
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);
3243 if (S_ISREG(inode->i_mode) &&
3244 attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
3245 handle_t *handle;
3247 handle = ext3_journal_start(inode, 3);
3248 if (IS_ERR(handle)) {
3249 error = PTR_ERR(handle);
3250 goto err_out;
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);
3261 if ((attr->ia_valid & ATTR_SIZE) &&
3262 attr->ia_size != i_size_read(inode)) {
3263 truncate_setsize(inode, attr->ia_size);
3264 ext3_truncate(inode);
3267 setattr_copy(inode, attr);
3268 mark_inode_dirty(inode);
3270 if (ia_valid & ATTR_MODE)
3271 rc = ext3_acl_chmod(inode);
3273 err_out:
3274 ext3_std_error(inode->i_sb, error);
3275 if (!error)
3276 error = rc;
3277 return error;
3282 * How many blocks doth make a writepage()?
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
3295 * 3 * (N + 5) + 2 + 2 * EXT3_SINGLEDATA_TRANS_BLOCKS
3297 * With ordered or writeback data it's the same, less the N data blocks.
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".
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.
3308 static int ext3_writepage_trans_blocks(struct inode *inode)
3310 int bpp = ext3_journal_blocks_per_page(inode);
3311 int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
3312 int ret;
3314 if (ext3_should_journal_data(inode))
3315 ret = 3 * (bpp + indirects) + 2;
3316 else
3317 ret = 2 * (bpp + indirects) + indirects + 2;
3319 #ifdef CONFIG_QUOTA
3320 /* We know that structure was already allocated during dquot_initialize so
3321 * we will be updating only the data blocks + inodes */
3322 ret += EXT3_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
3323 #endif
3325 return ret;
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.
3332 int ext3_mark_iloc_dirty(handle_t *handle,
3333 struct inode *inode, struct ext3_iloc *iloc)
3335 int err = 0;
3337 /* the do_update_inode consumes one bh->b_count */
3338 get_bh(iloc->bh);
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;
3347 * On success, We end up with an outstanding reference count against
3348 * iloc->bh. This _must_ be cleaned up later.
3352 ext3_reserve_inode_write(handle_t *handle, struct inode *inode,
3353 struct ext3_iloc *iloc)
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;
3367 ext3_std_error(inode->i_sb, err);
3368 return err;
3372 * 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).
3374 * 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.
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.
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.
3392 int ext3_mark_inode_dirty(handle_t *handle, struct inode *inode)
3394 struct ext3_iloc iloc;
3395 int err;
3397 might_sleep();
3398 trace_ext3_mark_inode_dirty(inode, _RET_IP_);
3399 err = ext3_reserve_inode_write(handle, inode, &iloc);
3400 if (!err)
3401 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
3402 return err;
3406 * ext3_dirty_inode() is called from __mark_inode_dirty()
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.
3412 * Also, dquot_alloc_space() will always dirty the inode when blocks
3413 * are allocated to the file.
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.
3419 void ext3_dirty_inode(struct inode *inode, int flags)
3421 handle_t *current_handle = ext3_journal_current_handle();
3422 handle_t *handle;
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",
3431 __func__);
3432 } else {
3433 jbd_debug(5, "marking dirty. outer handle=%p\n",
3434 current_handle);
3435 ext3_mark_inode_dirty(handle, inode);
3437 ext3_journal_stop(handle);
3438 out:
3439 return;
3442 #if 0
3444 * 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.
3450 static int ext3_pin_inode(handle_t *handle, struct inode *inode)
3452 struct ext3_iloc iloc;
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)
3461 err = ext3_journal_dirty_metadata(handle,
3462 iloc.bh);
3463 brelse(iloc.bh);
3466 ext3_std_error(inode->i_sb, err);
3467 return err;
3469 #endif
3471 int ext3_change_inode_journal_flag(struct inode *inode, int val)
3473 journal_t *journal;
3474 handle_t *handle;
3475 int err;
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.
3487 journal = EXT3_JOURNAL(inode);
3488 if (is_journal_aborted(journal))
3489 return -EROFS;
3491 journal_lock_updates(journal);
3492 journal_flush(journal);
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.
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);
3508 journal_unlock_updates(journal);
3510 /* Finally we can mark the inode as dirty. */
3512 handle = ext3_journal_start(inode, 1);
3513 if (IS_ERR(handle))
3514 return PTR_ERR(handle);
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);
3521 return err;