MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / fs / ext3 / inode.c
blobafde805c375d1b6672afb59d3c82ee51f8f30c9e
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/smp_lock.h>
31 #include <linux/highuid.h>
32 #include <linux/pagemap.h>
33 #include <linux/quotaops.h>
34 #include <linux/string.h>
35 #include <linux/buffer_head.h>
36 #include <linux/writeback.h>
37 #include <linux/mpage.h>
38 #include <linux/uio.h>
39 #include <linux/bio.h>
40 #include "xattr.h"
41 #include "acl.h"
43 static int ext3_writepage_trans_blocks(struct inode *inode);
46 * Test whether an inode is a fast symlink.
48 static int ext3_inode_is_fast_symlink(struct inode *inode)
50 int ea_blocks = EXT3_I(inode)->i_file_acl ?
51 (inode->i_sb->s_blocksize >> 9) : 0;
53 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
57 * The ext3 forget function must perform a revoke if we are freeing data
58 * which has been journaled. Metadata (eg. indirect blocks) must be
59 * revoked in all cases.
61 * "bh" may be NULL: a metadata block may have been freed from memory
62 * but there may still be a record of it in the journal, and that record
63 * still needs to be revoked.
65 int ext3_forget(handle_t *handle, int is_metadata, struct inode *inode,
66 struct buffer_head *bh, ext3_fsblk_t blocknr)
68 int err;
70 might_sleep();
72 BUFFER_TRACE(bh, "enter");
74 jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
75 "data mode %lx\n",
76 bh, is_metadata, inode->i_mode,
77 test_opt(inode->i_sb, DATA_FLAGS));
79 /* Never use the revoke function if we are doing full data
80 * journaling: there is no need to, and a V1 superblock won't
81 * support it. Otherwise, only skip the revoke on un-journaled
82 * data blocks. */
84 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ||
85 (!is_metadata && !ext3_should_journal_data(inode))) {
86 if (bh) {
87 BUFFER_TRACE(bh, "call journal_forget");
88 return ext3_journal_forget(handle, bh);
90 return 0;
94 * data!=journal && (is_metadata || should_journal_data(inode))
96 BUFFER_TRACE(bh, "call ext3_journal_revoke");
97 err = ext3_journal_revoke(handle, blocknr, bh);
98 if (err)
99 ext3_abort(inode->i_sb, __FUNCTION__,
100 "error %d when attempting revoke", err);
101 BUFFER_TRACE(bh, "exit");
102 return err;
106 * Work out how many blocks we need to proceed with the next chunk of a
107 * truncate transaction.
109 static unsigned long blocks_for_truncate(struct inode *inode)
111 unsigned long needed;
113 needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
115 /* Give ourselves just enough room to cope with inodes in which
116 * i_blocks is corrupt: we've seen disk corruptions in the past
117 * which resulted in random data in an inode which looked enough
118 * like a regular file for ext3 to try to delete it. Things
119 * will go a bit crazy if that happens, but at least we should
120 * try not to panic the whole kernel. */
121 if (needed < 2)
122 needed = 2;
124 /* But we need to bound the transaction so we don't overflow the
125 * journal. */
126 if (needed > EXT3_MAX_TRANS_DATA)
127 needed = EXT3_MAX_TRANS_DATA;
129 return EXT3_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
133 * Truncate transactions can be complex and absolutely huge. So we need to
134 * be able to restart the transaction at a conventient checkpoint to make
135 * sure we don't overflow the journal.
137 * start_transaction gets us a new handle for a truncate transaction,
138 * and extend_transaction tries to extend the existing one a bit. If
139 * extend fails, we need to propagate the failure up and restart the
140 * transaction in the top-level truncate loop. --sct
142 static handle_t *start_transaction(struct inode *inode)
144 handle_t *result;
146 result = ext3_journal_start(inode, blocks_for_truncate(inode));
147 if (!IS_ERR(result))
148 return result;
150 ext3_std_error(inode->i_sb, PTR_ERR(result));
151 return result;
155 * Try to extend this transaction for the purposes of truncation.
157 * Returns 0 if we managed to create more room. If we can't create more
158 * room, and the transaction must be restarted we return 1.
160 static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
162 if (handle->h_buffer_credits > EXT3_RESERVE_TRANS_BLOCKS)
163 return 0;
164 if (!ext3_journal_extend(handle, blocks_for_truncate(inode)))
165 return 0;
166 return 1;
170 * Restart the transaction associated with *handle. This does a commit,
171 * so before we call here everything must be consistently dirtied against
172 * this transaction.
174 static int ext3_journal_test_restart(handle_t *handle, struct inode *inode)
176 jbd_debug(2, "restarting handle %p\n", handle);
177 return ext3_journal_restart(handle, blocks_for_truncate(inode));
181 * Called at the last iput() if i_nlink is zero.
183 void ext3_delete_inode (struct inode * inode)
185 handle_t *handle;
187 truncate_inode_pages(&inode->i_data, 0);
189 if (is_bad_inode(inode))
190 goto no_delete;
192 handle = start_transaction(inode);
193 if (IS_ERR(handle)) {
195 * If we're going to skip the normal cleanup, we still need to
196 * make sure that the in-core orphan linked list is properly
197 * cleaned up.
199 ext3_orphan_del(NULL, inode);
200 goto no_delete;
203 if (IS_SYNC(inode))
204 handle->h_sync = 1;
205 inode->i_size = 0;
206 if (inode->i_blocks)
207 ext3_truncate(inode);
209 * Kill off the orphan record which ext3_truncate created.
210 * AKPM: I think this can be inside the above `if'.
211 * Note that ext3_orphan_del() has to be able to cope with the
212 * deletion of a non-existent orphan - this is because we don't
213 * know if ext3_truncate() actually created an orphan record.
214 * (Well, we could do this if we need to, but heck - it works)
216 ext3_orphan_del(handle, inode);
217 EXT3_I(inode)->i_dtime = get_seconds();
220 * One subtle ordering requirement: if anything has gone wrong
221 * (transaction abort, IO errors, whatever), then we can still
222 * do these next steps (the fs will already have been marked as
223 * having errors), but we can't free the inode if the mark_dirty
224 * fails.
226 if (ext3_mark_inode_dirty(handle, inode))
227 /* If that failed, just do the required in-core inode clear. */
228 clear_inode(inode);
229 else
230 ext3_free_inode(handle, inode);
231 ext3_journal_stop(handle);
232 return;
233 no_delete:
234 clear_inode(inode); /* We must guarantee clearing of inode... */
237 typedef struct {
238 __le32 *p;
239 __le32 key;
240 struct buffer_head *bh;
241 } Indirect;
243 static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
245 p->key = *(p->p = v);
246 p->bh = bh;
249 static int verify_chain(Indirect *from, Indirect *to)
251 while (from <= to && from->key == *from->p)
252 from++;
253 return (from > to);
257 * ext3_block_to_path - parse the block number into array of offsets
258 * @inode: inode in question (we are only interested in its superblock)
259 * @i_block: block number to be parsed
260 * @offsets: array to store the offsets in
261 * @boundary: set this non-zero if the referred-to block is likely to be
262 * followed (on disk) by an indirect block.
264 * To store the locations of file's data ext3 uses a data structure common
265 * for UNIX filesystems - tree of pointers anchored in the inode, with
266 * data blocks at leaves and indirect blocks in intermediate nodes.
267 * This function translates the block number into path in that tree -
268 * return value is the path length and @offsets[n] is the offset of
269 * pointer to (n+1)th node in the nth one. If @block is out of range
270 * (negative or too large) warning is printed and zero returned.
272 * Note: function doesn't find node addresses, so no IO is needed. All
273 * we need to know is the capacity of indirect blocks (taken from the
274 * inode->i_sb).
278 * Portability note: the last comparison (check that we fit into triple
279 * indirect block) is spelled differently, because otherwise on an
280 * architecture with 32-bit longs and 8Kb pages we might get into trouble
281 * if our filesystem had 8Kb blocks. We might use long long, but that would
282 * kill us on x86. Oh, well, at least the sign propagation does not matter -
283 * i_block would have to be negative in the very beginning, so we would not
284 * get there at all.
287 static int ext3_block_to_path(struct inode *inode,
288 long i_block, int offsets[4], int *boundary)
290 int ptrs = EXT3_ADDR_PER_BLOCK(inode->i_sb);
291 int ptrs_bits = EXT3_ADDR_PER_BLOCK_BITS(inode->i_sb);
292 const long direct_blocks = EXT3_NDIR_BLOCKS,
293 indirect_blocks = ptrs,
294 double_blocks = (1 << (ptrs_bits * 2));
295 int n = 0;
296 int final = 0;
298 if (i_block < 0) {
299 ext3_warning (inode->i_sb, "ext3_block_to_path", "block < 0");
300 } else if (i_block < direct_blocks) {
301 offsets[n++] = i_block;
302 final = direct_blocks;
303 } else if ( (i_block -= direct_blocks) < indirect_blocks) {
304 offsets[n++] = EXT3_IND_BLOCK;
305 offsets[n++] = i_block;
306 final = ptrs;
307 } else if ((i_block -= indirect_blocks) < double_blocks) {
308 offsets[n++] = EXT3_DIND_BLOCK;
309 offsets[n++] = i_block >> ptrs_bits;
310 offsets[n++] = i_block & (ptrs - 1);
311 final = ptrs;
312 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
313 offsets[n++] = EXT3_TIND_BLOCK;
314 offsets[n++] = i_block >> (ptrs_bits * 2);
315 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
316 offsets[n++] = i_block & (ptrs - 1);
317 final = ptrs;
318 } else {
319 ext3_warning(inode->i_sb, "ext3_block_to_path", "block > big");
321 if (boundary)
322 *boundary = final - 1 - (i_block & (ptrs - 1));
323 return n;
327 * ext3_get_branch - read the chain of indirect blocks leading to data
328 * @inode: inode in question
329 * @depth: depth of the chain (1 - direct pointer, etc.)
330 * @offsets: offsets of pointers in inode/indirect blocks
331 * @chain: place to store the result
332 * @err: here we store the error value
334 * Function fills the array of triples <key, p, bh> and returns %NULL
335 * if everything went OK or the pointer to the last filled triple
336 * (incomplete one) otherwise. Upon the return chain[i].key contains
337 * the number of (i+1)-th block in the chain (as it is stored in memory,
338 * i.e. little-endian 32-bit), chain[i].p contains the address of that
339 * number (it points into struct inode for i==0 and into the bh->b_data
340 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
341 * block for i>0 and NULL for i==0. In other words, it holds the block
342 * numbers of the chain, addresses they were taken from (and where we can
343 * verify that chain did not change) and buffer_heads hosting these
344 * numbers.
346 * Function stops when it stumbles upon zero pointer (absent block)
347 * (pointer to last triple returned, *@err == 0)
348 * or when it gets an IO error reading an indirect block
349 * (ditto, *@err == -EIO)
350 * or when it notices that chain had been changed while it was reading
351 * (ditto, *@err == -EAGAIN)
352 * or when it reads all @depth-1 indirect blocks successfully and finds
353 * the whole chain, all way to the data (returns %NULL, *err == 0).
355 static Indirect *ext3_get_branch(struct inode *inode, int depth, int *offsets,
356 Indirect chain[4], int *err)
358 struct super_block *sb = inode->i_sb;
359 Indirect *p = chain;
360 struct buffer_head *bh;
362 *err = 0;
363 /* i_data is not going away, no lock needed */
364 add_chain (chain, NULL, EXT3_I(inode)->i_data + *offsets);
365 if (!p->key)
366 goto no_block;
367 while (--depth) {
368 bh = sb_bread(sb, le32_to_cpu(p->key));
369 if (!bh)
370 goto failure;
371 /* Reader: pointers */
372 if (!verify_chain(chain, p))
373 goto changed;
374 add_chain(++p, bh, (__le32*)bh->b_data + *++offsets);
375 /* Reader: end */
376 if (!p->key)
377 goto no_block;
379 return NULL;
381 changed:
382 brelse(bh);
383 *err = -EAGAIN;
384 goto no_block;
385 failure:
386 *err = -EIO;
387 no_block:
388 return p;
392 * ext3_find_near - find a place for allocation with sufficient locality
393 * @inode: owner
394 * @ind: descriptor of indirect block.
396 * This function returns the prefered place for block allocation.
397 * It is used when heuristic for sequential allocation fails.
398 * Rules are:
399 * + if there is a block to the left of our position - allocate near it.
400 * + if pointer will live in indirect block - allocate near that block.
401 * + if pointer will live in inode - allocate in the same
402 * cylinder group.
404 * In the latter case we colour the starting block by the callers PID to
405 * prevent it from clashing with concurrent allocations for a different inode
406 * in the same block group. The PID is used here so that functionally related
407 * files will be close-by on-disk.
409 * Caller must make sure that @ind is valid and will stay that way.
411 static ext3_fsblk_t ext3_find_near(struct inode *inode, Indirect *ind)
413 struct ext3_inode_info *ei = EXT3_I(inode);
414 __le32 *start = ind->bh ? (__le32*) ind->bh->b_data : ei->i_data;
415 __le32 *p;
416 ext3_fsblk_t bg_start;
417 ext3_grpblk_t colour;
419 /* Try to find previous block */
420 for (p = ind->p - 1; p >= start; p--) {
421 if (*p)
422 return le32_to_cpu(*p);
425 /* No such thing, so let's try location of indirect block */
426 if (ind->bh)
427 return ind->bh->b_blocknr;
430 * It is going to be referred to from the inode itself? OK, just put it
431 * into the same cylinder group then.
433 bg_start = ext3_group_first_block_no(inode->i_sb, ei->i_block_group);
434 colour = (current->pid % 16) *
435 (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
436 return bg_start + colour;
440 * ext3_find_goal - find a prefered place for allocation.
441 * @inode: owner
442 * @block: block we want
443 * @chain: chain of indirect blocks
444 * @partial: pointer to the last triple within a chain
445 * @goal: place to store the result.
447 * Normally this function find the prefered place for block allocation,
448 * stores it in *@goal and returns zero.
451 static ext3_fsblk_t ext3_find_goal(struct inode *inode, long block,
452 Indirect chain[4], Indirect *partial)
454 struct ext3_block_alloc_info *block_i;
456 block_i = EXT3_I(inode)->i_block_alloc_info;
459 * try the heuristic for sequential allocation,
460 * failing that at least try to get decent locality.
462 if (block_i && (block == block_i->last_alloc_logical_block + 1)
463 && (block_i->last_alloc_physical_block != 0)) {
464 return block_i->last_alloc_physical_block + 1;
467 return ext3_find_near(inode, partial);
471 * ext3_blks_to_allocate: Look up the block map and count the number
472 * of direct blocks need to be allocated for the given branch.
474 * @branch: chain of indirect blocks
475 * @k: number of blocks need for indirect blocks
476 * @blks: number of data blocks to be mapped.
477 * @blocks_to_boundary: the offset in the indirect block
479 * return the total number of blocks to be allocate, including the
480 * direct and indirect blocks.
482 static int ext3_blks_to_allocate(Indirect *branch, int k, unsigned long blks,
483 int blocks_to_boundary)
485 unsigned long count = 0;
488 * Simple case, [t,d]Indirect block(s) has not allocated yet
489 * then it's clear blocks on that path have not allocated
491 if (k > 0) {
492 /* right now we don't handle cross boundary allocation */
493 if (blks < blocks_to_boundary + 1)
494 count += blks;
495 else
496 count += blocks_to_boundary + 1;
497 return count;
500 count++;
501 while (count < blks && count <= blocks_to_boundary &&
502 le32_to_cpu(*(branch[0].p + count)) == 0) {
503 count++;
505 return count;
509 * ext3_alloc_blocks: multiple allocate blocks needed for a branch
510 * @indirect_blks: the number of blocks need to allocate for indirect
511 * blocks
513 * @new_blocks: on return it will store the new block numbers for
514 * the indirect blocks(if needed) and the first direct block,
515 * @blks: on return it will store the total number of allocated
516 * direct blocks
518 static int ext3_alloc_blocks(handle_t *handle, struct inode *inode,
519 ext3_fsblk_t goal, int indirect_blks, int blks,
520 ext3_fsblk_t new_blocks[4], int *err)
522 int target, i;
523 unsigned long count = 0;
524 int index = 0;
525 ext3_fsblk_t current_block = 0;
526 int ret = 0;
529 * Here we try to allocate the requested multiple blocks at once,
530 * on a best-effort basis.
531 * To build a branch, we should allocate blocks for
532 * the indirect blocks(if not allocated yet), and at least
533 * the first direct block of this branch. That's the
534 * minimum number of blocks need to allocate(required)
536 target = blks + indirect_blks;
538 while (1) {
539 count = target;
540 /* allocating blocks for indirect blocks and direct blocks */
541 current_block = ext3_new_blocks(handle,inode,goal,&count,err);
542 if (*err)
543 goto failed_out;
545 target -= count;
546 /* allocate blocks for indirect blocks */
547 while (index < indirect_blks && count) {
548 new_blocks[index++] = current_block++;
549 count--;
552 if (count > 0)
553 break;
556 /* save the new block number for the first direct block */
557 new_blocks[index] = current_block;
559 /* total number of blocks allocated for direct blocks */
560 ret = count;
561 *err = 0;
562 return ret;
563 failed_out:
564 for (i = 0; i <index; i++)
565 ext3_free_blocks(handle, inode, new_blocks[i], 1);
566 return ret;
570 * ext3_alloc_branch - allocate and set up a chain of blocks.
571 * @inode: owner
572 * @indirect_blks: number of allocated indirect blocks
573 * @blks: number of allocated direct blocks
574 * @offsets: offsets (in the blocks) to store the pointers to next.
575 * @branch: place to store the chain in.
577 * This function allocates blocks, zeroes out all but the last one,
578 * links them into chain and (if we are synchronous) writes them to disk.
579 * In other words, it prepares a branch that can be spliced onto the
580 * inode. It stores the information about that chain in the branch[], in
581 * the same format as ext3_get_branch() would do. We are calling it after
582 * we had read the existing part of chain and partial points to the last
583 * triple of that (one with zero ->key). Upon the exit we have the same
584 * picture as after the successful ext3_get_block(), except that in one
585 * place chain is disconnected - *branch->p is still zero (we did not
586 * set the last link), but branch->key contains the number that should
587 * be placed into *branch->p to fill that gap.
589 * If allocation fails we free all blocks we've allocated (and forget
590 * their buffer_heads) and return the error value the from failed
591 * ext3_alloc_block() (normally -ENOSPC). Otherwise we set the chain
592 * as described above and return 0.
594 static int ext3_alloc_branch(handle_t *handle, struct inode *inode,
595 int indirect_blks, int *blks, ext3_fsblk_t goal,
596 int *offsets, Indirect *branch)
598 int blocksize = inode->i_sb->s_blocksize;
599 int i, n = 0;
600 int err = 0;
601 struct buffer_head *bh;
602 int num;
603 ext3_fsblk_t new_blocks[4];
604 ext3_fsblk_t current_block;
606 num = ext3_alloc_blocks(handle, inode, goal, indirect_blks,
607 *blks, new_blocks, &err);
608 if (err)
609 return err;
611 branch[0].key = cpu_to_le32(new_blocks[0]);
613 * metadata blocks and data blocks are allocated.
615 for (n = 1; n <= indirect_blks; n++) {
617 * Get buffer_head for parent block, zero it out
618 * and set the pointer to new one, then send
619 * parent to disk.
621 bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
622 branch[n].bh = bh;
623 lock_buffer(bh);
624 BUFFER_TRACE(bh, "call get_create_access");
625 err = ext3_journal_get_create_access(handle, bh);
626 if (err) {
627 unlock_buffer(bh);
628 brelse(bh);
629 goto failed;
632 memset(bh->b_data, 0, blocksize);
633 branch[n].p = (__le32 *) bh->b_data + offsets[n];
634 branch[n].key = cpu_to_le32(new_blocks[n]);
635 *branch[n].p = branch[n].key;
636 if ( n == indirect_blks) {
637 current_block = new_blocks[n];
639 * End of chain, update the last new metablock of
640 * the chain to point to the new allocated
641 * data blocks numbers
643 for (i=1; i < num; i++)
644 *(branch[n].p + i) = cpu_to_le32(++current_block);
646 BUFFER_TRACE(bh, "marking uptodate");
647 set_buffer_uptodate(bh);
648 unlock_buffer(bh);
650 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
651 err = ext3_journal_dirty_metadata(handle, bh);
652 if (err)
653 goto failed;
655 *blks = num;
656 return err;
657 failed:
658 /* Allocation failed, free what we already allocated */
659 for (i = 1; i <= n ; i++) {
660 BUFFER_TRACE(branch[i].bh, "call journal_forget");
661 ext3_journal_forget(handle, branch[i].bh);
663 for (i = 0; i <indirect_blks; i++)
664 ext3_free_blocks(handle, inode, new_blocks[i], 1);
666 ext3_free_blocks(handle, inode, new_blocks[i], num);
668 return err;
672 * ext3_splice_branch - splice the allocated branch onto inode.
673 * @inode: owner
674 * @block: (logical) number of block we are adding
675 * @chain: chain of indirect blocks (with a missing link - see
676 * ext3_alloc_branch)
677 * @where: location of missing link
678 * @num: number of indirect blocks we are adding
679 * @blks: number of direct blocks we are adding
681 * This function fills the missing link and does all housekeeping needed in
682 * inode (->i_blocks, etc.). In case of success we end up with the full
683 * chain to new block and return 0.
685 static int ext3_splice_branch(handle_t *handle, struct inode *inode,
686 long block, Indirect *where, int num, int blks)
688 int i;
689 int err = 0;
690 struct ext3_block_alloc_info *block_i;
691 ext3_fsblk_t current_block;
693 block_i = EXT3_I(inode)->i_block_alloc_info;
695 * If we're splicing into a [td]indirect block (as opposed to the
696 * inode) then we need to get write access to the [td]indirect block
697 * before the splice.
699 if (where->bh) {
700 BUFFER_TRACE(where->bh, "get_write_access");
701 err = ext3_journal_get_write_access(handle, where->bh);
702 if (err)
703 goto err_out;
705 /* That's it */
707 *where->p = where->key;
710 * Update the host buffer_head or inode to point to more just allocated
711 * direct blocks blocks
713 if (num == 0 && blks > 1) {
714 current_block = le32_to_cpu(where->key) + 1;
715 for (i = 1; i < blks; i++)
716 *(where->p + i ) = cpu_to_le32(current_block++);
720 * update the most recently allocated logical & physical block
721 * in i_block_alloc_info, to assist find the proper goal block for next
722 * allocation
724 if (block_i) {
725 block_i->last_alloc_logical_block = block + blks - 1;
726 block_i->last_alloc_physical_block =
727 le32_to_cpu(where[num].key) + blks - 1;
730 /* We are done with atomic stuff, now do the rest of housekeeping */
732 inode->i_ctime = CURRENT_TIME_SEC;
733 ext3_mark_inode_dirty(handle, inode);
735 /* had we spliced it onto indirect block? */
736 if (where->bh) {
738 * If we spliced it onto an indirect block, we haven't
739 * altered the inode. Note however that if it is being spliced
740 * onto an indirect block at the very end of the file (the
741 * file is growing) then we *will* alter the inode to reflect
742 * the new i_size. But that is not done here - it is done in
743 * generic_commit_write->__mark_inode_dirty->ext3_dirty_inode.
745 jbd_debug(5, "splicing indirect only\n");
746 BUFFER_TRACE(where->bh, "call ext3_journal_dirty_metadata");
747 err = ext3_journal_dirty_metadata(handle, where->bh);
748 if (err)
749 goto err_out;
750 } else {
752 * OK, we spliced it into the inode itself on a direct block.
753 * Inode was dirtied above.
755 jbd_debug(5, "splicing direct\n");
757 return err;
759 err_out:
760 for (i = 1; i <= num; i++) {
761 BUFFER_TRACE(where[i].bh, "call journal_forget");
762 ext3_journal_forget(handle, where[i].bh);
763 ext3_free_blocks(handle,inode,le32_to_cpu(where[i-1].key),1);
765 ext3_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks);
767 return err;
771 * Allocation strategy is simple: if we have to allocate something, we will
772 * have to go the whole way to leaf. So let's do it before attaching anything
773 * to tree, set linkage between the newborn blocks, write them if sync is
774 * required, recheck the path, free and repeat if check fails, otherwise
775 * set the last missing link (that will protect us from any truncate-generated
776 * removals - all blocks on the path are immune now) and possibly force the
777 * write on the parent block.
778 * That has a nice additional property: no special recovery from the failed
779 * allocations is needed - we simply release blocks and do not touch anything
780 * reachable from inode.
782 * `handle' can be NULL if create == 0.
784 * The BKL may not be held on entry here. Be sure to take it early.
785 * return > 0, # of blocks mapped or allocated.
786 * return = 0, if plain lookup failed.
787 * return < 0, error case.
789 int ext3_get_blocks_handle(handle_t *handle, struct inode *inode,
790 sector_t iblock, unsigned long maxblocks,
791 struct buffer_head *bh_result,
792 int create, int extend_disksize)
794 int err = -EIO;
795 int offsets[4];
796 Indirect chain[4];
797 Indirect *partial;
798 ext3_fsblk_t goal;
799 int indirect_blks;
800 int blocks_to_boundary = 0;
801 int depth;
802 struct ext3_inode_info *ei = EXT3_I(inode);
803 int count = 0;
804 ext3_fsblk_t first_block = 0;
807 J_ASSERT(handle != NULL || create == 0);
808 depth = ext3_block_to_path(inode,iblock,offsets,&blocks_to_boundary);
810 if (depth == 0)
811 goto out;
813 partial = ext3_get_branch(inode, depth, offsets, chain, &err);
815 /* Simplest case - block found, no allocation needed */
816 if (!partial) {
817 first_block = le32_to_cpu(chain[depth - 1].key);
818 clear_buffer_new(bh_result);
819 count++;
820 /*map more blocks*/
821 while (count < maxblocks && count <= blocks_to_boundary) {
822 ext3_fsblk_t blk;
824 if (!verify_chain(chain, partial)) {
826 * Indirect block might be removed by
827 * truncate while we were reading it.
828 * Handling of that case: forget what we've
829 * got now. Flag the err as EAGAIN, so it
830 * will reread.
832 err = -EAGAIN;
833 count = 0;
834 break;
836 blk = le32_to_cpu(*(chain[depth-1].p + count));
838 if (blk == first_block + count)
839 count++;
840 else
841 break;
843 if (err != -EAGAIN)
844 goto got_it;
847 /* Next simple case - plain lookup or failed read of indirect block */
848 if (!create || err == -EIO)
849 goto cleanup;
851 mutex_lock(&ei->truncate_mutex);
854 * If the indirect block is missing while we are reading
855 * the chain(ext3_get_branch() returns -EAGAIN err), or
856 * if the chain has been changed after we grab the semaphore,
857 * (either because another process truncated this branch, or
858 * another get_block allocated this branch) re-grab the chain to see if
859 * the request block has been allocated or not.
861 * Since we already block the truncate/other get_block
862 * at this point, we will have the current copy of the chain when we
863 * splice the branch into the tree.
865 if (err == -EAGAIN || !verify_chain(chain, partial)) {
866 while (partial > chain) {
867 brelse(partial->bh);
868 partial--;
870 partial = ext3_get_branch(inode, depth, offsets, chain, &err);
871 if (!partial) {
872 count++;
873 mutex_unlock(&ei->truncate_mutex);
874 if (err)
875 goto cleanup;
876 clear_buffer_new(bh_result);
877 goto got_it;
882 * Okay, we need to do block allocation. Lazily initialize the block
883 * allocation info here if necessary
885 if (S_ISREG(inode->i_mode) && (!ei->i_block_alloc_info))
886 ext3_init_block_alloc_info(inode);
888 goal = ext3_find_goal(inode, iblock, chain, partial);
890 /* the number of blocks need to allocate for [d,t]indirect blocks */
891 indirect_blks = (chain + depth) - partial - 1;
894 * Next look up the indirect map to count the totoal number of
895 * direct blocks to allocate for this branch.
897 count = ext3_blks_to_allocate(partial, indirect_blks,
898 maxblocks, blocks_to_boundary);
900 * Block out ext3_truncate while we alter the tree
902 err = ext3_alloc_branch(handle, inode, indirect_blks, &count, goal,
903 offsets + (partial - chain), partial);
906 * The ext3_splice_branch call will free and forget any buffers
907 * on the new chain if there is a failure, but that risks using
908 * up transaction credits, especially for bitmaps where the
909 * credits cannot be returned. Can we handle this somehow? We
910 * may need to return -EAGAIN upwards in the worst case. --sct
912 if (!err)
913 err = ext3_splice_branch(handle, inode, iblock,
914 partial, indirect_blks, count);
916 * i_disksize growing is protected by truncate_mutex. Don't forget to
917 * protect it if you're about to implement concurrent
918 * ext3_get_block() -bzzz
920 if (!err && extend_disksize && inode->i_size > ei->i_disksize)
921 ei->i_disksize = inode->i_size;
922 mutex_unlock(&ei->truncate_mutex);
923 if (err)
924 goto cleanup;
926 set_buffer_new(bh_result);
927 got_it:
928 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
929 if (count > blocks_to_boundary)
930 set_buffer_boundary(bh_result);
931 err = count;
932 /* Clean up and exit */
933 partial = chain + depth - 1; /* the whole chain */
934 cleanup:
935 while (partial > chain) {
936 BUFFER_TRACE(partial->bh, "call brelse");
937 brelse(partial->bh);
938 partial--;
940 BUFFER_TRACE(bh_result, "returned");
941 out:
942 return err;
945 #define DIO_CREDITS (EXT3_RESERVE_TRANS_BLOCKS + 32)
947 static int ext3_get_block(struct inode *inode, sector_t iblock,
948 struct buffer_head *bh_result, int create)
950 handle_t *handle = journal_current_handle();
951 int ret = 0;
952 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
954 if (!create)
955 goto get_block; /* A read */
957 if (max_blocks == 1)
958 goto get_block; /* A single block get */
960 if (handle->h_transaction->t_state == T_LOCKED) {
962 * Huge direct-io writes can hold off commits for long
963 * periods of time. Let this commit run.
965 ext3_journal_stop(handle);
966 handle = ext3_journal_start(inode, DIO_CREDITS);
967 if (IS_ERR(handle))
968 ret = PTR_ERR(handle);
969 goto get_block;
972 if (handle->h_buffer_credits <= EXT3_RESERVE_TRANS_BLOCKS) {
974 * Getting low on buffer credits...
976 ret = ext3_journal_extend(handle, DIO_CREDITS);
977 if (ret > 0) {
979 * Couldn't extend the transaction. Start a new one.
981 ret = ext3_journal_restart(handle, DIO_CREDITS);
985 get_block:
986 if (ret == 0) {
987 ret = ext3_get_blocks_handle(handle, inode, iblock,
988 max_blocks, bh_result, create, 0);
989 if (ret > 0) {
990 bh_result->b_size = (ret << inode->i_blkbits);
991 ret = 0;
994 return ret;
998 * `handle' can be NULL if create is zero
1000 struct buffer_head *ext3_getblk(handle_t *handle, struct inode *inode,
1001 long block, int create, int *errp)
1003 struct buffer_head dummy;
1004 int fatal = 0, err;
1006 J_ASSERT(handle != NULL || create == 0);
1008 dummy.b_state = 0;
1009 dummy.b_blocknr = -1000;
1010 buffer_trace_init(&dummy.b_history);
1011 err = ext3_get_blocks_handle(handle, inode, block, 1,
1012 &dummy, create, 1);
1014 * ext3_get_blocks_handle() returns number of blocks
1015 * mapped. 0 in case of a HOLE.
1017 if (err > 0) {
1018 if (err > 1)
1019 WARN_ON(1);
1020 err = 0;
1022 *errp = err;
1023 if (!err && buffer_mapped(&dummy)) {
1024 struct buffer_head *bh;
1025 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
1026 if (!bh) {
1027 *errp = -EIO;
1028 goto err;
1030 if (buffer_new(&dummy)) {
1031 J_ASSERT(create != 0);
1032 J_ASSERT(handle != 0);
1035 * Now that we do not always journal data, we should
1036 * keep in mind whether this should always journal the
1037 * new buffer as metadata. For now, regular file
1038 * writes use ext3_get_block instead, so it's not a
1039 * problem.
1041 lock_buffer(bh);
1042 BUFFER_TRACE(bh, "call get_create_access");
1043 fatal = ext3_journal_get_create_access(handle, bh);
1044 if (!fatal && !buffer_uptodate(bh)) {
1045 memset(bh->b_data,0,inode->i_sb->s_blocksize);
1046 set_buffer_uptodate(bh);
1048 unlock_buffer(bh);
1049 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1050 err = ext3_journal_dirty_metadata(handle, bh);
1051 if (!fatal)
1052 fatal = err;
1053 } else {
1054 BUFFER_TRACE(bh, "not a new buffer");
1056 if (fatal) {
1057 *errp = fatal;
1058 brelse(bh);
1059 bh = NULL;
1061 return bh;
1063 err:
1064 return NULL;
1067 struct buffer_head *ext3_bread(handle_t *handle, struct inode *inode,
1068 int block, int create, int *err)
1070 struct buffer_head * bh;
1072 bh = ext3_getblk(handle, inode, block, create, err);
1073 if (!bh)
1074 return bh;
1075 if (buffer_uptodate(bh))
1076 return bh;
1077 ll_rw_block(READ_META, 1, &bh);
1078 wait_on_buffer(bh);
1079 if (buffer_uptodate(bh))
1080 return bh;
1081 put_bh(bh);
1082 *err = -EIO;
1083 return NULL;
1086 static int walk_page_buffers( handle_t *handle,
1087 struct buffer_head *head,
1088 unsigned from,
1089 unsigned to,
1090 int *partial,
1091 int (*fn)( handle_t *handle,
1092 struct buffer_head *bh))
1094 struct buffer_head *bh;
1095 unsigned block_start, block_end;
1096 unsigned blocksize = head->b_size;
1097 int err, ret = 0;
1098 struct buffer_head *next;
1100 for ( bh = head, block_start = 0;
1101 ret == 0 && (bh != head || !block_start);
1102 block_start = block_end, bh = next)
1104 next = bh->b_this_page;
1105 block_end = block_start + blocksize;
1106 if (block_end <= from || block_start >= to) {
1107 if (partial && !buffer_uptodate(bh))
1108 *partial = 1;
1109 continue;
1111 err = (*fn)(handle, bh);
1112 if (!ret)
1113 ret = err;
1115 return ret;
1119 * To preserve ordering, it is essential that the hole instantiation and
1120 * the data write be encapsulated in a single transaction. We cannot
1121 * close off a transaction and start a new one between the ext3_get_block()
1122 * and the commit_write(). So doing the journal_start at the start of
1123 * prepare_write() is the right place.
1125 * Also, this function can nest inside ext3_writepage() ->
1126 * block_write_full_page(). In that case, we *know* that ext3_writepage()
1127 * has generated enough buffer credits to do the whole page. So we won't
1128 * block on the journal in that case, which is good, because the caller may
1129 * be PF_MEMALLOC.
1131 * By accident, ext3 can be reentered when a transaction is open via
1132 * quota file writes. If we were to commit the transaction while thus
1133 * reentered, there can be a deadlock - we would be holding a quota
1134 * lock, and the commit would never complete if another thread had a
1135 * transaction open and was blocking on the quota lock - a ranking
1136 * violation.
1138 * So what we do is to rely on the fact that journal_stop/journal_start
1139 * will _not_ run commit under these circumstances because handle->h_ref
1140 * is elevated. We'll still have enough credits for the tiny quotafile
1141 * write.
1143 static int do_journal_get_write_access(handle_t *handle,
1144 struct buffer_head *bh)
1146 if (!buffer_mapped(bh) || buffer_freed(bh))
1147 return 0;
1148 return ext3_journal_get_write_access(handle, bh);
1151 static int ext3_prepare_write(struct file *file, struct page *page,
1152 unsigned from, unsigned to)
1154 #if 0 // mask by Victor Yu... 07-12-2007
1155 struct inode *inode = page->mapping->host;
1156 #else
1157 struct inode *inode = page->u.xx.mapping->host;
1158 #endif
1159 int ret, needed_blocks = ext3_writepage_trans_blocks(inode);
1160 handle_t *handle;
1161 int retries = 0;
1163 retry:
1164 handle = ext3_journal_start(inode, needed_blocks);
1165 if (IS_ERR(handle)) {
1166 ret = PTR_ERR(handle);
1167 goto out;
1169 if (test_opt(inode->i_sb, NOBH) && ext3_should_writeback_data(inode))
1170 ret = nobh_prepare_write(page, from, to, ext3_get_block);
1171 else
1172 ret = block_prepare_write(page, from, to, ext3_get_block);
1173 if (ret)
1174 goto prepare_write_failed;
1176 if (ext3_should_journal_data(inode)) {
1177 ret = walk_page_buffers(handle, page_buffers(page),
1178 from, to, NULL, do_journal_get_write_access);
1180 prepare_write_failed:
1181 if (ret)
1182 ext3_journal_stop(handle);
1183 if (ret == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
1184 goto retry;
1185 out:
1186 return ret;
1189 int ext3_journal_dirty_data(handle_t *handle, struct buffer_head *bh)
1191 int err = journal_dirty_data(handle, bh);
1192 if (err)
1193 ext3_journal_abort_handle(__FUNCTION__, __FUNCTION__,
1194 bh, handle,err);
1195 return err;
1198 /* For commit_write() in data=journal mode */
1199 static int commit_write_fn(handle_t *handle, struct buffer_head *bh)
1201 if (!buffer_mapped(bh) || buffer_freed(bh))
1202 return 0;
1203 set_buffer_uptodate(bh);
1204 return ext3_journal_dirty_metadata(handle, bh);
1208 * We need to pick up the new inode size which generic_commit_write gave us
1209 * `file' can be NULL - eg, when called from page_symlink().
1211 * ext3 never places buffers on inode->i_mapping->private_list. metadata
1212 * buffers are managed internally.
1214 static int ext3_ordered_commit_write(struct file *file, struct page *page,
1215 unsigned from, unsigned to)
1217 handle_t *handle = ext3_journal_current_handle();
1218 #if 0 // mask by Victor Yu. 07-12-2007
1219 struct inode *inode = page->mapping->host;
1220 #else
1221 struct inode *inode = page->u.xx.mapping->host;
1222 #endif
1223 int ret = 0, ret2;
1225 ret = walk_page_buffers(handle, page_buffers(page),
1226 from, to, NULL, ext3_journal_dirty_data);
1228 if (ret == 0) {
1230 * generic_commit_write() will run mark_inode_dirty() if i_size
1231 * changes. So let's piggyback the i_disksize mark_inode_dirty
1232 * into that.
1234 loff_t new_i_size;
1236 new_i_size = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1237 if (new_i_size > EXT3_I(inode)->i_disksize)
1238 EXT3_I(inode)->i_disksize = new_i_size;
1239 ret = generic_commit_write(file, page, from, to);
1241 ret2 = ext3_journal_stop(handle);
1242 if (!ret)
1243 ret = ret2;
1244 return ret;
1247 static int ext3_writeback_commit_write(struct file *file, struct page *page,
1248 unsigned from, unsigned to)
1250 handle_t *handle = ext3_journal_current_handle();
1251 #if 0 // mask by Victor Yu. 07-12-2007
1252 struct inode *inode = page->mapping->host;
1253 #else
1254 struct inode *inode = page->u.xx.mapping->host;
1255 #endif
1256 int ret = 0, ret2;
1257 loff_t new_i_size;
1259 new_i_size = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1260 if (new_i_size > EXT3_I(inode)->i_disksize)
1261 EXT3_I(inode)->i_disksize = new_i_size;
1263 if (test_opt(inode->i_sb, NOBH) && ext3_should_writeback_data(inode))
1264 ret = nobh_commit_write(file, page, from, to);
1265 else
1266 ret = generic_commit_write(file, page, from, to);
1268 ret2 = ext3_journal_stop(handle);
1269 if (!ret)
1270 ret = ret2;
1271 return ret;
1274 static int ext3_journalled_commit_write(struct file *file,
1275 struct page *page, unsigned from, unsigned to)
1277 handle_t *handle = ext3_journal_current_handle();
1278 #if 0 // mask by Victor Yu. 07-12-2007
1279 struct inode *inode = page->mapping->host;
1280 #else
1281 struct inode *inode = page->u.xx.mapping->host;
1282 #endif
1283 int ret = 0, ret2;
1284 int partial = 0;
1285 loff_t pos;
1288 * Here we duplicate the generic_commit_write() functionality
1290 pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
1292 ret = walk_page_buffers(handle, page_buffers(page), from,
1293 to, &partial, commit_write_fn);
1294 if (!partial)
1295 SetPageUptodate(page);
1296 if (pos > inode->i_size)
1297 i_size_write(inode, pos);
1298 EXT3_I(inode)->i_state |= EXT3_STATE_JDATA;
1299 if (inode->i_size > EXT3_I(inode)->i_disksize) {
1300 EXT3_I(inode)->i_disksize = inode->i_size;
1301 ret2 = ext3_mark_inode_dirty(handle, inode);
1302 if (!ret)
1303 ret = ret2;
1305 ret2 = ext3_journal_stop(handle);
1306 if (!ret)
1307 ret = ret2;
1308 return ret;
1312 * bmap() is special. It gets used by applications such as lilo and by
1313 * the swapper to find the on-disk block of a specific piece of data.
1315 * Naturally, this is dangerous if the block concerned is still in the
1316 * journal. If somebody makes a swapfile on an ext3 data-journaling
1317 * filesystem and enables swap, then they may get a nasty shock when the
1318 * data getting swapped to that swapfile suddenly gets overwritten by
1319 * the original zero's written out previously to the journal and
1320 * awaiting writeback in the kernel's buffer cache.
1322 * So, if we see any bmap calls here on a modified, data-journaled file,
1323 * take extra steps to flush any blocks which might be in the cache.
1325 static sector_t ext3_bmap(struct address_space *mapping, sector_t block)
1327 struct inode *inode = mapping->host;
1328 journal_t *journal;
1329 int err;
1331 if (EXT3_I(inode)->i_state & EXT3_STATE_JDATA) {
1333 * This is a REALLY heavyweight approach, but the use of
1334 * bmap on dirty files is expected to be extremely rare:
1335 * only if we run lilo or swapon on a freshly made file
1336 * do we expect this to happen.
1338 * (bmap requires CAP_SYS_RAWIO so this does not
1339 * represent an unprivileged user DOS attack --- we'd be
1340 * in trouble if mortal users could trigger this path at
1341 * will.)
1343 * NB. EXT3_STATE_JDATA is not set on files other than
1344 * regular files. If somebody wants to bmap a directory
1345 * or symlink and gets confused because the buffer
1346 * hasn't yet been flushed to disk, they deserve
1347 * everything they get.
1350 EXT3_I(inode)->i_state &= ~EXT3_STATE_JDATA;
1351 journal = EXT3_JOURNAL(inode);
1352 journal_lock_updates(journal);
1353 err = journal_flush(journal);
1354 journal_unlock_updates(journal);
1356 if (err)
1357 return 0;
1360 return generic_block_bmap(mapping,block,ext3_get_block);
1363 static int bget_one(handle_t *handle, struct buffer_head *bh)
1365 get_bh(bh);
1366 return 0;
1369 static int bput_one(handle_t *handle, struct buffer_head *bh)
1371 put_bh(bh);
1372 return 0;
1375 static int journal_dirty_data_fn(handle_t *handle, struct buffer_head *bh)
1377 if (buffer_mapped(bh))
1378 return ext3_journal_dirty_data(handle, bh);
1379 return 0;
1383 * Note that we always start a transaction even if we're not journalling
1384 * data. This is to preserve ordering: any hole instantiation within
1385 * __block_write_full_page -> ext3_get_block() should be journalled
1386 * along with the data so we don't crash and then get metadata which
1387 * refers to old data.
1389 * In all journalling modes block_write_full_page() will start the I/O.
1391 * Problem:
1393 * ext3_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
1394 * ext3_writepage()
1396 * Similar for:
1398 * ext3_file_write() -> generic_file_write() -> __alloc_pages() -> ...
1400 * Same applies to ext3_get_block(). We will deadlock on various things like
1401 * lock_journal and i_truncate_mutex.
1403 * Setting PF_MEMALLOC here doesn't work - too many internal memory
1404 * allocations fail.
1406 * 16May01: If we're reentered then journal_current_handle() will be
1407 * non-zero. We simply *return*.
1409 * 1 July 2001: @@@ FIXME:
1410 * In journalled data mode, a data buffer may be metadata against the
1411 * current transaction. But the same file is part of a shared mapping
1412 * and someone does a writepage() on it.
1414 * We will move the buffer onto the async_data list, but *after* it has
1415 * been dirtied. So there's a small window where we have dirty data on
1416 * BJ_Metadata.
1418 * Note that this only applies to the last partial page in the file. The
1419 * bit which block_write_full_page() uses prepare/commit for. (That's
1420 * broken code anyway: it's wrong for msync()).
1422 * It's a rare case: affects the final partial page, for journalled data
1423 * where the file is subject to bith write() and writepage() in the same
1424 * transction. To fix it we'll need a custom block_write_full_page().
1425 * We'll probably need that anyway for journalling writepage() output.
1427 * We don't honour synchronous mounts for writepage(). That would be
1428 * disastrous. Any write() or metadata operation will sync the fs for
1429 * us.
1431 * AKPM2: if all the page's buffers are mapped to disk and !data=journal,
1432 * we don't need to open a transaction here.
1434 static int ext3_ordered_writepage(struct page *page,
1435 struct writeback_control *wbc)
1437 #if 0 // mask by Victor Yu. 07-12-2007
1438 struct inode *inode = page->mapping->host;
1439 #else
1440 struct inode *inode = page->u.xx.mapping->host;
1441 #endif
1442 struct buffer_head *page_bufs;
1443 handle_t *handle = NULL;
1444 int ret = 0;
1445 int err;
1447 J_ASSERT(PageLocked(page));
1450 * We give up here if we're reentered, because it might be for a
1451 * different filesystem.
1453 if (ext3_journal_current_handle())
1454 goto out_fail;
1456 handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
1458 if (IS_ERR(handle)) {
1459 ret = PTR_ERR(handle);
1460 goto out_fail;
1463 if (!page_has_buffers(page)) {
1464 create_empty_buffers(page, inode->i_sb->s_blocksize,
1465 (1 << BH_Dirty)|(1 << BH_Uptodate));
1467 page_bufs = page_buffers(page);
1468 walk_page_buffers(handle, page_bufs, 0,
1469 PAGE_CACHE_SIZE, NULL, bget_one);
1471 ret = block_write_full_page(page, ext3_get_block, wbc);
1474 * The page can become unlocked at any point now, and
1475 * truncate can then come in and change things. So we
1476 * can't touch *page from now on. But *page_bufs is
1477 * safe due to elevated refcount.
1481 * And attach them to the current transaction. But only if
1482 * block_write_full_page() succeeded. Otherwise they are unmapped,
1483 * and generally junk.
1485 if (ret == 0) {
1486 err = walk_page_buffers(handle, page_bufs, 0, PAGE_CACHE_SIZE,
1487 NULL, journal_dirty_data_fn);
1488 if (!ret)
1489 ret = err;
1491 walk_page_buffers(handle, page_bufs, 0,
1492 PAGE_CACHE_SIZE, NULL, bput_one);
1493 err = ext3_journal_stop(handle);
1494 if (!ret)
1495 ret = err;
1496 return ret;
1498 out_fail:
1499 redirty_page_for_writepage(wbc, page);
1500 unlock_page(page);
1501 return ret;
1504 static int ext3_writeback_writepage(struct page *page,
1505 struct writeback_control *wbc)
1507 #if 0 // mask by Victor Yu. 07-12-2007
1508 struct inode *inode = page->mapping->host;
1509 #else
1510 struct inode *inode = page->u.xx.mapping->host;
1511 #endif
1512 handle_t *handle = NULL;
1513 int ret = 0;
1514 int err;
1516 if (ext3_journal_current_handle())
1517 goto out_fail;
1519 handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
1520 if (IS_ERR(handle)) {
1521 ret = PTR_ERR(handle);
1522 goto out_fail;
1525 if (test_opt(inode->i_sb, NOBH) && ext3_should_writeback_data(inode))
1526 ret = nobh_writepage(page, ext3_get_block, wbc);
1527 else
1528 ret = block_write_full_page(page, ext3_get_block, wbc);
1530 err = ext3_journal_stop(handle);
1531 if (!ret)
1532 ret = err;
1533 return ret;
1535 out_fail:
1536 redirty_page_for_writepage(wbc, page);
1537 unlock_page(page);
1538 return ret;
1541 static int ext3_journalled_writepage(struct page *page,
1542 struct writeback_control *wbc)
1544 #if 0 // mask by Victor Yu. 07-12-2007
1545 struct inode *inode = page->mapping->host;
1546 #else
1547 struct inode *inode = page->u.xx.mapping->host;
1548 #endif
1549 handle_t *handle = NULL;
1550 int ret = 0;
1551 int err;
1553 if (ext3_journal_current_handle())
1554 goto no_write;
1556 handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
1557 if (IS_ERR(handle)) {
1558 ret = PTR_ERR(handle);
1559 goto no_write;
1562 if (!page_has_buffers(page) || PageChecked(page)) {
1564 * It's mmapped pagecache. Add buffers and journal it. There
1565 * doesn't seem much point in redirtying the page here.
1567 ClearPageChecked(page);
1568 ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE,
1569 ext3_get_block);
1570 if (ret != 0) {
1571 ext3_journal_stop(handle);
1572 goto out_unlock;
1574 ret = walk_page_buffers(handle, page_buffers(page), 0,
1575 PAGE_CACHE_SIZE, NULL, do_journal_get_write_access);
1577 err = walk_page_buffers(handle, page_buffers(page), 0,
1578 PAGE_CACHE_SIZE, NULL, commit_write_fn);
1579 if (ret == 0)
1580 ret = err;
1581 EXT3_I(inode)->i_state |= EXT3_STATE_JDATA;
1582 unlock_page(page);
1583 } else {
1585 * It may be a page full of checkpoint-mode buffers. We don't
1586 * really know unless we go poke around in the buffer_heads.
1587 * But block_write_full_page will do the right thing.
1589 ret = block_write_full_page(page, ext3_get_block, wbc);
1591 err = ext3_journal_stop(handle);
1592 if (!ret)
1593 ret = err;
1594 out:
1595 return ret;
1597 no_write:
1598 redirty_page_for_writepage(wbc, page);
1599 out_unlock:
1600 unlock_page(page);
1601 goto out;
1604 static int ext3_readpage(struct file *file, struct page *page)
1606 return mpage_readpage(page, ext3_get_block);
1609 static int
1610 ext3_readpages(struct file *file, struct address_space *mapping,
1611 struct list_head *pages, unsigned nr_pages)
1613 return mpage_readpages(mapping, pages, nr_pages, ext3_get_block);
1616 static void ext3_invalidatepage(struct page *page, unsigned long offset)
1618 #if 0 // mask by Victor Yu. 07-12-2007
1619 journal_t *journal = EXT3_JOURNAL(page->mapping->host);
1620 #else
1621 journal_t *journal = EXT3_JOURNAL(page->u.xx.mapping->host);
1622 #endif
1625 * If it's a full truncate we just forget about the pending dirtying
1627 if (offset == 0)
1628 ClearPageChecked(page);
1630 journal_invalidatepage(journal, page, offset);
1633 static int ext3_releasepage(struct page *page, gfp_t wait)
1635 #if 0 // mask by Victor Yu. 07-12-2007
1636 journal_t *journal = EXT3_JOURNAL(page->mapping->host);
1637 #else
1638 journal_t *journal = EXT3_JOURNAL(page->u.xx.mapping->host);
1639 #endif
1641 WARN_ON(PageChecked(page));
1642 if (!page_has_buffers(page))
1643 return 0;
1644 return journal_try_to_free_buffers(journal, page, wait);
1647 #ifdef CONFIG_DIRECTIO
1649 * If the O_DIRECT write will extend the file then add this inode to the
1650 * orphan list. So recovery will truncate it back to the original size
1651 * if the machine crashes during the write.
1653 * If the O_DIRECT write is intantiating holes inside i_size and the machine
1654 * crashes then stale disk data _may_ be exposed inside the file.
1656 static ssize_t ext3_direct_IO(int rw, struct kiocb *iocb,
1657 const struct iovec *iov, loff_t offset,
1658 unsigned long nr_segs)
1660 struct file *file = iocb->ki_filp;
1661 struct inode *inode = file->f_mapping->host;
1662 struct ext3_inode_info *ei = EXT3_I(inode);
1663 handle_t *handle = NULL;
1664 ssize_t ret;
1665 int orphan = 0;
1666 size_t count = iov_length(iov, nr_segs);
1668 if (rw == WRITE) {
1669 loff_t final_size = offset + count;
1671 handle = ext3_journal_start(inode, DIO_CREDITS);
1672 if (IS_ERR(handle)) {
1673 ret = PTR_ERR(handle);
1674 goto out;
1676 if (final_size > inode->i_size) {
1677 ret = ext3_orphan_add(handle, inode);
1678 if (ret)
1679 goto out_stop;
1680 orphan = 1;
1681 ei->i_disksize = inode->i_size;
1685 ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
1686 offset, nr_segs,
1687 ext3_get_block, NULL);
1690 * Reacquire the handle: ext3_get_block() can restart the transaction
1692 handle = journal_current_handle();
1694 out_stop:
1695 if (handle) {
1696 int err;
1698 if (orphan && inode->i_nlink)
1699 ext3_orphan_del(handle, inode);
1700 if (orphan && ret > 0) {
1701 loff_t end = offset + ret;
1702 if (end > inode->i_size) {
1703 ei->i_disksize = end;
1704 i_size_write(inode, end);
1706 * We're going to return a positive `ret'
1707 * here due to non-zero-length I/O, so there's
1708 * no way of reporting error returns from
1709 * ext3_mark_inode_dirty() to userspace. So
1710 * ignore it.
1712 ext3_mark_inode_dirty(handle, inode);
1715 err = ext3_journal_stop(handle);
1716 if (ret == 0)
1717 ret = err;
1719 out:
1720 return ret;
1722 #endif
1725 * Pages can be marked dirty completely asynchronously from ext3's journalling
1726 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
1727 * much here because ->set_page_dirty is called under VFS locks. The page is
1728 * not necessarily locked.
1730 * We cannot just dirty the page and leave attached buffers clean, because the
1731 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
1732 * or jbddirty because all the journalling code will explode.
1734 * So what we do is to mark the page "pending dirty" and next time writepage
1735 * is called, propagate that into the buffers appropriately.
1737 static int ext3_journalled_set_page_dirty(struct page *page)
1739 SetPageChecked(page);
1740 return __set_page_dirty_nobuffers(page);
1743 static const struct address_space_operations ext3_ordered_aops = {
1744 .readpage = ext3_readpage,
1745 .readpages = ext3_readpages,
1746 .writepage = ext3_ordered_writepage,
1747 .sync_page = block_sync_page,
1748 .prepare_write = ext3_prepare_write,
1749 .commit_write = ext3_ordered_commit_write,
1750 .bmap = ext3_bmap,
1751 .invalidatepage = ext3_invalidatepage,
1752 .releasepage = ext3_releasepage,
1753 #ifdef CONFIG_DIRECTIO
1754 .direct_IO = ext3_direct_IO,
1755 #endif
1756 .migratepage = buffer_migrate_page,
1759 static const struct address_space_operations ext3_writeback_aops = {
1760 .readpage = ext3_readpage,
1761 .readpages = ext3_readpages,
1762 .writepage = ext3_writeback_writepage,
1763 .sync_page = block_sync_page,
1764 .prepare_write = ext3_prepare_write,
1765 .commit_write = ext3_writeback_commit_write,
1766 .bmap = ext3_bmap,
1767 .invalidatepage = ext3_invalidatepage,
1768 .releasepage = ext3_releasepage,
1769 #ifdef CONFIG_DIRECTIO
1770 .direct_IO = ext3_direct_IO,
1771 #endif
1772 .migratepage = buffer_migrate_page,
1775 static const struct address_space_operations ext3_journalled_aops = {
1776 .readpage = ext3_readpage,
1777 .readpages = ext3_readpages,
1778 .writepage = ext3_journalled_writepage,
1779 .sync_page = block_sync_page,
1780 .prepare_write = ext3_prepare_write,
1781 .commit_write = ext3_journalled_commit_write,
1782 .set_page_dirty = ext3_journalled_set_page_dirty,
1783 .bmap = ext3_bmap,
1784 .invalidatepage = ext3_invalidatepage,
1785 .releasepage = ext3_releasepage,
1788 void ext3_set_aops(struct inode *inode)
1790 if (ext3_should_order_data(inode))
1791 inode->i_mapping->a_ops = &ext3_ordered_aops;
1792 else if (ext3_should_writeback_data(inode))
1793 inode->i_mapping->a_ops = &ext3_writeback_aops;
1794 else
1795 inode->i_mapping->a_ops = &ext3_journalled_aops;
1799 * ext3_block_truncate_page() zeroes out a mapping from file offset `from'
1800 * up to the end of the block which corresponds to `from'.
1801 * This required during truncate. We need to physically zero the tail end
1802 * of that block so it doesn't yield old data if the file is later grown.
1804 static int ext3_block_truncate_page(handle_t *handle, struct page *page,
1805 struct address_space *mapping, loff_t from)
1807 ext3_fsblk_t index = from >> PAGE_CACHE_SHIFT;
1808 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1809 unsigned blocksize, iblock, length, pos;
1810 struct inode *inode = mapping->host;
1811 struct buffer_head *bh;
1812 int err = 0;
1813 void *kaddr;
1815 blocksize = inode->i_sb->s_blocksize;
1816 length = blocksize - (offset & (blocksize - 1));
1817 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1820 * For "nobh" option, we can only work if we don't need to
1821 * read-in the page - otherwise we create buffers to do the IO.
1823 if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) &&
1824 ext3_should_writeback_data(inode) && PageUptodate(page)) {
1825 kaddr = kmap_atomic(page, KM_USER0);
1826 memset(kaddr + offset, 0, length);
1827 flush_dcache_page(page);
1828 kunmap_atomic(kaddr, KM_USER0);
1829 set_page_dirty(page);
1830 goto unlock;
1833 if (!page_has_buffers(page))
1834 create_empty_buffers(page, blocksize, 0);
1836 /* Find the buffer that contains "offset" */
1837 bh = page_buffers(page);
1838 pos = blocksize;
1839 while (offset >= pos) {
1840 bh = bh->b_this_page;
1841 iblock++;
1842 pos += blocksize;
1845 err = 0;
1846 if (buffer_freed(bh)) {
1847 BUFFER_TRACE(bh, "freed: skip");
1848 goto unlock;
1851 if (!buffer_mapped(bh)) {
1852 BUFFER_TRACE(bh, "unmapped");
1853 ext3_get_block(inode, iblock, bh, 0);
1854 /* unmapped? It's a hole - nothing to do */
1855 if (!buffer_mapped(bh)) {
1856 BUFFER_TRACE(bh, "still unmapped");
1857 goto unlock;
1861 /* Ok, it's mapped. Make sure it's up-to-date */
1862 if (PageUptodate(page))
1863 set_buffer_uptodate(bh);
1865 if (!buffer_uptodate(bh)) {
1866 err = -EIO;
1867 ll_rw_block(READ, 1, &bh);
1868 wait_on_buffer(bh);
1869 /* Uhhuh. Read error. Complain and punt. */
1870 if (!buffer_uptodate(bh))
1871 goto unlock;
1874 if (ext3_should_journal_data(inode)) {
1875 BUFFER_TRACE(bh, "get write access");
1876 err = ext3_journal_get_write_access(handle, bh);
1877 if (err)
1878 goto unlock;
1881 kaddr = kmap_atomic(page, KM_USER0);
1882 memset(kaddr + offset, 0, length);
1883 flush_dcache_page(page);
1884 kunmap_atomic(kaddr, KM_USER0);
1886 BUFFER_TRACE(bh, "zeroed end of block");
1888 err = 0;
1889 if (ext3_should_journal_data(inode)) {
1890 err = ext3_journal_dirty_metadata(handle, bh);
1891 } else {
1892 if (ext3_should_order_data(inode))
1893 err = ext3_journal_dirty_data(handle, bh);
1894 mark_buffer_dirty(bh);
1897 unlock:
1898 unlock_page(page);
1899 page_cache_release(page);
1900 return err;
1904 * Probably it should be a library function... search for first non-zero word
1905 * or memcmp with zero_page, whatever is better for particular architecture.
1906 * Linus?
1908 static inline int all_zeroes(__le32 *p, __le32 *q)
1910 while (p < q)
1911 if (*p++)
1912 return 0;
1913 return 1;
1917 * ext3_find_shared - find the indirect blocks for partial truncation.
1918 * @inode: inode in question
1919 * @depth: depth of the affected branch
1920 * @offsets: offsets of pointers in that branch (see ext3_block_to_path)
1921 * @chain: place to store the pointers to partial indirect blocks
1922 * @top: place to the (detached) top of branch
1924 * This is a helper function used by ext3_truncate().
1926 * When we do truncate() we may have to clean the ends of several
1927 * indirect blocks but leave the blocks themselves alive. Block is
1928 * partially truncated if some data below the new i_size is refered
1929 * from it (and it is on the path to the first completely truncated
1930 * data block, indeed). We have to free the top of that path along
1931 * with everything to the right of the path. Since no allocation
1932 * past the truncation point is possible until ext3_truncate()
1933 * finishes, we may safely do the latter, but top of branch may
1934 * require special attention - pageout below the truncation point
1935 * might try to populate it.
1937 * We atomically detach the top of branch from the tree, store the
1938 * block number of its root in *@top, pointers to buffer_heads of
1939 * partially truncated blocks - in @chain[].bh and pointers to
1940 * their last elements that should not be removed - in
1941 * @chain[].p. Return value is the pointer to last filled element
1942 * of @chain.
1944 * The work left to caller to do the actual freeing of subtrees:
1945 * a) free the subtree starting from *@top
1946 * b) free the subtrees whose roots are stored in
1947 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
1948 * c) free the subtrees growing from the inode past the @chain[0].
1949 * (no partially truncated stuff there). */
1951 static Indirect *ext3_find_shared(struct inode *inode, int depth,
1952 int offsets[4], Indirect chain[4], __le32 *top)
1954 Indirect *partial, *p;
1955 int k, err;
1957 *top = 0;
1958 /* Make k index the deepest non-null offest + 1 */
1959 for (k = depth; k > 1 && !offsets[k-1]; k--)
1961 partial = ext3_get_branch(inode, k, offsets, chain, &err);
1962 /* Writer: pointers */
1963 if (!partial)
1964 partial = chain + k-1;
1966 * If the branch acquired continuation since we've looked at it -
1967 * fine, it should all survive and (new) top doesn't belong to us.
1969 if (!partial->key && *partial->p)
1970 /* Writer: end */
1971 goto no_top;
1972 for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--)
1975 * OK, we've found the last block that must survive. The rest of our
1976 * branch should be detached before unlocking. However, if that rest
1977 * of branch is all ours and does not grow immediately from the inode
1978 * it's easier to cheat and just decrement partial->p.
1980 if (p == chain + k - 1 && p > chain) {
1981 p->p--;
1982 } else {
1983 *top = *p->p;
1984 /* Nope, don't do this in ext3. Must leave the tree intact */
1985 #if 0
1986 *p->p = 0;
1987 #endif
1989 /* Writer: end */
1991 while(partial > p) {
1992 brelse(partial->bh);
1993 partial--;
1995 no_top:
1996 return partial;
2000 * Zero a number of block pointers in either an inode or an indirect block.
2001 * If we restart the transaction we must again get write access to the
2002 * indirect block for further modification.
2004 * We release `count' blocks on disk, but (last - first) may be greater
2005 * than `count' because there can be holes in there.
2007 static void ext3_clear_blocks(handle_t *handle, struct inode *inode,
2008 struct buffer_head *bh, ext3_fsblk_t block_to_free,
2009 unsigned long count, __le32 *first, __le32 *last)
2011 __le32 *p;
2012 if (try_to_extend_transaction(handle, inode)) {
2013 if (bh) {
2014 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
2015 ext3_journal_dirty_metadata(handle, bh);
2017 ext3_mark_inode_dirty(handle, inode);
2018 ext3_journal_test_restart(handle, inode);
2019 if (bh) {
2020 BUFFER_TRACE(bh, "retaking write access");
2021 ext3_journal_get_write_access(handle, bh);
2026 * Any buffers which are on the journal will be in memory. We find
2027 * them on the hash table so journal_revoke() will run journal_forget()
2028 * on them. We've already detached each block from the file, so
2029 * bforget() in journal_forget() should be safe.
2031 * AKPM: turn on bforget in journal_forget()!!!
2033 for (p = first; p < last; p++) {
2034 u32 nr = le32_to_cpu(*p);
2035 if (nr) {
2036 struct buffer_head *bh;
2038 *p = 0;
2039 bh = sb_find_get_block(inode->i_sb, nr);
2040 ext3_forget(handle, 0, inode, bh, nr);
2044 ext3_free_blocks(handle, inode, block_to_free, count);
2048 * ext3_free_data - free a list of data blocks
2049 * @handle: handle for this transaction
2050 * @inode: inode we are dealing with
2051 * @this_bh: indirect buffer_head which contains *@first and *@last
2052 * @first: array of block numbers
2053 * @last: points immediately past the end of array
2055 * We are freeing all blocks refered from that array (numbers are stored as
2056 * little-endian 32-bit) and updating @inode->i_blocks appropriately.
2058 * We accumulate contiguous runs of blocks to free. Conveniently, if these
2059 * blocks are contiguous then releasing them at one time will only affect one
2060 * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
2061 * actually use a lot of journal space.
2063 * @this_bh will be %NULL if @first and @last point into the inode's direct
2064 * block pointers.
2066 static void ext3_free_data(handle_t *handle, struct inode *inode,
2067 struct buffer_head *this_bh,
2068 __le32 *first, __le32 *last)
2070 ext3_fsblk_t block_to_free = 0; /* Starting block # of a run */
2071 unsigned long count = 0; /* Number of blocks in the run */
2072 __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
2073 corresponding to
2074 block_to_free */
2075 ext3_fsblk_t nr; /* Current block # */
2076 __le32 *p; /* Pointer into inode/ind
2077 for current block */
2078 int err;
2080 if (this_bh) { /* For indirect block */
2081 BUFFER_TRACE(this_bh, "get_write_access");
2082 err = ext3_journal_get_write_access(handle, this_bh);
2083 /* Important: if we can't update the indirect pointers
2084 * to the blocks, we can't free them. */
2085 if (err)
2086 return;
2089 for (p = first; p < last; p++) {
2090 nr = le32_to_cpu(*p);
2091 if (nr) {
2092 /* accumulate blocks to free if they're contiguous */
2093 if (count == 0) {
2094 block_to_free = nr;
2095 block_to_free_p = p;
2096 count = 1;
2097 } else if (nr == block_to_free + count) {
2098 count++;
2099 } else {
2100 ext3_clear_blocks(handle, inode, this_bh,
2101 block_to_free,
2102 count, block_to_free_p, p);
2103 block_to_free = nr;
2104 block_to_free_p = p;
2105 count = 1;
2110 if (count > 0)
2111 ext3_clear_blocks(handle, inode, this_bh, block_to_free,
2112 count, block_to_free_p, p);
2114 if (this_bh) {
2115 BUFFER_TRACE(this_bh, "call ext3_journal_dirty_metadata");
2116 ext3_journal_dirty_metadata(handle, this_bh);
2121 * ext3_free_branches - free an array of branches
2122 * @handle: JBD handle for this transaction
2123 * @inode: inode we are dealing with
2124 * @parent_bh: the buffer_head which contains *@first and *@last
2125 * @first: array of block numbers
2126 * @last: pointer immediately past the end of array
2127 * @depth: depth of the branches to free
2129 * We are freeing all blocks refered from these branches (numbers are
2130 * stored as little-endian 32-bit) and updating @inode->i_blocks
2131 * appropriately.
2133 static void ext3_free_branches(handle_t *handle, struct inode *inode,
2134 struct buffer_head *parent_bh,
2135 __le32 *first, __le32 *last, int depth)
2137 ext3_fsblk_t nr;
2138 __le32 *p;
2140 if (is_handle_aborted(handle))
2141 return;
2143 if (depth--) {
2144 struct buffer_head *bh;
2145 int addr_per_block = EXT3_ADDR_PER_BLOCK(inode->i_sb);
2146 p = last;
2147 while (--p >= first) {
2148 nr = le32_to_cpu(*p);
2149 if (!nr)
2150 continue; /* A hole */
2152 /* Go read the buffer for the next level down */
2153 bh = sb_bread(inode->i_sb, nr);
2156 * A read failure? Report error and clear slot
2157 * (should be rare).
2159 if (!bh) {
2160 ext3_error(inode->i_sb, "ext3_free_branches",
2161 "Read failure, inode=%lu, block="E3FSBLK,
2162 inode->i_ino, nr);
2163 continue;
2166 /* This zaps the entire block. Bottom up. */
2167 BUFFER_TRACE(bh, "free child branches");
2168 ext3_free_branches(handle, inode, bh,
2169 (__le32*)bh->b_data,
2170 (__le32*)bh->b_data + addr_per_block,
2171 depth);
2174 * We've probably journalled the indirect block several
2175 * times during the truncate. But it's no longer
2176 * needed and we now drop it from the transaction via
2177 * journal_revoke().
2179 * That's easy if it's exclusively part of this
2180 * transaction. But if it's part of the committing
2181 * transaction then journal_forget() will simply
2182 * brelse() it. That means that if the underlying
2183 * block is reallocated in ext3_get_block(),
2184 * unmap_underlying_metadata() will find this block
2185 * and will try to get rid of it. damn, damn.
2187 * If this block has already been committed to the
2188 * journal, a revoke record will be written. And
2189 * revoke records must be emitted *before* clearing
2190 * this block's bit in the bitmaps.
2192 ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
2195 * Everything below this this pointer has been
2196 * released. Now let this top-of-subtree go.
2198 * We want the freeing of this indirect block to be
2199 * atomic in the journal with the updating of the
2200 * bitmap block which owns it. So make some room in
2201 * the journal.
2203 * We zero the parent pointer *after* freeing its
2204 * pointee in the bitmaps, so if extend_transaction()
2205 * for some reason fails to put the bitmap changes and
2206 * the release into the same transaction, recovery
2207 * will merely complain about releasing a free block,
2208 * rather than leaking blocks.
2210 if (is_handle_aborted(handle))
2211 return;
2212 if (try_to_extend_transaction(handle, inode)) {
2213 ext3_mark_inode_dirty(handle, inode);
2214 ext3_journal_test_restart(handle, inode);
2217 ext3_free_blocks(handle, inode, nr, 1);
2219 if (parent_bh) {
2221 * The block which we have just freed is
2222 * pointed to by an indirect block: journal it
2224 BUFFER_TRACE(parent_bh, "get_write_access");
2225 if (!ext3_journal_get_write_access(handle,
2226 parent_bh)){
2227 *p = 0;
2228 BUFFER_TRACE(parent_bh,
2229 "call ext3_journal_dirty_metadata");
2230 ext3_journal_dirty_metadata(handle,
2231 parent_bh);
2235 } else {
2236 /* We have reached the bottom of the tree. */
2237 BUFFER_TRACE(parent_bh, "free data blocks");
2238 ext3_free_data(handle, inode, parent_bh, first, last);
2243 * ext3_truncate()
2245 * We block out ext3_get_block() block instantiations across the entire
2246 * transaction, and VFS/VM ensures that ext3_truncate() cannot run
2247 * simultaneously on behalf of the same inode.
2249 * As we work through the truncate and commmit bits of it to the journal there
2250 * is one core, guiding principle: the file's tree must always be consistent on
2251 * disk. We must be able to restart the truncate after a crash.
2253 * The file's tree may be transiently inconsistent in memory (although it
2254 * probably isn't), but whenever we close off and commit a journal transaction,
2255 * the contents of (the filesystem + the journal) must be consistent and
2256 * restartable. It's pretty simple, really: bottom up, right to left (although
2257 * left-to-right works OK too).
2259 * Note that at recovery time, journal replay occurs *before* the restart of
2260 * truncate against the orphan inode list.
2262 * The committed inode has the new, desired i_size (which is the same as
2263 * i_disksize in this case). After a crash, ext3_orphan_cleanup() will see
2264 * that this inode's truncate did not complete and it will again call
2265 * ext3_truncate() to have another go. So there will be instantiated blocks
2266 * to the right of the truncation point in a crashed ext3 filesystem. But
2267 * that's fine - as long as they are linked from the inode, the post-crash
2268 * ext3_truncate() run will find them and release them.
2270 void ext3_truncate(struct inode *inode)
2272 handle_t *handle;
2273 struct ext3_inode_info *ei = EXT3_I(inode);
2274 __le32 *i_data = ei->i_data;
2275 int addr_per_block = EXT3_ADDR_PER_BLOCK(inode->i_sb);
2276 struct address_space *mapping = inode->i_mapping;
2277 int offsets[4];
2278 Indirect chain[4];
2279 Indirect *partial;
2280 __le32 nr = 0;
2281 int n;
2282 long last_block;
2283 unsigned blocksize = inode->i_sb->s_blocksize;
2284 struct page *page;
2286 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2287 S_ISLNK(inode->i_mode)))
2288 return;
2289 if (ext3_inode_is_fast_symlink(inode))
2290 return;
2291 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2292 return;
2295 * We have to lock the EOF page here, because lock_page() nests
2296 * outside journal_start().
2298 if ((inode->i_size & (blocksize - 1)) == 0) {
2299 /* Block boundary? Nothing to do */
2300 page = NULL;
2301 } else {
2302 page = grab_cache_page(mapping,
2303 inode->i_size >> PAGE_CACHE_SHIFT);
2304 if (!page)
2305 return;
2308 handle = start_transaction(inode);
2309 if (IS_ERR(handle)) {
2310 if (page) {
2311 clear_highpage(page);
2312 flush_dcache_page(page);
2313 unlock_page(page);
2314 page_cache_release(page);
2316 return; /* AKPM: return what? */
2319 last_block = (inode->i_size + blocksize-1)
2320 >> EXT3_BLOCK_SIZE_BITS(inode->i_sb);
2322 if (page)
2323 ext3_block_truncate_page(handle, page, mapping, inode->i_size);
2325 n = ext3_block_to_path(inode, last_block, offsets, NULL);
2326 if (n == 0)
2327 goto out_stop; /* error */
2330 * OK. This truncate is going to happen. We add the inode to the
2331 * orphan list, so that if this truncate spans multiple transactions,
2332 * and we crash, we will resume the truncate when the filesystem
2333 * recovers. It also marks the inode dirty, to catch the new size.
2335 * Implication: the file must always be in a sane, consistent
2336 * truncatable state while each transaction commits.
2338 if (ext3_orphan_add(handle, inode))
2339 goto out_stop;
2342 * The orphan list entry will now protect us from any crash which
2343 * occurs before the truncate completes, so it is now safe to propagate
2344 * the new, shorter inode size (held for now in i_size) into the
2345 * on-disk inode. We do this via i_disksize, which is the value which
2346 * ext3 *really* writes onto the disk inode.
2348 ei->i_disksize = inode->i_size;
2351 * From here we block out all ext3_get_block() callers who want to
2352 * modify the block allocation tree.
2354 mutex_lock(&ei->truncate_mutex);
2356 if (n == 1) { /* direct blocks */
2357 ext3_free_data(handle, inode, NULL, i_data+offsets[0],
2358 i_data + EXT3_NDIR_BLOCKS);
2359 goto do_indirects;
2362 partial = ext3_find_shared(inode, n, offsets, chain, &nr);
2363 /* Kill the top of shared branch (not detached) */
2364 if (nr) {
2365 if (partial == chain) {
2366 /* Shared branch grows from the inode */
2367 ext3_free_branches(handle, inode, NULL,
2368 &nr, &nr+1, (chain+n-1) - partial);
2369 *partial->p = 0;
2371 * We mark the inode dirty prior to restart,
2372 * and prior to stop. No need for it here.
2374 } else {
2375 /* Shared branch grows from an indirect block */
2376 BUFFER_TRACE(partial->bh, "get_write_access");
2377 ext3_free_branches(handle, inode, partial->bh,
2378 partial->p,
2379 partial->p+1, (chain+n-1) - partial);
2382 /* Clear the ends of indirect blocks on the shared branch */
2383 while (partial > chain) {
2384 ext3_free_branches(handle, inode, partial->bh, partial->p + 1,
2385 (__le32*)partial->bh->b_data+addr_per_block,
2386 (chain+n-1) - partial);
2387 BUFFER_TRACE(partial->bh, "call brelse");
2388 brelse (partial->bh);
2389 partial--;
2391 do_indirects:
2392 /* Kill the remaining (whole) subtrees */
2393 switch (offsets[0]) {
2394 default:
2395 nr = i_data[EXT3_IND_BLOCK];
2396 if (nr) {
2397 ext3_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
2398 i_data[EXT3_IND_BLOCK] = 0;
2400 case EXT3_IND_BLOCK:
2401 nr = i_data[EXT3_DIND_BLOCK];
2402 if (nr) {
2403 ext3_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
2404 i_data[EXT3_DIND_BLOCK] = 0;
2406 case EXT3_DIND_BLOCK:
2407 nr = i_data[EXT3_TIND_BLOCK];
2408 if (nr) {
2409 ext3_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
2410 i_data[EXT3_TIND_BLOCK] = 0;
2412 case EXT3_TIND_BLOCK:
2416 ext3_discard_reservation(inode);
2418 mutex_unlock(&ei->truncate_mutex);
2419 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
2420 ext3_mark_inode_dirty(handle, inode);
2423 * In a multi-transaction truncate, we only make the final transaction
2424 * synchronous
2426 if (IS_SYNC(inode))
2427 handle->h_sync = 1;
2428 out_stop:
2430 * If this was a simple ftruncate(), and the file will remain alive
2431 * then we need to clear up the orphan record which we created above.
2432 * However, if this was a real unlink then we were called by
2433 * ext3_delete_inode(), and we allow that function to clean up the
2434 * orphan info for us.
2436 if (inode->i_nlink)
2437 ext3_orphan_del(handle, inode);
2439 ext3_journal_stop(handle);
2442 static ext3_fsblk_t ext3_get_inode_block(struct super_block *sb,
2443 unsigned long ino, struct ext3_iloc *iloc)
2445 unsigned long desc, group_desc, block_group;
2446 unsigned long offset;
2447 ext3_fsblk_t block;
2448 struct buffer_head *bh;
2449 struct ext3_group_desc * gdp;
2451 if (!ext3_valid_inum(sb, ino)) {
2453 * This error is already checked for in namei.c unless we are
2454 * looking at an NFS filehandle, in which case no error
2455 * report is needed
2457 return 0;
2460 block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
2461 if (block_group >= EXT3_SB(sb)->s_groups_count) {
2462 ext3_error(sb,"ext3_get_inode_block","group >= groups count");
2463 return 0;
2465 smp_rmb();
2466 group_desc = block_group >> EXT3_DESC_PER_BLOCK_BITS(sb);
2467 desc = block_group & (EXT3_DESC_PER_BLOCK(sb) - 1);
2468 bh = EXT3_SB(sb)->s_group_desc[group_desc];
2469 if (!bh) {
2470 ext3_error (sb, "ext3_get_inode_block",
2471 "Descriptor not loaded");
2472 return 0;
2475 gdp = (struct ext3_group_desc *)bh->b_data;
2477 * Figure out the offset within the block group inode table
2479 offset = ((ino - 1) % EXT3_INODES_PER_GROUP(sb)) *
2480 EXT3_INODE_SIZE(sb);
2481 block = le32_to_cpu(gdp[desc].bg_inode_table) +
2482 (offset >> EXT3_BLOCK_SIZE_BITS(sb));
2484 iloc->block_group = block_group;
2485 iloc->offset = offset & (EXT3_BLOCK_SIZE(sb) - 1);
2486 return block;
2490 * ext3_get_inode_loc returns with an extra refcount against the inode's
2491 * underlying buffer_head on success. If 'in_mem' is true, we have all
2492 * data in memory that is needed to recreate the on-disk version of this
2493 * inode.
2495 static int __ext3_get_inode_loc(struct inode *inode,
2496 struct ext3_iloc *iloc, int in_mem)
2498 ext3_fsblk_t block;
2499 struct buffer_head *bh;
2501 block = ext3_get_inode_block(inode->i_sb, inode->i_ino, iloc);
2502 if (!block)
2503 return -EIO;
2505 bh = sb_getblk(inode->i_sb, block);
2506 if (!bh) {
2507 ext3_error (inode->i_sb, "ext3_get_inode_loc",
2508 "unable to read inode block - "
2509 "inode=%lu, block="E3FSBLK,
2510 inode->i_ino, block);
2511 return -EIO;
2513 if (!buffer_uptodate(bh)) {
2514 lock_buffer(bh);
2515 if (buffer_uptodate(bh)) {
2516 /* someone brought it uptodate while we waited */
2517 unlock_buffer(bh);
2518 goto has_buffer;
2522 * If we have all information of the inode in memory and this
2523 * is the only valid inode in the block, we need not read the
2524 * block.
2526 if (in_mem) {
2527 struct buffer_head *bitmap_bh;
2528 struct ext3_group_desc *desc;
2529 int inodes_per_buffer;
2530 int inode_offset, i;
2531 int block_group;
2532 int start;
2534 block_group = (inode->i_ino - 1) /
2535 EXT3_INODES_PER_GROUP(inode->i_sb);
2536 inodes_per_buffer = bh->b_size /
2537 EXT3_INODE_SIZE(inode->i_sb);
2538 inode_offset = ((inode->i_ino - 1) %
2539 EXT3_INODES_PER_GROUP(inode->i_sb));
2540 start = inode_offset & ~(inodes_per_buffer - 1);
2542 /* Is the inode bitmap in cache? */
2543 desc = ext3_get_group_desc(inode->i_sb,
2544 block_group, NULL);
2545 if (!desc)
2546 goto make_io;
2548 bitmap_bh = sb_getblk(inode->i_sb,
2549 le32_to_cpu(desc->bg_inode_bitmap));
2550 if (!bitmap_bh)
2551 goto make_io;
2554 * If the inode bitmap isn't in cache then the
2555 * optimisation may end up performing two reads instead
2556 * of one, so skip it.
2558 if (!buffer_uptodate(bitmap_bh)) {
2559 brelse(bitmap_bh);
2560 goto make_io;
2562 for (i = start; i < start + inodes_per_buffer; i++) {
2563 if (i == inode_offset)
2564 continue;
2565 if (ext3_test_bit(i, bitmap_bh->b_data))
2566 break;
2568 brelse(bitmap_bh);
2569 if (i == start + inodes_per_buffer) {
2570 /* all other inodes are free, so skip I/O */
2571 memset(bh->b_data, 0, bh->b_size);
2572 set_buffer_uptodate(bh);
2573 unlock_buffer(bh);
2574 goto has_buffer;
2578 make_io:
2580 * There are other valid inodes in the buffer, this inode
2581 * has in-inode xattrs, or we don't have this inode in memory.
2582 * Read the block from disk.
2584 get_bh(bh);
2585 bh->b_end_io = end_buffer_read_sync;
2586 submit_bh(READ_META, bh);
2587 wait_on_buffer(bh);
2588 if (!buffer_uptodate(bh)) {
2589 ext3_error(inode->i_sb, "ext3_get_inode_loc",
2590 "unable to read inode block - "
2591 "inode=%lu, block="E3FSBLK,
2592 inode->i_ino, block);
2593 brelse(bh);
2594 return -EIO;
2597 has_buffer:
2598 iloc->bh = bh;
2599 return 0;
2602 int ext3_get_inode_loc(struct inode *inode, struct ext3_iloc *iloc)
2604 /* We have all inode data except xattrs in memory here. */
2605 return __ext3_get_inode_loc(inode, iloc,
2606 !(EXT3_I(inode)->i_state & EXT3_STATE_XATTR));
2609 void ext3_set_inode_flags(struct inode *inode)
2611 unsigned int flags = EXT3_I(inode)->i_flags;
2613 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
2614 if (flags & EXT3_SYNC_FL)
2615 inode->i_flags |= S_SYNC;
2616 if (flags & EXT3_APPEND_FL)
2617 inode->i_flags |= S_APPEND;
2618 if (flags & EXT3_IMMUTABLE_FL)
2619 inode->i_flags |= S_IMMUTABLE;
2620 if (flags & EXT3_NOATIME_FL)
2621 inode->i_flags |= S_NOATIME;
2622 if (flags & EXT3_DIRSYNC_FL)
2623 inode->i_flags |= S_DIRSYNC;
2626 void ext3_read_inode(struct inode * inode)
2628 struct ext3_iloc iloc;
2629 struct ext3_inode *raw_inode;
2630 struct ext3_inode_info *ei = EXT3_I(inode);
2631 struct buffer_head *bh;
2632 int block;
2634 #ifdef CONFIG_EXT3_FS_POSIX_ACL
2635 ei->i_acl = EXT3_ACL_NOT_CACHED;
2636 ei->i_default_acl = EXT3_ACL_NOT_CACHED;
2637 #endif
2638 ei->i_block_alloc_info = NULL;
2640 if (__ext3_get_inode_loc(inode, &iloc, 0))
2641 goto bad_inode;
2642 bh = iloc.bh;
2643 raw_inode = ext3_raw_inode(&iloc);
2644 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
2645 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
2646 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
2647 if(!(test_opt (inode->i_sb, NO_UID32))) {
2648 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
2649 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
2651 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
2652 inode->i_size = le32_to_cpu(raw_inode->i_size);
2653 inode->i_atime.tv_sec = le32_to_cpu(raw_inode->i_atime);
2654 inode->i_ctime.tv_sec = le32_to_cpu(raw_inode->i_ctime);
2655 inode->i_mtime.tv_sec = le32_to_cpu(raw_inode->i_mtime);
2656 inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = inode->i_mtime.tv_nsec = 0;
2658 ei->i_state = 0;
2659 ei->i_dir_start_lookup = 0;
2660 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
2661 /* We now have enough fields to check if the inode was active or not.
2662 * This is needed because nfsd might try to access dead inodes
2663 * the test is that same one that e2fsck uses
2664 * NeilBrown 1999oct15
2666 if (inode->i_nlink == 0) {
2667 if (inode->i_mode == 0 ||
2668 !(EXT3_SB(inode->i_sb)->s_mount_state & EXT3_ORPHAN_FS)) {
2669 /* this inode is deleted */
2670 brelse (bh);
2671 goto bad_inode;
2673 /* The only unlinked inodes we let through here have
2674 * valid i_mode and are being read by the orphan
2675 * recovery code: that's fine, we're about to complete
2676 * the process of deleting those. */
2678 inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
2679 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
2680 #ifdef EXT3_FRAGMENTS
2681 ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
2682 ei->i_frag_no = raw_inode->i_frag;
2683 ei->i_frag_size = raw_inode->i_fsize;
2684 #endif
2685 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
2686 if (!S_ISREG(inode->i_mode)) {
2687 ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
2688 } else {
2689 inode->i_size |=
2690 ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
2692 ei->i_disksize = inode->i_size;
2693 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
2694 ei->i_block_group = iloc.block_group;
2696 * NOTE! The in-memory inode i_data array is in little-endian order
2697 * even on big-endian machines: we do NOT byteswap the block numbers!
2699 for (block = 0; block < EXT3_N_BLOCKS; block++)
2700 ei->i_data[block] = raw_inode->i_block[block];
2701 INIT_LIST_HEAD(&ei->i_orphan);
2703 if (inode->i_ino >= EXT3_FIRST_INO(inode->i_sb) + 1 &&
2704 EXT3_INODE_SIZE(inode->i_sb) > EXT3_GOOD_OLD_INODE_SIZE) {
2706 * When mke2fs creates big inodes it does not zero out
2707 * the unused bytes above EXT3_GOOD_OLD_INODE_SIZE,
2708 * so ignore those first few inodes.
2710 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
2711 if (EXT3_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
2712 EXT3_INODE_SIZE(inode->i_sb))
2713 goto bad_inode;
2714 if (ei->i_extra_isize == 0) {
2715 /* The extra space is currently unused. Use it. */
2716 ei->i_extra_isize = sizeof(struct ext3_inode) -
2717 EXT3_GOOD_OLD_INODE_SIZE;
2718 } else {
2719 __le32 *magic = (void *)raw_inode +
2720 EXT3_GOOD_OLD_INODE_SIZE +
2721 ei->i_extra_isize;
2722 if (*magic == cpu_to_le32(EXT3_XATTR_MAGIC))
2723 ei->i_state |= EXT3_STATE_XATTR;
2725 } else
2726 ei->i_extra_isize = 0;
2728 if (S_ISREG(inode->i_mode)) {
2729 inode->i_op = &ext3_file_inode_operations;
2730 inode->i_fop = &ext3_file_operations;
2731 ext3_set_aops(inode);
2732 } else if (S_ISDIR(inode->i_mode)) {
2733 inode->i_op = &ext3_dir_inode_operations;
2734 inode->i_fop = &ext3_dir_operations;
2735 } else if (S_ISLNK(inode->i_mode)) {
2736 if (ext3_inode_is_fast_symlink(inode))
2737 inode->i_op = &ext3_fast_symlink_inode_operations;
2738 else {
2739 inode->i_op = &ext3_symlink_inode_operations;
2740 ext3_set_aops(inode);
2742 } else {
2743 inode->i_op = &ext3_special_inode_operations;
2744 if (raw_inode->i_block[0])
2745 init_special_inode(inode, inode->i_mode,
2746 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
2747 else
2748 init_special_inode(inode, inode->i_mode,
2749 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
2751 brelse (iloc.bh);
2752 ext3_set_inode_flags(inode);
2753 return;
2755 bad_inode:
2756 make_bad_inode(inode);
2757 return;
2761 * Post the struct inode info into an on-disk inode location in the
2762 * buffer-cache. This gobbles the caller's reference to the
2763 * buffer_head in the inode location struct.
2765 * The caller must have write access to iloc->bh.
2767 static int ext3_do_update_inode(handle_t *handle,
2768 struct inode *inode,
2769 struct ext3_iloc *iloc)
2771 struct ext3_inode *raw_inode = ext3_raw_inode(iloc);
2772 struct ext3_inode_info *ei = EXT3_I(inode);
2773 struct buffer_head *bh = iloc->bh;
2774 int err = 0, rc, block;
2776 /* For fields not not tracking in the in-memory inode,
2777 * initialise them to zero for new inodes. */
2778 if (ei->i_state & EXT3_STATE_NEW)
2779 memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
2781 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
2782 if(!(test_opt(inode->i_sb, NO_UID32))) {
2783 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
2784 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
2786 * Fix up interoperability with old kernels. Otherwise, old inodes get
2787 * re-used with the upper 16 bits of the uid/gid intact
2789 if(!ei->i_dtime) {
2790 raw_inode->i_uid_high =
2791 cpu_to_le16(high_16_bits(inode->i_uid));
2792 raw_inode->i_gid_high =
2793 cpu_to_le16(high_16_bits(inode->i_gid));
2794 } else {
2795 raw_inode->i_uid_high = 0;
2796 raw_inode->i_gid_high = 0;
2798 } else {
2799 raw_inode->i_uid_low =
2800 cpu_to_le16(fs_high2lowuid(inode->i_uid));
2801 raw_inode->i_gid_low =
2802 cpu_to_le16(fs_high2lowgid(inode->i_gid));
2803 raw_inode->i_uid_high = 0;
2804 raw_inode->i_gid_high = 0;
2806 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
2807 raw_inode->i_size = cpu_to_le32(ei->i_disksize);
2808 raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
2809 raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
2810 raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
2811 raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
2812 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
2813 raw_inode->i_flags = cpu_to_le32(ei->i_flags);
2814 #ifdef EXT3_FRAGMENTS
2815 raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
2816 raw_inode->i_frag = ei->i_frag_no;
2817 raw_inode->i_fsize = ei->i_frag_size;
2818 #endif
2819 raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
2820 if (!S_ISREG(inode->i_mode)) {
2821 raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
2822 } else {
2823 raw_inode->i_size_high =
2824 cpu_to_le32(ei->i_disksize >> 32);
2825 if (ei->i_disksize > 0x7fffffffULL) {
2826 struct super_block *sb = inode->i_sb;
2827 if (!EXT3_HAS_RO_COMPAT_FEATURE(sb,
2828 EXT3_FEATURE_RO_COMPAT_LARGE_FILE) ||
2829 EXT3_SB(sb)->s_es->s_rev_level ==
2830 cpu_to_le32(EXT3_GOOD_OLD_REV)) {
2831 /* If this is the first large file
2832 * created, add a flag to the superblock.
2834 err = ext3_journal_get_write_access(handle,
2835 EXT3_SB(sb)->s_sbh);
2836 if (err)
2837 goto out_brelse;
2838 ext3_update_dynamic_rev(sb);
2839 EXT3_SET_RO_COMPAT_FEATURE(sb,
2840 EXT3_FEATURE_RO_COMPAT_LARGE_FILE);
2841 sb->s_dirt = 1;
2842 handle->h_sync = 1;
2843 err = ext3_journal_dirty_metadata(handle,
2844 EXT3_SB(sb)->s_sbh);
2848 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
2849 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
2850 if (old_valid_dev(inode->i_rdev)) {
2851 raw_inode->i_block[0] =
2852 cpu_to_le32(old_encode_dev(inode->i_rdev));
2853 raw_inode->i_block[1] = 0;
2854 } else {
2855 raw_inode->i_block[0] = 0;
2856 raw_inode->i_block[1] =
2857 cpu_to_le32(new_encode_dev(inode->i_rdev));
2858 raw_inode->i_block[2] = 0;
2860 } else for (block = 0; block < EXT3_N_BLOCKS; block++)
2861 raw_inode->i_block[block] = ei->i_data[block];
2863 if (ei->i_extra_isize)
2864 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
2866 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
2867 rc = ext3_journal_dirty_metadata(handle, bh);
2868 if (!err)
2869 err = rc;
2870 ei->i_state &= ~EXT3_STATE_NEW;
2872 out_brelse:
2873 brelse (bh);
2874 ext3_std_error(inode->i_sb, err);
2875 return err;
2879 * ext3_write_inode()
2881 * We are called from a few places:
2883 * - Within generic_file_write() for O_SYNC files.
2884 * Here, there will be no transaction running. We wait for any running
2885 * trasnaction to commit.
2887 * - Within sys_sync(), kupdate and such.
2888 * We wait on commit, if tol to.
2890 * - Within prune_icache() (PF_MEMALLOC == true)
2891 * Here we simply return. We can't afford to block kswapd on the
2892 * journal commit.
2894 * In all cases it is actually safe for us to return without doing anything,
2895 * because the inode has been copied into a raw inode buffer in
2896 * ext3_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
2897 * knfsd.
2899 * Note that we are absolutely dependent upon all inode dirtiers doing the
2900 * right thing: they *must* call mark_inode_dirty() after dirtying info in
2901 * which we are interested.
2903 * It would be a bug for them to not do this. The code:
2905 * mark_inode_dirty(inode)
2906 * stuff();
2907 * inode->i_size = expr;
2909 * is in error because a kswapd-driven write_inode() could occur while
2910 * `stuff()' is running, and the new i_size will be lost. Plus the inode
2911 * will no longer be on the superblock's dirty inode list.
2913 int ext3_write_inode(struct inode *inode, int wait)
2915 if (current->flags & PF_MEMALLOC)
2916 return 0;
2918 if (ext3_journal_current_handle()) {
2919 jbd_debug(0, "called recursively, non-PF_MEMALLOC!\n");
2920 dump_stack();
2921 return -EIO;
2924 if (!wait)
2925 return 0;
2927 return ext3_force_commit(inode->i_sb);
2931 * ext3_setattr()
2933 * Called from notify_change.
2935 * We want to trap VFS attempts to truncate the file as soon as
2936 * possible. In particular, we want to make sure that when the VFS
2937 * shrinks i_size, we put the inode on the orphan list and modify
2938 * i_disksize immediately, so that during the subsequent flushing of
2939 * dirty pages and freeing of disk blocks, we can guarantee that any
2940 * commit will leave the blocks being flushed in an unused state on
2941 * disk. (On recovery, the inode will get truncated and the blocks will
2942 * be freed, so we have a strong guarantee that no future commit will
2943 * leave these blocks visible to the user.)
2945 * Called with inode->sem down.
2947 int ext3_setattr(struct dentry *dentry, struct iattr *attr)
2949 struct inode *inode = dentry->d_inode;
2950 int error, rc = 0;
2951 const unsigned int ia_valid = attr->ia_valid;
2953 error = inode_change_ok(inode, attr);
2954 if (error)
2955 return error;
2957 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
2958 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
2959 handle_t *handle;
2961 /* (user+group)*(old+new) structure, inode write (sb,
2962 * inode block, ? - but truncate inode update has it) */
2963 handle = ext3_journal_start(inode, 2*(EXT3_QUOTA_INIT_BLOCKS(inode->i_sb)+
2964 EXT3_QUOTA_DEL_BLOCKS(inode->i_sb))+3);
2965 if (IS_ERR(handle)) {
2966 error = PTR_ERR(handle);
2967 goto err_out;
2969 error = DQUOT_TRANSFER(inode, attr) ? -EDQUOT : 0;
2970 if (error) {
2971 ext3_journal_stop(handle);
2972 return error;
2974 /* Update corresponding info in inode so that everything is in
2975 * one transaction */
2976 if (attr->ia_valid & ATTR_UID)
2977 inode->i_uid = attr->ia_uid;
2978 if (attr->ia_valid & ATTR_GID)
2979 inode->i_gid = attr->ia_gid;
2980 error = ext3_mark_inode_dirty(handle, inode);
2981 ext3_journal_stop(handle);
2984 if (S_ISREG(inode->i_mode) &&
2985 attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
2986 handle_t *handle;
2988 handle = ext3_journal_start(inode, 3);
2989 if (IS_ERR(handle)) {
2990 error = PTR_ERR(handle);
2991 goto err_out;
2994 error = ext3_orphan_add(handle, inode);
2995 EXT3_I(inode)->i_disksize = attr->ia_size;
2996 rc = ext3_mark_inode_dirty(handle, inode);
2997 if (!error)
2998 error = rc;
2999 ext3_journal_stop(handle);
3002 rc = inode_setattr(inode, attr);
3004 /* If inode_setattr's call to ext3_truncate failed to get a
3005 * transaction handle at all, we need to clean up the in-core
3006 * orphan list manually. */
3007 if (inode->i_nlink)
3008 ext3_orphan_del(NULL, inode);
3010 if (!rc && (ia_valid & ATTR_MODE))
3011 rc = ext3_acl_chmod(inode);
3013 err_out:
3014 ext3_std_error(inode->i_sb, error);
3015 if (!error)
3016 error = rc;
3017 return error;
3022 * How many blocks doth make a writepage()?
3024 * With N blocks per page, it may be:
3025 * N data blocks
3026 * 2 indirect block
3027 * 2 dindirect
3028 * 1 tindirect
3029 * N+5 bitmap blocks (from the above)
3030 * N+5 group descriptor summary blocks
3031 * 1 inode block
3032 * 1 superblock.
3033 * 2 * EXT3_SINGLEDATA_TRANS_BLOCKS for the quote files
3035 * 3 * (N + 5) + 2 + 2 * EXT3_SINGLEDATA_TRANS_BLOCKS
3037 * With ordered or writeback data it's the same, less the N data blocks.
3039 * If the inode's direct blocks can hold an integral number of pages then a
3040 * page cannot straddle two indirect blocks, and we can only touch one indirect
3041 * and dindirect block, and the "5" above becomes "3".
3043 * This still overestimates under most circumstances. If we were to pass the
3044 * start and end offsets in here as well we could do block_to_path() on each
3045 * block and work out the exact number of indirects which are touched. Pah.
3048 static int ext3_writepage_trans_blocks(struct inode *inode)
3050 int bpp = ext3_journal_blocks_per_page(inode);
3051 int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
3052 int ret;
3054 if (ext3_should_journal_data(inode))
3055 ret = 3 * (bpp + indirects) + 2;
3056 else
3057 ret = 2 * (bpp + indirects) + 2;
3059 #ifdef CONFIG_QUOTA
3060 /* We know that structure was already allocated during DQUOT_INIT so
3061 * we will be updating only the data blocks + inodes */
3062 ret += 2*EXT3_QUOTA_TRANS_BLOCKS(inode->i_sb);
3063 #endif
3065 return ret;
3069 * The caller must have previously called ext3_reserve_inode_write().
3070 * Give this, we know that the caller already has write access to iloc->bh.
3072 int ext3_mark_iloc_dirty(handle_t *handle,
3073 struct inode *inode, struct ext3_iloc *iloc)
3075 int err = 0;
3077 /* the do_update_inode consumes one bh->b_count */
3078 get_bh(iloc->bh);
3080 /* ext3_do_update_inode() does journal_dirty_metadata */
3081 err = ext3_do_update_inode(handle, inode, iloc);
3082 put_bh(iloc->bh);
3083 return err;
3087 * On success, We end up with an outstanding reference count against
3088 * iloc->bh. This _must_ be cleaned up later.
3092 ext3_reserve_inode_write(handle_t *handle, struct inode *inode,
3093 struct ext3_iloc *iloc)
3095 int err = 0;
3096 if (handle) {
3097 err = ext3_get_inode_loc(inode, iloc);
3098 if (!err) {
3099 BUFFER_TRACE(iloc->bh, "get_write_access");
3100 err = ext3_journal_get_write_access(handle, iloc->bh);
3101 if (err) {
3102 brelse(iloc->bh);
3103 iloc->bh = NULL;
3107 ext3_std_error(inode->i_sb, err);
3108 return err;
3112 * What we do here is to mark the in-core inode as clean with respect to inode
3113 * dirtiness (it may still be data-dirty).
3114 * This means that the in-core inode may be reaped by prune_icache
3115 * without having to perform any I/O. This is a very good thing,
3116 * because *any* task may call prune_icache - even ones which
3117 * have a transaction open against a different journal.
3119 * Is this cheating? Not really. Sure, we haven't written the
3120 * inode out, but prune_icache isn't a user-visible syncing function.
3121 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
3122 * we start and wait on commits.
3124 * Is this efficient/effective? Well, we're being nice to the system
3125 * by cleaning up our inodes proactively so they can be reaped
3126 * without I/O. But we are potentially leaving up to five seconds'
3127 * worth of inodes floating about which prune_icache wants us to
3128 * write out. One way to fix that would be to get prune_icache()
3129 * to do a write_super() to free up some memory. It has the desired
3130 * effect.
3132 int ext3_mark_inode_dirty(handle_t *handle, struct inode *inode)
3134 struct ext3_iloc iloc;
3135 int err;
3137 might_sleep();
3138 err = ext3_reserve_inode_write(handle, inode, &iloc);
3139 if (!err)
3140 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
3141 return err;
3145 * ext3_dirty_inode() is called from __mark_inode_dirty()
3147 * We're really interested in the case where a file is being extended.
3148 * i_size has been changed by generic_commit_write() and we thus need
3149 * to include the updated inode in the current transaction.
3151 * Also, DQUOT_ALLOC_SPACE() will always dirty the inode when blocks
3152 * are allocated to the file.
3154 * If the inode is marked synchronous, we don't honour that here - doing
3155 * so would cause a commit on atime updates, which we don't bother doing.
3156 * We handle synchronous inodes at the highest possible level.
3158 void ext3_dirty_inode(struct inode *inode)
3160 handle_t *current_handle = ext3_journal_current_handle();
3161 handle_t *handle;
3163 handle = ext3_journal_start(inode, 2);
3164 if (IS_ERR(handle))
3165 goto out;
3166 if (current_handle &&
3167 current_handle->h_transaction != handle->h_transaction) {
3168 /* This task has a transaction open against a different fs */
3169 printk(KERN_EMERG "%s: transactions do not match!\n",
3170 __FUNCTION__);
3171 } else {
3172 jbd_debug(5, "marking dirty. outer handle=%p\n",
3173 current_handle);
3174 ext3_mark_inode_dirty(handle, inode);
3176 ext3_journal_stop(handle);
3177 out:
3178 return;
3181 #if 0
3183 * Bind an inode's backing buffer_head into this transaction, to prevent
3184 * it from being flushed to disk early. Unlike
3185 * ext3_reserve_inode_write, this leaves behind no bh reference and
3186 * returns no iloc structure, so the caller needs to repeat the iloc
3187 * lookup to mark the inode dirty later.
3189 static int ext3_pin_inode(handle_t *handle, struct inode *inode)
3191 struct ext3_iloc iloc;
3193 int err = 0;
3194 if (handle) {
3195 err = ext3_get_inode_loc(inode, &iloc);
3196 if (!err) {
3197 BUFFER_TRACE(iloc.bh, "get_write_access");
3198 err = journal_get_write_access(handle, iloc.bh);
3199 if (!err)
3200 err = ext3_journal_dirty_metadata(handle,
3201 iloc.bh);
3202 brelse(iloc.bh);
3205 ext3_std_error(inode->i_sb, err);
3206 return err;
3208 #endif
3210 int ext3_change_inode_journal_flag(struct inode *inode, int val)
3212 journal_t *journal;
3213 handle_t *handle;
3214 int err;
3217 * We have to be very careful here: changing a data block's
3218 * journaling status dynamically is dangerous. If we write a
3219 * data block to the journal, change the status and then delete
3220 * that block, we risk forgetting to revoke the old log record
3221 * from the journal and so a subsequent replay can corrupt data.
3222 * So, first we make sure that the journal is empty and that
3223 * nobody is changing anything.
3226 journal = EXT3_JOURNAL(inode);
3227 if (is_journal_aborted(journal) || IS_RDONLY(inode))
3228 return -EROFS;
3230 journal_lock_updates(journal);
3231 journal_flush(journal);
3234 * OK, there are no updates running now, and all cached data is
3235 * synced to disk. We are now in a completely consistent state
3236 * which doesn't have anything in the journal, and we know that
3237 * no filesystem updates are running, so it is safe to modify
3238 * the inode's in-core data-journaling state flag now.
3241 if (val)
3242 EXT3_I(inode)->i_flags |= EXT3_JOURNAL_DATA_FL;
3243 else
3244 EXT3_I(inode)->i_flags &= ~EXT3_JOURNAL_DATA_FL;
3245 ext3_set_aops(inode);
3247 journal_unlock_updates(journal);
3249 /* Finally we can mark the inode as dirty. */
3251 handle = ext3_journal_start(inode, 1);
3252 if (IS_ERR(handle))
3253 return PTR_ERR(handle);
3255 err = ext3_mark_inode_dirty(handle, inode);
3256 handle->h_sync = 1;
3257 ext3_journal_stop(handle);
3258 ext3_std_error(inode->i_sb, err);
3260 return err;