2 * linux/fs/ext2/inode.c
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
11 * linux/fs/minix/inode.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
15 * Goal-directed block allocation by Stephen Tweedie
16 * (sct@dcs.ed.ac.uk), 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 ext2_get_block() by Al Viro, 2000
26 #include <linux/ext2_fs.h>
27 #include <linux/locks.h>
28 #include <linux/smp_lock.h>
29 #include <linux/sched.h>
30 #include <linux/highuid.h>
31 #include <linux/quotaops.h>
32 #include <linux/module.h>
34 MODULE_AUTHOR("Remy Card and others");
35 MODULE_DESCRIPTION("Second Extended Filesystem");
36 MODULE_LICENSE("GPL");
39 static int ext2_update_inode(struct inode * inode, int do_sync);
42 * Called at each iput()
44 void ext2_put_inode (struct inode * inode)
46 ext2_discard_prealloc (inode);
50 * Called at the last iput() if i_nlink is zero.
52 void ext2_delete_inode (struct inode * inode)
56 if (is_bad_inode(inode) ||
57 inode->i_ino == EXT2_ACL_IDX_INO ||
58 inode->i_ino == EXT2_ACL_DATA_INO)
60 inode->u.ext2_i.i_dtime = CURRENT_TIME;
61 mark_inode_dirty(inode);
62 ext2_update_inode(inode, IS_SYNC(inode));
65 ext2_truncate (inode);
66 ext2_free_inode (inode);
72 clear_inode(inode); /* We must guarantee clearing of inode... */
75 void ext2_discard_prealloc (struct inode * inode)
77 #ifdef EXT2_PREALLOCATE
79 /* Writer: ->i_prealloc* */
80 if (inode->u.ext2_i.i_prealloc_count) {
81 unsigned short total = inode->u.ext2_i.i_prealloc_count;
82 unsigned long block = inode->u.ext2_i.i_prealloc_block;
83 inode->u.ext2_i.i_prealloc_count = 0;
84 inode->u.ext2_i.i_prealloc_block = 0;
86 ext2_free_blocks (inode, block, total);
92 static int ext2_alloc_block (struct inode * inode, unsigned long goal, int *err)
95 static unsigned long alloc_hits = 0, alloc_attempts = 0;
100 #ifdef EXT2_PREALLOCATE
101 /* Writer: ->i_prealloc* */
102 if (inode->u.ext2_i.i_prealloc_count &&
103 (goal == inode->u.ext2_i.i_prealloc_block ||
104 goal + 1 == inode->u.ext2_i.i_prealloc_block))
106 result = inode->u.ext2_i.i_prealloc_block++;
107 inode->u.ext2_i.i_prealloc_count--;
109 ext2_debug ("preallocation hit (%lu/%lu).\n",
110 ++alloc_hits, ++alloc_attempts);
112 ext2_discard_prealloc (inode);
113 ext2_debug ("preallocation miss (%lu/%lu).\n",
114 alloc_hits, ++alloc_attempts);
115 if (S_ISREG(inode->i_mode))
116 result = ext2_new_block (inode, goal,
117 &inode->u.ext2_i.i_prealloc_count,
118 &inode->u.ext2_i.i_prealloc_block, err);
120 result = ext2_new_block (inode, goal, 0, 0, err);
123 result = ext2_new_block (inode, goal, 0, 0, err);
131 struct buffer_head *bh;
134 static inline void add_chain(Indirect *p, struct buffer_head *bh, u32 *v)
136 p->key = *(p->p = v);
140 static inline int verify_chain(Indirect *from, Indirect *to)
142 while (from <= to && from->key == *from->p)
148 * ext2_block_to_path - parse the block number into array of offsets
149 * @inode: inode in question (we are only interested in its superblock)
150 * @i_block: block number to be parsed
151 * @offsets: array to store the offsets in
153 * To store the locations of file's data ext2 uses a data structure common
154 * for UNIX filesystems - tree of pointers anchored in the inode, with
155 * data blocks at leaves and indirect blocks in intermediate nodes.
156 * This function translates the block number into path in that tree -
157 * return value is the path length and @offsets[n] is the offset of
158 * pointer to (n+1)th node in the nth one. If @block is out of range
159 * (negative or too large) warning is printed and zero returned.
161 * Note: function doesn't find node addresses, so no IO is needed. All
162 * we need to know is the capacity of indirect blocks (taken from the
167 * Portability note: the last comparison (check that we fit into triple
168 * indirect block) is spelled differently, because otherwise on an
169 * architecture with 32-bit longs and 8Kb pages we might get into trouble
170 * if our filesystem had 8Kb blocks. We might use long long, but that would
171 * kill us on x86. Oh, well, at least the sign propagation does not matter -
172 * i_block would have to be negative in the very beginning, so we would not
176 static int ext2_block_to_path(struct inode *inode, long i_block, int offsets[4])
178 int ptrs = EXT2_ADDR_PER_BLOCK(inode->i_sb);
179 int ptrs_bits = EXT2_ADDR_PER_BLOCK_BITS(inode->i_sb);
180 const long direct_blocks = EXT2_NDIR_BLOCKS,
181 indirect_blocks = ptrs,
182 double_blocks = (1 << (ptrs_bits * 2));
186 ext2_warning (inode->i_sb, "ext2_block_to_path", "block < 0");
187 } else if (i_block < direct_blocks) {
188 offsets[n++] = i_block;
189 } else if ( (i_block -= direct_blocks) < indirect_blocks) {
190 offsets[n++] = EXT2_IND_BLOCK;
191 offsets[n++] = i_block;
192 } else if ((i_block -= indirect_blocks) < double_blocks) {
193 offsets[n++] = EXT2_DIND_BLOCK;
194 offsets[n++] = i_block >> ptrs_bits;
195 offsets[n++] = i_block & (ptrs - 1);
196 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
197 offsets[n++] = EXT2_TIND_BLOCK;
198 offsets[n++] = i_block >> (ptrs_bits * 2);
199 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
200 offsets[n++] = i_block & (ptrs - 1);
202 ext2_warning (inode->i_sb, "ext2_block_to_path", "block > big");
208 * ext2_get_branch - read the chain of indirect blocks leading to data
209 * @inode: inode in question
210 * @depth: depth of the chain (1 - direct pointer, etc.)
211 * @offsets: offsets of pointers in inode/indirect blocks
212 * @chain: place to store the result
213 * @err: here we store the error value
215 * Function fills the array of triples <key, p, bh> and returns %NULL
216 * if everything went OK or the pointer to the last filled triple
217 * (incomplete one) otherwise. Upon the return chain[i].key contains
218 * the number of (i+1)-th block in the chain (as it is stored in memory,
219 * i.e. little-endian 32-bit), chain[i].p contains the address of that
220 * number (it points into struct inode for i==0 and into the bh->b_data
221 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
222 * block for i>0 and NULL for i==0. In other words, it holds the block
223 * numbers of the chain, addresses they were taken from (and where we can
224 * verify that chain did not change) and buffer_heads hosting these
227 * Function stops when it stumbles upon zero pointer (absent block)
228 * (pointer to last triple returned, *@err == 0)
229 * or when it gets an IO error reading an indirect block
230 * (ditto, *@err == -EIO)
231 * or when it notices that chain had been changed while it was reading
232 * (ditto, *@err == -EAGAIN)
233 * or when it reads all @depth-1 indirect blocks successfully and finds
234 * the whole chain, all way to the data (returns %NULL, *err == 0).
236 static Indirect *ext2_get_branch(struct inode *inode,
242 struct super_block *sb = inode->i_sb;
244 struct buffer_head *bh;
247 /* i_data is not going away, no lock needed */
248 add_chain (chain, NULL, inode->u.ext2_i.i_data + *offsets);
252 bh = sb_bread(sb, le32_to_cpu(p->key));
255 /* Reader: pointers */
256 if (!verify_chain(chain, p))
258 add_chain(++p, bh, (u32*)bh->b_data + *++offsets);
275 * ext2_find_near - find a place for allocation with sufficient locality
277 * @ind: descriptor of indirect block.
279 * This function returns the prefered place for block allocation.
280 * It is used when heuristic for sequential allocation fails.
282 * + if there is a block to the left of our position - allocate near it.
283 * + if pointer will live in indirect block - allocate near that block.
284 * + if pointer will live in inode - allocate in the same cylinder group.
285 * Caller must make sure that @ind is valid and will stay that way.
288 static inline unsigned long ext2_find_near(struct inode *inode, Indirect *ind)
290 u32 *start = ind->bh ? (u32*) ind->bh->b_data : inode->u.ext2_i.i_data;
293 /* Try to find previous block */
294 for (p = ind->p - 1; p >= start; p--)
296 return le32_to_cpu(*p);
298 /* No such thing, so let's try location of indirect block */
300 return ind->bh->b_blocknr;
303 * It is going to be refered from inode itself? OK, just put it into
304 * the same cylinder group then.
306 return (inode->u.ext2_i.i_block_group *
307 EXT2_BLOCKS_PER_GROUP(inode->i_sb)) +
308 le32_to_cpu(inode->i_sb->u.ext2_sb.s_es->s_first_data_block);
312 * ext2_find_goal - find a prefered place for allocation.
314 * @block: block we want
315 * @chain: chain of indirect blocks
316 * @partial: pointer to the last triple within a chain
317 * @goal: place to store the result.
319 * Normally this function find the prefered place for block allocation,
320 * stores it in *@goal and returns zero. If the branch had been changed
321 * under us we return -EAGAIN.
324 static inline int ext2_find_goal(struct inode *inode,
330 /* Writer: ->i_next_alloc* */
331 if (block == inode->u.ext2_i.i_next_alloc_block + 1) {
332 inode->u.ext2_i.i_next_alloc_block++;
333 inode->u.ext2_i.i_next_alloc_goal++;
336 /* Reader: pointers, ->i_next_alloc* */
337 if (verify_chain(chain, partial)) {
339 * try the heuristic for sequential allocation,
340 * failing that at least try to get decent locality.
342 if (block == inode->u.ext2_i.i_next_alloc_block)
343 *goal = inode->u.ext2_i.i_next_alloc_goal;
345 *goal = ext2_find_near(inode, partial);
353 * ext2_alloc_branch - allocate and set up a chain of blocks.
355 * @num: depth of the chain (number of blocks to allocate)
356 * @offsets: offsets (in the blocks) to store the pointers to next.
357 * @branch: place to store the chain in.
359 * This function allocates @num blocks, zeroes out all but the last one,
360 * links them into chain and (if we are synchronous) writes them to disk.
361 * In other words, it prepares a branch that can be spliced onto the
362 * inode. It stores the information about that chain in the branch[], in
363 * the same format as ext2_get_branch() would do. We are calling it after
364 * we had read the existing part of chain and partial points to the last
365 * triple of that (one with zero ->key). Upon the exit we have the same
366 * picture as after the successful ext2_get_block(), excpet that in one
367 * place chain is disconnected - *branch->p is still zero (we did not
368 * set the last link), but branch->key contains the number that should
369 * be placed into *branch->p to fill that gap.
371 * If allocation fails we free all blocks we've allocated (and forget
372 * their buffer_heads) and return the error value the from failed
373 * ext2_alloc_block() (normally -ENOSPC). Otherwise we set the chain
374 * as described above and return 0.
377 static int ext2_alloc_branch(struct inode *inode,
383 int blocksize = inode->i_sb->s_blocksize;
387 int parent = ext2_alloc_block(inode, goal, &err);
389 branch[0].key = cpu_to_le32(parent);
390 if (parent) for (n = 1; n < num; n++) {
391 struct buffer_head *bh;
392 /* Allocate the next block */
393 int nr = ext2_alloc_block(inode, parent, &err);
396 branch[n].key = cpu_to_le32(nr);
398 * Get buffer_head for parent block, zero it out and set
399 * the pointer to new one, then send parent to disk.
401 bh = sb_getblk(inode->i_sb, parent);
403 memset(bh->b_data, 0, blocksize);
405 branch[n].p = (u32*) bh->b_data + offsets[n];
406 *branch[n].p = branch[n].key;
407 mark_buffer_uptodate(bh, 1);
409 mark_buffer_dirty_inode(bh, inode);
410 /* We used to sync bh here if IS_SYNC(inode).
411 * But for S_ISREG files we now rely upon generic_osync_inode()
412 * and b_inode_buffers
414 if (S_ISDIR(inode->i_mode) && IS_SYNC(inode)) {
415 ll_rw_block (WRITE, 1, &bh);
423 /* Allocation failed, free what we already allocated */
424 for (i = 1; i < n; i++)
425 bforget(branch[i].bh);
426 for (i = 0; i < n; i++)
427 ext2_free_blocks(inode, le32_to_cpu(branch[i].key), 1);
432 * ext2_splice_branch - splice the allocated branch onto inode.
434 * @block: (logical) number of block we are adding
435 * @chain: chain of indirect blocks (with a missing link - see
437 * @where: location of missing link
438 * @num: number of blocks we are adding
440 * This function verifies that chain (up to the missing link) had not
441 * changed, fills the missing link and does all housekeeping needed in
442 * inode (->i_blocks, etc.). In case of success we end up with the full
443 * chain to new block and return 0. Otherwise (== chain had been changed)
444 * we free the new blocks (forgetting their buffer_heads, indeed) and
448 static inline int ext2_splice_branch(struct inode *inode,
456 /* Verify that place we are splicing to is still there and vacant */
458 /* Writer: pointers, ->i_next_alloc* */
459 if (!verify_chain(chain, where-1) || *where->p)
465 *where->p = where->key;
466 inode->u.ext2_i.i_next_alloc_block = block;
467 inode->u.ext2_i.i_next_alloc_goal = le32_to_cpu(where[num-1].key);
471 /* We are done with atomic stuff, now do the rest of housekeeping */
473 inode->i_ctime = CURRENT_TIME;
475 /* had we spliced it onto indirect block? */
477 mark_buffer_dirty_inode(where->bh, inode);
478 if (S_ISDIR(inode->i_mode) && IS_SYNC(inode)) {
479 ll_rw_block(WRITE, 1, &where->bh);
480 wait_on_buffer(where->bh);
484 mark_inode_dirty(inode);
488 for (i = 1; i < num; i++)
489 bforget(where[i].bh);
490 for (i = 0; i < num; i++)
491 ext2_free_blocks(inode, le32_to_cpu(where[i].key), 1);
496 * Allocation strategy is simple: if we have to allocate something, we will
497 * have to go the whole way to leaf. So let's do it before attaching anything
498 * to tree, set linkage between the newborn blocks, write them if sync is
499 * required, recheck the path, free and repeat if check fails, otherwise
500 * set the last missing link (that will protect us from any truncate-generated
501 * removals - all blocks on the path are immune now) and possibly force the
502 * write on the parent block.
503 * That has a nice additional property: no special recovery from the failed
504 * allocations is needed - we simply release blocks and do not touch anything
505 * reachable from inode.
508 static int ext2_get_block(struct inode *inode, long iblock, struct buffer_head *bh_result, int create)
516 int depth = ext2_block_to_path(inode, iblock, offsets);
523 partial = ext2_get_branch(inode, depth, offsets, chain, &err);
525 /* Simplest case - block found, no allocation needed */
528 bh_result->b_dev = inode->i_dev;
529 bh_result->b_blocknr = le32_to_cpu(chain[depth-1].key);
530 bh_result->b_state |= (1UL << BH_Mapped);
531 /* Clean up and exit */
532 partial = chain+depth-1; /* the whole chain */
536 /* Next simple case - plain lookup or failed read of indirect block */
537 if (!create || err == -EIO) {
539 while (partial > chain) {
549 * Indirect block might be removed by truncate while we were
550 * reading it. Handling of that case (forget what we've got and
551 * reread) is taken out of the main path.
556 if (ext2_find_goal(inode, iblock, chain, partial, &goal) < 0)
559 left = (chain + depth) - partial;
560 err = ext2_alloc_branch(inode, left, goal,
561 offsets+(partial-chain), partial);
565 if (ext2_splice_branch(inode, iblock, chain, partial, left) < 0)
568 bh_result->b_state |= (1UL << BH_New);
572 while (partial > chain) {
579 static int ext2_writepage(struct page *page)
581 return block_write_full_page(page,ext2_get_block);
583 static int ext2_readpage(struct file *file, struct page *page)
585 return block_read_full_page(page,ext2_get_block);
587 static int ext2_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
589 return block_prepare_write(page,from,to,ext2_get_block);
591 static int ext2_bmap(struct address_space *mapping, long block)
593 return generic_block_bmap(mapping,block,ext2_get_block);
595 static int ext2_direct_IO(int rw, struct inode * inode, struct kiobuf * iobuf, unsigned long blocknr, int blocksize)
597 return generic_direct_IO(rw, inode, iobuf, blocknr, blocksize, ext2_get_block);
599 struct address_space_operations ext2_aops = {
600 readpage: ext2_readpage,
601 writepage: ext2_writepage,
602 sync_page: block_sync_page,
603 prepare_write: ext2_prepare_write,
604 commit_write: generic_commit_write,
606 direct_IO: ext2_direct_IO,
610 * Probably it should be a library function... search for first non-zero word
611 * or memcmp with zero_page, whatever is better for particular architecture.
614 static inline int all_zeroes(u32 *p, u32 *q)
623 * ext2_find_shared - find the indirect blocks for partial truncation.
624 * @inode: inode in question
625 * @depth: depth of the affected branch
626 * @offsets: offsets of pointers in that branch (see ext2_block_to_path)
627 * @chain: place to store the pointers to partial indirect blocks
628 * @top: place to the (detached) top of branch
630 * This is a helper function used by ext2_truncate().
632 * When we do truncate() we may have to clean the ends of several indirect
633 * blocks but leave the blocks themselves alive. Block is partially
634 * truncated if some data below the new i_size is refered from it (and
635 * it is on the path to the first completely truncated data block, indeed).
636 * We have to free the top of that path along with everything to the right
637 * of the path. Since no allocation past the truncation point is possible
638 * until ext2_truncate() finishes, we may safely do the latter, but top
639 * of branch may require special attention - pageout below the truncation
640 * point might try to populate it.
642 * We atomically detach the top of branch from the tree, store the block
643 * number of its root in *@top, pointers to buffer_heads of partially
644 * truncated blocks - in @chain[].bh and pointers to their last elements
645 * that should not be removed - in @chain[].p. Return value is the pointer
646 * to last filled element of @chain.
648 * The work left to caller to do the actual freeing of subtrees:
649 * a) free the subtree starting from *@top
650 * b) free the subtrees whose roots are stored in
651 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
652 * c) free the subtrees growing from the inode past the @chain[0].p
653 * (no partially truncated stuff there).
656 static Indirect *ext2_find_shared(struct inode *inode,
662 Indirect *partial, *p;
666 for (k = depth; k > 1 && !offsets[k-1]; k--)
668 partial = ext2_get_branch(inode, k, offsets, chain, &err);
669 /* Writer: pointers */
671 partial = chain + k-1;
673 * If the branch acquired continuation since we've looked at it -
674 * fine, it should all survive and (new) top doesn't belong to us.
676 if (!partial->key && *partial->p)
679 for (p=partial; p>chain && all_zeroes((u32*)p->bh->b_data,p->p); p--)
682 * OK, we've found the last block that must survive. The rest of our
683 * branch should be detached before unlocking. However, if that rest
684 * of branch is all ours and does not grow immediately from the inode
685 * it's easier to cheat and just decrement partial->p.
687 if (p == chain + k - 1 && p > chain) {
705 * ext2_free_data - free a list of data blocks
706 * @inode: inode we are dealing with
707 * @p: array of block numbers
708 * @q: points immediately past the end of array
710 * We are freeing all blocks refered from that array (numbers are
711 * stored as little-endian 32-bit) and updating @inode->i_blocks
714 static inline void ext2_free_data(struct inode *inode, u32 *p, u32 *q)
716 unsigned long block_to_free = 0, count = 0;
719 for ( ; p < q ; p++) {
720 nr = le32_to_cpu(*p);
723 /* accumulate blocks to free if they're contiguous */
726 else if (block_to_free == nr - count)
729 mark_inode_dirty(inode);
730 ext2_free_blocks (inode, block_to_free, count);
738 mark_inode_dirty(inode);
739 ext2_free_blocks (inode, block_to_free, count);
744 * ext2_free_branches - free an array of branches
745 * @inode: inode we are dealing with
746 * @p: array of block numbers
747 * @q: pointer immediately past the end of array
748 * @depth: depth of the branches to free
750 * We are freeing all blocks refered from these branches (numbers are
751 * stored as little-endian 32-bit) and updating @inode->i_blocks
754 static void ext2_free_branches(struct inode *inode, u32 *p, u32 *q, int depth)
756 struct buffer_head * bh;
760 int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
761 for ( ; p < q ; p++) {
762 nr = le32_to_cpu(*p);
766 bh = sb_bread(inode->i_sb, nr);
768 * A read failure? Report error and clear slot
772 ext2_error(inode->i_sb, "ext2_free_branches",
773 "Read failure, inode=%ld, block=%ld",
777 ext2_free_branches(inode,
779 (u32*)bh->b_data + addr_per_block,
782 ext2_free_blocks(inode, nr, 1);
783 mark_inode_dirty(inode);
786 ext2_free_data(inode, p, q);
789 void ext2_truncate (struct inode * inode)
791 u32 *i_data = inode->u.ext2_i.i_data;
792 int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
801 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
802 S_ISLNK(inode->i_mode)))
804 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
807 ext2_discard_prealloc(inode);
809 blocksize = inode->i_sb->s_blocksize;
810 iblock = (inode->i_size + blocksize-1)
811 >> EXT2_BLOCK_SIZE_BITS(inode->i_sb);
813 block_truncate_page(inode->i_mapping, inode->i_size, ext2_get_block);
815 n = ext2_block_to_path(inode, iblock, offsets);
820 ext2_free_data(inode, i_data+offsets[0],
821 i_data + EXT2_NDIR_BLOCKS);
825 partial = ext2_find_shared(inode, n, offsets, chain, &nr);
826 /* Kill the top of shared branch (already detached) */
828 if (partial == chain)
829 mark_inode_dirty(inode);
831 mark_buffer_dirty_inode(partial->bh, inode);
832 ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
834 /* Clear the ends of indirect blocks on the shared branch */
835 while (partial > chain) {
836 ext2_free_branches(inode,
838 (u32*)partial->bh->b_data + addr_per_block,
839 (chain+n-1) - partial);
840 mark_buffer_dirty_inode(partial->bh, inode);
841 brelse (partial->bh);
845 /* Kill the remaining (whole) subtrees */
846 switch (offsets[0]) {
848 nr = i_data[EXT2_IND_BLOCK];
850 i_data[EXT2_IND_BLOCK] = 0;
851 mark_inode_dirty(inode);
852 ext2_free_branches(inode, &nr, &nr+1, 1);
855 nr = i_data[EXT2_DIND_BLOCK];
857 i_data[EXT2_DIND_BLOCK] = 0;
858 mark_inode_dirty(inode);
859 ext2_free_branches(inode, &nr, &nr+1, 2);
861 case EXT2_DIND_BLOCK:
862 nr = i_data[EXT2_TIND_BLOCK];
864 i_data[EXT2_TIND_BLOCK] = 0;
865 mark_inode_dirty(inode);
866 ext2_free_branches(inode, &nr, &nr+1, 3);
868 case EXT2_TIND_BLOCK:
871 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
872 if (IS_SYNC(inode)) {
873 fsync_inode_buffers(inode);
874 ext2_sync_inode (inode);
876 mark_inode_dirty(inode);
880 void ext2_read_inode (struct inode * inode)
882 struct buffer_head * bh;
883 struct ext2_inode * raw_inode;
884 unsigned long block_group;
885 unsigned long group_desc;
888 unsigned long offset;
889 struct ext2_group_desc * gdp;
891 if ((inode->i_ino != EXT2_ROOT_INO && inode->i_ino != EXT2_ACL_IDX_INO &&
892 inode->i_ino != EXT2_ACL_DATA_INO &&
893 inode->i_ino < EXT2_FIRST_INO(inode->i_sb)) ||
894 inode->i_ino > le32_to_cpu(inode->i_sb->u.ext2_sb.s_es->s_inodes_count)) {
895 ext2_error (inode->i_sb, "ext2_read_inode",
896 "bad inode number: %lu", inode->i_ino);
899 block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
900 if (block_group >= inode->i_sb->u.ext2_sb.s_groups_count) {
901 ext2_error (inode->i_sb, "ext2_read_inode",
902 "group >= groups count");
905 group_desc = block_group >> EXT2_DESC_PER_BLOCK_BITS(inode->i_sb);
906 desc = block_group & (EXT2_DESC_PER_BLOCK(inode->i_sb) - 1);
907 bh = inode->i_sb->u.ext2_sb.s_group_desc[group_desc];
909 ext2_error (inode->i_sb, "ext2_read_inode",
910 "Descriptor not loaded");
914 gdp = (struct ext2_group_desc *) bh->b_data;
916 * Figure out the offset within the block group inode table
918 offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) *
919 EXT2_INODE_SIZE(inode->i_sb);
920 block = le32_to_cpu(gdp[desc].bg_inode_table) +
921 (offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb));
922 if (!(bh = sb_bread(inode->i_sb, block))) {
923 ext2_error (inode->i_sb, "ext2_read_inode",
924 "unable to read inode block - "
925 "inode=%lu, block=%lu", inode->i_ino, block);
928 offset &= (EXT2_BLOCK_SIZE(inode->i_sb) - 1);
929 raw_inode = (struct ext2_inode *) (bh->b_data + offset);
931 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
932 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
933 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
934 if(!(test_opt (inode->i_sb, NO_UID32))) {
935 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
936 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
938 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
939 inode->i_size = le32_to_cpu(raw_inode->i_size);
940 inode->i_atime = le32_to_cpu(raw_inode->i_atime);
941 inode->i_ctime = le32_to_cpu(raw_inode->i_ctime);
942 inode->i_mtime = le32_to_cpu(raw_inode->i_mtime);
943 inode->u.ext2_i.i_dtime = le32_to_cpu(raw_inode->i_dtime);
944 /* We now have enough fields to check if the inode was active or not.
945 * This is needed because nfsd might try to access dead inodes
946 * the test is that same one that e2fsck uses
947 * NeilBrown 1999oct15
949 if (inode->i_nlink == 0 && (inode->i_mode == 0 || inode->u.ext2_i.i_dtime)) {
950 /* this inode is deleted */
954 inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat), not the fs block size */
955 inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
956 inode->i_version = ++event;
957 inode->u.ext2_i.i_flags = le32_to_cpu(raw_inode->i_flags);
958 inode->u.ext2_i.i_faddr = le32_to_cpu(raw_inode->i_faddr);
959 inode->u.ext2_i.i_frag_no = raw_inode->i_frag;
960 inode->u.ext2_i.i_frag_size = raw_inode->i_fsize;
961 inode->u.ext2_i.i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
962 if (S_ISREG(inode->i_mode))
963 inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
965 inode->u.ext2_i.i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
966 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
967 inode->u.ext2_i.i_prealloc_count = 0;
968 inode->u.ext2_i.i_block_group = block_group;
971 * NOTE! The in-memory inode i_data array is in little-endian order
972 * even on big-endian machines: we do NOT byteswap the block numbers!
974 for (block = 0; block < EXT2_N_BLOCKS; block++)
975 inode->u.ext2_i.i_data[block] = raw_inode->i_block[block];
977 if (inode->i_ino == EXT2_ACL_IDX_INO ||
978 inode->i_ino == EXT2_ACL_DATA_INO)
979 /* Nothing to do */ ;
980 else if (S_ISREG(inode->i_mode)) {
981 inode->i_op = &ext2_file_inode_operations;
982 inode->i_fop = &ext2_file_operations;
983 inode->i_mapping->a_ops = &ext2_aops;
984 } else if (S_ISDIR(inode->i_mode)) {
985 inode->i_op = &ext2_dir_inode_operations;
986 inode->i_fop = &ext2_dir_operations;
987 inode->i_mapping->a_ops = &ext2_aops;
988 } else if (S_ISLNK(inode->i_mode)) {
989 if (!inode->i_blocks)
990 inode->i_op = &ext2_fast_symlink_inode_operations;
992 inode->i_op = &page_symlink_inode_operations;
993 inode->i_mapping->a_ops = &ext2_aops;
996 init_special_inode(inode, inode->i_mode,
997 le32_to_cpu(raw_inode->i_block[0]));
999 inode->i_attr_flags = 0;
1000 if (inode->u.ext2_i.i_flags & EXT2_SYNC_FL) {
1001 inode->i_attr_flags |= ATTR_FLAG_SYNCRONOUS;
1002 inode->i_flags |= S_SYNC;
1004 if (inode->u.ext2_i.i_flags & EXT2_APPEND_FL) {
1005 inode->i_attr_flags |= ATTR_FLAG_APPEND;
1006 inode->i_flags |= S_APPEND;
1008 if (inode->u.ext2_i.i_flags & EXT2_IMMUTABLE_FL) {
1009 inode->i_attr_flags |= ATTR_FLAG_IMMUTABLE;
1010 inode->i_flags |= S_IMMUTABLE;
1012 if (inode->u.ext2_i.i_flags & EXT2_NOATIME_FL) {
1013 inode->i_attr_flags |= ATTR_FLAG_NOATIME;
1014 inode->i_flags |= S_NOATIME;
1019 make_bad_inode(inode);
1023 static int ext2_update_inode(struct inode * inode, int do_sync)
1025 struct buffer_head * bh;
1026 struct ext2_inode * raw_inode;
1027 unsigned long block_group;
1028 unsigned long group_desc;
1030 unsigned long block;
1031 unsigned long offset;
1033 struct ext2_group_desc * gdp;
1035 if ((inode->i_ino != EXT2_ROOT_INO &&
1036 inode->i_ino < EXT2_FIRST_INO(inode->i_sb)) ||
1037 inode->i_ino > le32_to_cpu(inode->i_sb->u.ext2_sb.s_es->s_inodes_count)) {
1038 ext2_error (inode->i_sb, "ext2_write_inode",
1039 "bad inode number: %lu", inode->i_ino);
1042 block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
1043 if (block_group >= inode->i_sb->u.ext2_sb.s_groups_count) {
1044 ext2_error (inode->i_sb, "ext2_write_inode",
1045 "group >= groups count");
1048 group_desc = block_group >> EXT2_DESC_PER_BLOCK_BITS(inode->i_sb);
1049 desc = block_group & (EXT2_DESC_PER_BLOCK(inode->i_sb) - 1);
1050 bh = inode->i_sb->u.ext2_sb.s_group_desc[group_desc];
1052 ext2_error (inode->i_sb, "ext2_write_inode",
1053 "Descriptor not loaded");
1056 gdp = (struct ext2_group_desc *) bh->b_data;
1058 * Figure out the offset within the block group inode table
1060 offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) *
1061 EXT2_INODE_SIZE(inode->i_sb);
1062 block = le32_to_cpu(gdp[desc].bg_inode_table) +
1063 (offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb));
1064 if (!(bh = sb_bread(inode->i_sb, block))) {
1065 ext2_error (inode->i_sb, "ext2_write_inode",
1066 "unable to read inode block - "
1067 "inode=%lu, block=%lu", inode->i_ino, block);
1070 offset &= EXT2_BLOCK_SIZE(inode->i_sb) - 1;
1071 raw_inode = (struct ext2_inode *) (bh->b_data + offset);
1073 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
1074 if(!(test_opt(inode->i_sb, NO_UID32))) {
1075 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
1076 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
1078 * Fix up interoperability with old kernels. Otherwise, old inodes get
1079 * re-used with the upper 16 bits of the uid/gid intact
1081 if(!inode->u.ext2_i.i_dtime) {
1082 raw_inode->i_uid_high = cpu_to_le16(high_16_bits(inode->i_uid));
1083 raw_inode->i_gid_high = cpu_to_le16(high_16_bits(inode->i_gid));
1085 raw_inode->i_uid_high = 0;
1086 raw_inode->i_gid_high = 0;
1089 raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(inode->i_uid));
1090 raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(inode->i_gid));
1091 raw_inode->i_uid_high = 0;
1092 raw_inode->i_gid_high = 0;
1094 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
1095 raw_inode->i_size = cpu_to_le32(inode->i_size);
1096 raw_inode->i_atime = cpu_to_le32(inode->i_atime);
1097 raw_inode->i_ctime = cpu_to_le32(inode->i_ctime);
1098 raw_inode->i_mtime = cpu_to_le32(inode->i_mtime);
1099 raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
1100 raw_inode->i_dtime = cpu_to_le32(inode->u.ext2_i.i_dtime);
1101 raw_inode->i_flags = cpu_to_le32(inode->u.ext2_i.i_flags);
1102 raw_inode->i_faddr = cpu_to_le32(inode->u.ext2_i.i_faddr);
1103 raw_inode->i_frag = inode->u.ext2_i.i_frag_no;
1104 raw_inode->i_fsize = inode->u.ext2_i.i_frag_size;
1105 raw_inode->i_file_acl = cpu_to_le32(inode->u.ext2_i.i_file_acl);
1106 if (!S_ISREG(inode->i_mode))
1107 raw_inode->i_dir_acl = cpu_to_le32(inode->u.ext2_i.i_dir_acl);
1109 raw_inode->i_size_high = cpu_to_le32(inode->i_size >> 32);
1110 if (inode->i_size > 0x7fffffffULL) {
1111 struct super_block *sb = inode->i_sb;
1112 if (!EXT2_HAS_RO_COMPAT_FEATURE(sb,
1113 EXT2_FEATURE_RO_COMPAT_LARGE_FILE) ||
1114 EXT2_SB(sb)->s_es->s_rev_level ==
1115 cpu_to_le32(EXT2_GOOD_OLD_REV)) {
1116 /* If this is the first large file
1117 * created, add a flag to the superblock.
1120 ext2_update_dynamic_rev(sb);
1121 EXT2_SET_RO_COMPAT_FEATURE(sb,
1122 EXT2_FEATURE_RO_COMPAT_LARGE_FILE);
1124 ext2_write_super(sb);
1129 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
1130 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
1131 raw_inode->i_block[0] = cpu_to_le32(kdev_t_to_nr(inode->i_rdev));
1132 else for (block = 0; block < EXT2_N_BLOCKS; block++)
1133 raw_inode->i_block[block] = inode->u.ext2_i.i_data[block];
1134 mark_buffer_dirty(bh);
1136 ll_rw_block (WRITE, 1, &bh);
1137 wait_on_buffer (bh);
1138 if (buffer_req(bh) && !buffer_uptodate(bh)) {
1139 printk ("IO error syncing ext2 inode ["
1141 bdevname(inode->i_dev), inode->i_ino);
1149 void ext2_write_inode (struct inode * inode, int wait)
1152 ext2_update_inode (inode, wait);
1156 int ext2_sync_inode (struct inode *inode)
1158 return ext2_update_inode (inode, 1);