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/locks.h>
27 #include <linux/smp_lock.h>
28 #include <linux/sched.h>
29 #include <linux/highuid.h>
31 static int ext2_update_inode(struct inode
* inode
, int do_sync
);
34 * Called at each iput()
36 void ext2_put_inode (struct inode
* inode
)
38 ext2_discard_prealloc (inode
);
42 * Called at the last iput() if i_nlink is zero.
44 void ext2_delete_inode (struct inode
* inode
)
48 if (is_bad_inode(inode
) ||
49 inode
->i_ino
== EXT2_ACL_IDX_INO
||
50 inode
->i_ino
== EXT2_ACL_DATA_INO
)
52 inode
->u
.ext2_i
.i_dtime
= CURRENT_TIME
;
53 mark_inode_dirty(inode
);
54 ext2_update_inode(inode
, IS_SYNC(inode
));
57 ext2_truncate (inode
);
58 ext2_free_inode (inode
);
64 clear_inode(inode
); /* We must guarantee clearing of inode... */
67 void ext2_discard_prealloc (struct inode
* inode
)
69 #ifdef EXT2_PREALLOCATE
71 /* Writer: ->i_prealloc* */
72 if (inode
->u
.ext2_i
.i_prealloc_count
) {
73 unsigned short total
= inode
->u
.ext2_i
.i_prealloc_count
;
74 unsigned long block
= inode
->u
.ext2_i
.i_prealloc_block
;
75 inode
->u
.ext2_i
.i_prealloc_count
= 0;
76 inode
->u
.ext2_i
.i_prealloc_block
= 0;
78 ext2_free_blocks (inode
, block
, total
);
84 static int ext2_alloc_block (struct inode
* inode
, unsigned long goal
, int *err
)
87 static unsigned long alloc_hits
= 0, alloc_attempts
= 0;
92 #ifdef EXT2_PREALLOCATE
93 /* Writer: ->i_prealloc* */
94 if (inode
->u
.ext2_i
.i_prealloc_count
&&
95 (goal
== inode
->u
.ext2_i
.i_prealloc_block
||
96 goal
+ 1 == inode
->u
.ext2_i
.i_prealloc_block
))
98 result
= inode
->u
.ext2_i
.i_prealloc_block
++;
99 inode
->u
.ext2_i
.i_prealloc_count
--;
102 ext2_debug ("preallocation hit (%lu/%lu).\n",
103 ++alloc_hits
, ++alloc_attempts
);
106 ext2_discard_prealloc (inode
);
108 ext2_debug ("preallocation miss (%lu/%lu).\n",
109 alloc_hits
, ++alloc_attempts
);
111 if (S_ISREG(inode
->i_mode
))
112 result
= ext2_new_block (inode
, goal
,
113 &inode
->u
.ext2_i
.i_prealloc_count
,
114 &inode
->u
.ext2_i
.i_prealloc_block
, err
);
116 result
= ext2_new_block (inode
, goal
, 0, 0, err
);
119 result
= ext2_new_block (inode
, goal
, 0, 0, err
);
127 struct buffer_head
*bh
;
130 static inline void add_chain(Indirect
*p
, struct buffer_head
*bh
, u32
*v
)
132 p
->key
= *(p
->p
= v
);
136 static inline int verify_chain(Indirect
*from
, Indirect
*to
)
138 while (from
<= to
&& from
->key
== *from
->p
)
144 * ext2_block_to_path - parse the block number into array of offsets
145 * @inode: inode in question (we are only interested in its superblock)
146 * @i_block: block number to be parsed
147 * @offsets: array to store the offsets in
149 * To store the locations of file's data ext2 uses a data structure common
150 * for UNIX filesystems - tree of pointers anchored in the inode, with
151 * data blocks at leaves and indirect blocks in intermediate nodes.
152 * This function translates the block number into path in that tree -
153 * return value is the path length and @offsets[n] is the offset of
154 * pointer to (n+1)th node in the nth one. If @block is out of range
155 * (negative or too large) warning is printed and zero returned.
157 * Note: function doesn't find node addresses, so no IO is needed. All
158 * we need to know is the capacity of indirect blocks (taken from the
163 * Portability note: the last comparison (check that we fit into triple
164 * indirect block) is spelled differently, because otherwise on an
165 * architecture with 32-bit longs and 8Kb pages we might get into trouble
166 * if our filesystem had 8Kb blocks. We might use long long, but that would
167 * kill us on x86. Oh, well, at least the sign propagation does not matter -
168 * i_block would have to be negative in the very beginning, so we would not
172 static int ext2_block_to_path(struct inode
*inode
, long i_block
, int offsets
[4])
174 int ptrs
= EXT2_ADDR_PER_BLOCK(inode
->i_sb
);
175 int ptrs_bits
= EXT2_ADDR_PER_BLOCK_BITS(inode
->i_sb
);
176 const long direct_blocks
= EXT2_NDIR_BLOCKS
,
177 indirect_blocks
= ptrs
,
178 double_blocks
= (1 << (ptrs_bits
* 2));
182 ext2_warning (inode
->i_sb
, "ext2_block_to_path", "block < 0");
183 } else if (i_block
< direct_blocks
) {
184 offsets
[n
++] = i_block
;
185 } else if ( (i_block
-= direct_blocks
) < indirect_blocks
) {
186 offsets
[n
++] = EXT2_IND_BLOCK
;
187 offsets
[n
++] = i_block
;
188 } else if ((i_block
-= indirect_blocks
) < double_blocks
) {
189 offsets
[n
++] = EXT2_DIND_BLOCK
;
190 offsets
[n
++] = i_block
>> ptrs_bits
;
191 offsets
[n
++] = i_block
& (ptrs
- 1);
192 } else if (((i_block
-= double_blocks
) >> (ptrs_bits
* 2)) < ptrs
) {
193 offsets
[n
++] = EXT2_TIND_BLOCK
;
194 offsets
[n
++] = i_block
>> (ptrs_bits
* 2);
195 offsets
[n
++] = (i_block
>> ptrs_bits
) & (ptrs
- 1);
196 offsets
[n
++] = i_block
& (ptrs
- 1);
198 ext2_warning (inode
->i_sb
, "ext2_block_to_path", "block > big");
204 * ext2_get_branch - read the chain of indirect blocks leading to data
205 * @inode: inode in question
206 * @depth: depth of the chain (1 - direct pointer, etc.)
207 * @offsets: offsets of pointers in inode/indirect blocks
208 * @chain: place to store the result
209 * @err: here we store the error value
211 * Function fills the array of triples <key, p, bh> and returns %NULL
212 * if everything went OK or the pointer to the last filled triple
213 * (incomplete one) otherwise. Upon the return chain[i].key contains
214 * the number of (i+1)-th block in the chain (as it is stored in memory,
215 * i.e. little-endian 32-bit), chain[i].p contains the address of that
216 * number (it points into struct inode for i==0 and into the bh->b_data
217 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
218 * block for i>0 and NULL for i==0. In other words, it holds the block
219 * numbers of the chain, addresses they were taken from (and where we can
220 * verify that chain did not change) and buffer_heads hosting these
223 * Function stops when it stumbles upon zero pointer (absent block)
224 * (pointer to last triple returned, *@err == 0)
225 * or when it gets an IO error reading an indirect block
226 * (ditto, *@err == -EIO)
227 * or when it notices that chain had been changed while it was reading
228 * (ditto, *@err == -EAGAIN)
229 * or when it reads all @depth-1 indirect blocks successfully and finds
230 * the whole chain, all way to the data (returns %NULL, *err == 0).
232 static inline Indirect
*ext2_get_branch(struct inode
*inode
,
238 kdev_t dev
= inode
->i_dev
;
239 int size
= inode
->i_sb
->s_blocksize
;
241 struct buffer_head
*bh
;
244 /* i_data is not going away, no lock needed */
245 add_chain (chain
, NULL
, inode
->u
.ext2_i
.i_data
+ *offsets
);
249 * switch below is merely an unrolled loop - body should be
250 * repeated depth-1 times. Maybe loop would be actually better,
251 * but that way we get straight execution path in normal cases.
252 * Easy to change, anyway - all cases in switch are literally
257 bh
= bread(dev
, le32_to_cpu(p
->key
), size
);
260 /* Reader: pointers */
261 if (!verify_chain(chain
, p
))
263 add_chain(++p
, bh
, (u32
*)bh
->b_data
+ *++offsets
);
268 bh
= bread(dev
, le32_to_cpu(p
->key
), size
);
271 /* Reader: pointers */
272 if (!verify_chain(chain
, p
))
274 add_chain(++p
, bh
, (u32
*)bh
->b_data
+ *++offsets
);
279 bh
= bread(dev
, le32_to_cpu(p
->key
), size
);
282 /* Reader: pointers */
283 if (!verify_chain(chain
, p
))
285 add_chain(++p
, bh
, (u32
*)bh
->b_data
+ *++offsets
);
302 * ext2_find_near - find a place for allocation with sufficient locality
304 * @ind: descriptor of indirect block.
306 * This function returns the prefered place for block allocation.
307 * It is used when heuristic for sequential allocation fails.
309 * + if there is a block to the left of our position - allocate near it.
310 * + if pointer will live in indirect block - allocate near that block.
311 * + if pointer will live in inode - allocate in the same cylinder group.
312 * Caller must make sure that @ind is valid and will stay that way.
315 static inline unsigned long ext2_find_near(struct inode
*inode
, Indirect
*ind
)
317 u32
*start
= ind
->bh
? (u32
*) ind
->bh
->b_data
: inode
->u
.ext2_i
.i_data
;
320 /* Try to find previous block */
321 for (p
= ind
->p
- 1; p
>= start
; p
--)
323 return le32_to_cpu(*p
);
325 /* No such thing, so let's try location of indirect block */
327 return ind
->bh
->b_blocknr
;
330 * It is going to be refered from inode itself? OK, just put it into
331 * the same cylinder group then.
333 return (inode
->u
.ext2_i
.i_block_group
*
334 EXT2_BLOCKS_PER_GROUP(inode
->i_sb
)) +
335 le32_to_cpu(inode
->i_sb
->u
.ext2_sb
.s_es
->s_first_data_block
);
339 * ext2_find_goal - find a prefered place for allocation.
341 * @block: block we want
342 * @chain: chain of indirect blocks
343 * @partial: pointer to the last triple within a chain.
345 * This function returns the prefered place for block allocation.
348 static inline unsigned long ext2_find_goal(struct inode
*inode
,
353 unsigned long goal
= 0;
355 /* Writer: ->i_next_alloc* */
356 if (block
== inode
->u
.ext2_i
.i_next_alloc_block
+ 1) {
357 inode
->u
.ext2_i
.i_next_alloc_block
++;
358 inode
->u
.ext2_i
.i_next_alloc_goal
++;
361 /* Reader: pointers, ->i_next_alloc* */
362 if (verify_chain(chain
, partial
)) {
364 * try the heuristic for sequential allocation,
365 * failing that at least try to get decent locality.
367 if (block
== inode
->u
.ext2_i
.i_next_alloc_block
)
368 goal
= inode
->u
.ext2_i
.i_next_alloc_goal
;
370 goal
= ext2_find_near(inode
, partial
);
377 * ext2_alloc_branch - allocate and set up a chain of blocks.
379 * @num: depth of the chain (number of blocks to allocate)
380 * @offsets: offsets (in the blocks) to store the pointers to next.
381 * @branch: place to store the chain in.
383 * This function allocates @num blocks, zeroes out all but the last one,
384 * links them into chain and (if we are synchronous) writes them to disk.
385 * In other words, it prepares a branch that can be spliced onto the
386 * inode. It stores the information about that chain in the branch[], in
387 * the same format as ext2_get_branch() would do. We are calling it after
388 * we had read the existing part of chain and partial points to the last
389 * triple of that (one with zero ->key). Upon the exit we have the same
390 * picture as after the successful ext2_get_block(), excpet that in one
391 * place chain is disconnected - *branch->p is still zero (we did not
392 * set the last link), but branch->key contains the number that should
393 * be placed into *branch->p to fill that gap.
395 * If allocation fails we free all blocks we've allocated (and forget
396 * ther buffer_heads) and return the error value the from failed
397 * ext2_alloc_block() (normally -ENOSPC). Otherwise we set the chain
398 * as described above and return 0.
401 static int ext2_alloc_branch(struct inode
*inode
,
407 int blocksize
= inode
->i_sb
->s_blocksize
;
411 int parent
= ext2_alloc_block(inode
, goal
, &err
);
413 branch
[0].key
= cpu_to_le32(parent
);
414 if (parent
) for (n
= 1; n
< num
; n
++) {
415 struct buffer_head
*bh
;
416 /* Allocate the next block */
417 int nr
= ext2_alloc_block(inode
, parent
, &err
);
420 branch
[n
].key
= cpu_to_le32(nr
);
422 * Get buffer_head for parent block, zero it out and set
423 * the pointer to new one, then send parent to disk.
425 bh
= getblk(inode
->i_dev
, parent
, blocksize
);
426 if (!buffer_uptodate(bh
))
428 memset(bh
->b_data
, 0, blocksize
);
430 branch
[n
].p
= (u32
*) bh
->b_data
+ offsets
[n
];
431 *branch
[n
].p
= branch
[n
].key
;
432 mark_buffer_uptodate(bh
, 1);
433 mark_buffer_dirty(bh
, 1);
434 if (IS_SYNC(inode
) || inode
->u
.ext2_i
.i_osync
) {
435 ll_rw_block (WRITE
, 1, &bh
);
443 /* Allocation failed, free what we already allocated */
444 for (i
= 1; i
< n
; i
++)
445 bforget(branch
[i
].bh
);
446 for (i
= 0; i
< n
; i
++)
447 ext2_free_blocks(inode
, le32_to_cpu(branch
[i
].key
), 1);
452 * ext2_splice_branch - splice the allocated branch onto inode.
454 * @block: (logical) number of block we are adding
455 * @chain: chain of indirect blocks (with a missing link - see
457 * @where: location of missing link
458 * @num: number of blocks we are adding
460 * This function verifies that chain (up to the missing link) had not
461 * changed, fills the missing link and does all housekeeping needed in
462 * inode (->i_blocks, etc.). In case of success we end up with the full
463 * chain to new block and return 0. Otherwise (== chain had been changed)
464 * we free the new blocks (forgetting their buffer_heads, indeed) and
468 static inline int ext2_splice_branch(struct inode
*inode
,
476 /* Verify that place we are splicing to is still there and vacant */
478 /* Writer: pointers, ->i_next_alloc*, ->i_blocks */
479 if (!verify_chain(chain
, where
-1) || *where
->p
)
485 *where
->p
= where
->key
;
486 inode
->u
.ext2_i
.i_next_alloc_block
= block
;
487 inode
->u
.ext2_i
.i_next_alloc_goal
= le32_to_cpu(where
[num
-1].key
);
488 inode
->i_blocks
+= num
* inode
->i_sb
->s_blocksize
/512;
492 /* We are done with atomic stuff, now do the rest of housekeeping */
494 inode
->i_ctime
= CURRENT_TIME
;
496 /* had we spliced it onto indirect block? */
498 mark_buffer_dirty(where
->bh
, 1);
499 if (IS_SYNC(inode
) || inode
->u
.ext2_i
.i_osync
) {
500 ll_rw_block (WRITE
, 1, &where
->bh
);
501 wait_on_buffer(where
->bh
);
505 if (IS_SYNC(inode
) || inode
->u
.ext2_i
.i_osync
)
506 ext2_sync_inode (inode
);
508 mark_inode_dirty(inode
);
512 for (i
= 1; i
< num
; i
++)
513 bforget(where
[i
].bh
);
514 for (i
= 0; i
< num
; i
++)
515 ext2_free_blocks(inode
, le32_to_cpu(where
[i
].key
), 1);
520 * Allocation strategy is simple: if we have to allocate something, we will
521 * have to go the whole way to leaf. So let's do it before attaching anything
522 * to tree, set linkage between the newborn blocks, write them if sync is
523 * required, recheck the path, free and repeat if check fails, otherwise
524 * set the last missing link (that will protect us from any truncate-generated
525 * removals - all blocks on the path are immune now) and possibly force the
526 * write on the parent block.
527 * That has a nice additional property: no special recovery from the failed
528 * allocations is needed - we simply release blocks and do not touch anything
529 * reachable from inode.
532 static int ext2_get_block(struct inode
*inode
, long iblock
, struct buffer_head
*bh_result
, int create
)
540 int depth
= ext2_block_to_path(inode
, iblock
, offsets
);
547 partial
= ext2_get_branch(inode
, depth
, offsets
, chain
, &err
);
549 /* Simplest case - block found, no allocation needed */
552 bh_result
->b_dev
= inode
->i_dev
;
553 bh_result
->b_blocknr
= le32_to_cpu(chain
[depth
-1].key
);
554 bh_result
->b_state
|= (1UL << BH_Mapped
);
555 /* Clean up and exit */
556 partial
= chain
+depth
-1; /* the whole chain */
560 /* Next simple case - plain lookup or failed read of indirect block */
561 if (!create
|| err
== -EIO
) {
563 while (partial
> chain
) {
573 * Indirect block might be removed by truncate while we were
574 * reading it. Handling of that case (forget what we've got and
575 * reread) is taken out of the main path.
580 goal
= ext2_find_goal(inode
, iblock
, chain
, partial
);
584 left
= (chain
+ depth
) - partial
;
585 err
= ext2_alloc_branch(inode
, left
, goal
,
586 offsets
+(partial
-chain
), partial
);
590 if (ext2_splice_branch(inode
, iblock
, chain
, partial
, left
) < 0)
593 bh_result
->b_state
|= (1UL << BH_New
);
597 while (partial
> chain
) {
598 bforget(partial
->bh
);
604 struct buffer_head
* ext2_getblk(struct inode
* inode
, long block
, int create
, int * err
)
606 struct buffer_head dummy
;
610 dummy
.b_blocknr
= -1000;
611 error
= ext2_get_block(inode
, block
, &dummy
, create
);
613 if (!error
&& buffer_mapped(&dummy
)) {
614 struct buffer_head
*bh
;
615 bh
= getblk(dummy
.b_dev
, dummy
.b_blocknr
, inode
->i_sb
->s_blocksize
);
616 if (buffer_new(&dummy
)) {
617 if (!buffer_uptodate(bh
))
619 memset(bh
->b_data
, 0, inode
->i_sb
->s_blocksize
);
620 mark_buffer_uptodate(bh
, 1);
621 mark_buffer_dirty(bh
, 1);
628 struct buffer_head
* ext2_bread (struct inode
* inode
, int block
,
629 int create
, int *err
)
631 struct buffer_head
* bh
;
634 prev_blocks
= inode
->i_blocks
;
636 bh
= ext2_getblk (inode
, block
, create
, err
);
641 * If the inode has grown, and this is a directory, then perform
642 * preallocation of a few more blocks to try to keep directory
643 * fragmentation down.
646 S_ISDIR(inode
->i_mode
) &&
647 inode
->i_blocks
> prev_blocks
&&
648 EXT2_HAS_COMPAT_FEATURE(inode
->i_sb
,
649 EXT2_FEATURE_COMPAT_DIR_PREALLOC
)) {
651 struct buffer_head
*tmp_bh
;
654 i
< EXT2_SB(inode
->i_sb
)->s_es
->s_prealloc_dir_blocks
;
657 * ext2_getblk will zero out the contents of the
660 tmp_bh
= ext2_getblk(inode
, block
+i
, create
, err
);
669 if (buffer_uptodate(bh
))
671 ll_rw_block (READ
, 1, &bh
);
673 if (buffer_uptodate(bh
))
680 static int ext2_writepage(struct file
*file
, struct page
*page
)
682 return block_write_full_page(page
,ext2_get_block
);
684 static int ext2_readpage(struct file
*file
, struct page
*page
)
686 return block_read_full_page(page
,ext2_get_block
);
688 static int ext2_prepare_write(struct file
*file
, struct page
*page
, unsigned from
, unsigned to
)
690 return block_prepare_write(page
,from
,to
,ext2_get_block
);
692 static int ext2_bmap(struct address_space
*mapping
, long block
)
694 return generic_block_bmap(mapping
,block
,ext2_get_block
);
696 struct address_space_operations ext2_aops
= {
697 readpage
: ext2_readpage
,
698 writepage
: ext2_writepage
,
699 sync_page
: block_sync_page
,
700 prepare_write
: ext2_prepare_write
,
701 commit_write
: generic_commit_write
,
705 void ext2_read_inode (struct inode
* inode
)
707 struct buffer_head
* bh
;
708 struct ext2_inode
* raw_inode
;
709 unsigned long block_group
;
710 unsigned long group_desc
;
713 unsigned long offset
;
714 struct ext2_group_desc
* gdp
;
716 if ((inode
->i_ino
!= EXT2_ROOT_INO
&& inode
->i_ino
!= EXT2_ACL_IDX_INO
&&
717 inode
->i_ino
!= EXT2_ACL_DATA_INO
&&
718 inode
->i_ino
< EXT2_FIRST_INO(inode
->i_sb
)) ||
719 inode
->i_ino
> le32_to_cpu(inode
->i_sb
->u
.ext2_sb
.s_es
->s_inodes_count
)) {
720 ext2_error (inode
->i_sb
, "ext2_read_inode",
721 "bad inode number: %lu", inode
->i_ino
);
724 block_group
= (inode
->i_ino
- 1) / EXT2_INODES_PER_GROUP(inode
->i_sb
);
725 if (block_group
>= inode
->i_sb
->u
.ext2_sb
.s_groups_count
) {
726 ext2_error (inode
->i_sb
, "ext2_read_inode",
727 "group >= groups count");
730 group_desc
= block_group
>> EXT2_DESC_PER_BLOCK_BITS(inode
->i_sb
);
731 desc
= block_group
& (EXT2_DESC_PER_BLOCK(inode
->i_sb
) - 1);
732 bh
= inode
->i_sb
->u
.ext2_sb
.s_group_desc
[group_desc
];
734 ext2_error (inode
->i_sb
, "ext2_read_inode",
735 "Descriptor not loaded");
739 gdp
= (struct ext2_group_desc
*) bh
->b_data
;
741 * Figure out the offset within the block group inode table
743 offset
= ((inode
->i_ino
- 1) % EXT2_INODES_PER_GROUP(inode
->i_sb
)) *
744 EXT2_INODE_SIZE(inode
->i_sb
);
745 block
= le32_to_cpu(gdp
[desc
].bg_inode_table
) +
746 (offset
>> EXT2_BLOCK_SIZE_BITS(inode
->i_sb
));
747 if (!(bh
= bread (inode
->i_dev
, block
, inode
->i_sb
->s_blocksize
))) {
748 ext2_error (inode
->i_sb
, "ext2_read_inode",
749 "unable to read inode block - "
750 "inode=%lu, block=%lu", inode
->i_ino
, block
);
753 offset
&= (EXT2_BLOCK_SIZE(inode
->i_sb
) - 1);
754 raw_inode
= (struct ext2_inode
*) (bh
->b_data
+ offset
);
756 inode
->i_mode
= le16_to_cpu(raw_inode
->i_mode
);
757 inode
->i_uid
= (uid_t
)le16_to_cpu(raw_inode
->i_uid_low
);
758 inode
->i_gid
= (gid_t
)le16_to_cpu(raw_inode
->i_gid_low
);
759 if(!(test_opt (inode
->i_sb
, NO_UID32
))) {
760 inode
->i_uid
|= le16_to_cpu(raw_inode
->i_uid_high
) << 16;
761 inode
->i_gid
|= le16_to_cpu(raw_inode
->i_gid_high
) << 16;
763 inode
->i_nlink
= le16_to_cpu(raw_inode
->i_links_count
);
764 inode
->i_size
= le32_to_cpu(raw_inode
->i_size
);
765 inode
->i_atime
= le32_to_cpu(raw_inode
->i_atime
);
766 inode
->i_ctime
= le32_to_cpu(raw_inode
->i_ctime
);
767 inode
->i_mtime
= le32_to_cpu(raw_inode
->i_mtime
);
768 inode
->u
.ext2_i
.i_dtime
= le32_to_cpu(raw_inode
->i_dtime
);
769 /* We now have enough fields to check if the inode was active or not.
770 * This is needed because nfsd might try to access dead inodes
771 * the test is that same one that e2fsck uses
772 * NeilBrown 1999oct15
774 if (inode
->i_nlink
== 0 && (inode
->i_mode
== 0 || inode
->u
.ext2_i
.i_dtime
)) {
775 /* this inode is deleted */
779 inode
->i_blksize
= PAGE_SIZE
; /* This is the optimal IO size (for stat), not the fs block size */
780 inode
->i_blocks
= le32_to_cpu(raw_inode
->i_blocks
);
781 inode
->i_version
= ++event
;
782 inode
->u
.ext2_i
.i_new_inode
= 0;
783 inode
->u
.ext2_i
.i_flags
= le32_to_cpu(raw_inode
->i_flags
);
784 inode
->u
.ext2_i
.i_faddr
= le32_to_cpu(raw_inode
->i_faddr
);
785 inode
->u
.ext2_i
.i_frag_no
= raw_inode
->i_frag
;
786 inode
->u
.ext2_i
.i_frag_size
= raw_inode
->i_fsize
;
787 inode
->u
.ext2_i
.i_osync
= 0;
788 inode
->u
.ext2_i
.i_file_acl
= le32_to_cpu(raw_inode
->i_file_acl
);
789 if (S_ISDIR(inode
->i_mode
))
790 inode
->u
.ext2_i
.i_dir_acl
= le32_to_cpu(raw_inode
->i_dir_acl
);
792 inode
->u
.ext2_i
.i_dir_acl
= 0;
793 inode
->u
.ext2_i
.i_high_size
= le32_to_cpu(raw_inode
->i_size_high
);
794 inode
->i_size
|= ((__u64
)le32_to_cpu(raw_inode
->i_size_high
)) << 32;
796 inode
->i_generation
= le32_to_cpu(raw_inode
->i_generation
);
797 inode
->u
.ext2_i
.i_block_group
= block_group
;
798 inode
->u
.ext2_i
.i_next_alloc_block
= 0;
799 inode
->u
.ext2_i
.i_next_alloc_goal
= 0;
800 if (inode
->u
.ext2_i
.i_prealloc_count
)
801 ext2_error (inode
->i_sb
, "ext2_read_inode",
802 "New inode has non-zero prealloc count!");
805 * NOTE! The in-memory inode i_blocks array is in little-endian order
806 * even on big-endian machines: we do NOT byteswap the block numbers!
808 for (block
= 0; block
< EXT2_N_BLOCKS
; block
++)
809 inode
->u
.ext2_i
.i_data
[block
] = raw_inode
->i_block
[block
];
811 if (inode
->i_ino
== EXT2_ACL_IDX_INO
||
812 inode
->i_ino
== EXT2_ACL_DATA_INO
)
813 /* Nothing to do */ ;
814 else if (S_ISREG(inode
->i_mode
)) {
815 inode
->i_op
= &ext2_file_inode_operations
;
816 inode
->i_fop
= &ext2_file_operations
;
817 inode
->i_mapping
->a_ops
= &ext2_aops
;
818 } else if (S_ISDIR(inode
->i_mode
)) {
819 inode
->i_op
= &ext2_dir_inode_operations
;
820 inode
->i_fop
= &ext2_dir_operations
;
821 } else if (S_ISLNK(inode
->i_mode
)) {
822 if (!inode
->i_blocks
)
823 inode
->i_op
= &ext2_fast_symlink_inode_operations
;
825 inode
->i_op
= &page_symlink_inode_operations
;
826 inode
->i_mapping
->a_ops
= &ext2_aops
;
829 init_special_inode(inode
, inode
->i_mode
,
830 le32_to_cpu(raw_inode
->i_block
[0]));
832 inode
->i_attr_flags
= 0;
833 if (inode
->u
.ext2_i
.i_flags
& EXT2_SYNC_FL
) {
834 inode
->i_attr_flags
|= ATTR_FLAG_SYNCRONOUS
;
835 inode
->i_flags
|= S_SYNC
;
837 if (inode
->u
.ext2_i
.i_flags
& EXT2_APPEND_FL
) {
838 inode
->i_attr_flags
|= ATTR_FLAG_APPEND
;
839 inode
->i_flags
|= S_APPEND
;
841 if (inode
->u
.ext2_i
.i_flags
& EXT2_IMMUTABLE_FL
) {
842 inode
->i_attr_flags
|= ATTR_FLAG_IMMUTABLE
;
843 inode
->i_flags
|= S_IMMUTABLE
;
845 if (inode
->u
.ext2_i
.i_flags
& EXT2_NOATIME_FL
) {
846 inode
->i_attr_flags
|= ATTR_FLAG_NOATIME
;
847 inode
->i_flags
|= S_NOATIME
;
852 make_bad_inode(inode
);
856 static int ext2_update_inode(struct inode
* inode
, int do_sync
)
858 struct buffer_head
* bh
;
859 struct ext2_inode
* raw_inode
;
860 unsigned long block_group
;
861 unsigned long group_desc
;
864 unsigned long offset
;
866 struct ext2_group_desc
* gdp
;
868 if ((inode
->i_ino
!= EXT2_ROOT_INO
&&
869 inode
->i_ino
< EXT2_FIRST_INO(inode
->i_sb
)) ||
870 inode
->i_ino
> le32_to_cpu(inode
->i_sb
->u
.ext2_sb
.s_es
->s_inodes_count
)) {
871 ext2_error (inode
->i_sb
, "ext2_write_inode",
872 "bad inode number: %lu", inode
->i_ino
);
875 block_group
= (inode
->i_ino
- 1) / EXT2_INODES_PER_GROUP(inode
->i_sb
);
876 if (block_group
>= inode
->i_sb
->u
.ext2_sb
.s_groups_count
) {
877 ext2_error (inode
->i_sb
, "ext2_write_inode",
878 "group >= groups count");
881 group_desc
= block_group
>> EXT2_DESC_PER_BLOCK_BITS(inode
->i_sb
);
882 desc
= block_group
& (EXT2_DESC_PER_BLOCK(inode
->i_sb
) - 1);
883 bh
= inode
->i_sb
->u
.ext2_sb
.s_group_desc
[group_desc
];
885 ext2_error (inode
->i_sb
, "ext2_write_inode",
886 "Descriptor not loaded");
889 gdp
= (struct ext2_group_desc
*) bh
->b_data
;
891 * Figure out the offset within the block group inode table
893 offset
= ((inode
->i_ino
- 1) % EXT2_INODES_PER_GROUP(inode
->i_sb
)) *
894 EXT2_INODE_SIZE(inode
->i_sb
);
895 block
= le32_to_cpu(gdp
[desc
].bg_inode_table
) +
896 (offset
>> EXT2_BLOCK_SIZE_BITS(inode
->i_sb
));
897 if (!(bh
= bread (inode
->i_dev
, block
, inode
->i_sb
->s_blocksize
))) {
898 ext2_error (inode
->i_sb
, "ext2_write_inode",
899 "unable to read inode block - "
900 "inode=%lu, block=%lu", inode
->i_ino
, block
);
903 offset
&= EXT2_BLOCK_SIZE(inode
->i_sb
) - 1;
904 raw_inode
= (struct ext2_inode
*) (bh
->b_data
+ offset
);
906 raw_inode
->i_mode
= cpu_to_le16(inode
->i_mode
);
907 if(!(test_opt(inode
->i_sb
, NO_UID32
))) {
908 raw_inode
->i_uid_low
= cpu_to_le16(low_16_bits(inode
->i_uid
));
909 raw_inode
->i_gid_low
= cpu_to_le16(low_16_bits(inode
->i_gid
));
911 * Fix up interoperability with old kernels. Otherwise, old inodes get
912 * re-used with the upper 16 bits of the uid/gid intact
914 if(!inode
->u
.ext2_i
.i_dtime
) {
915 raw_inode
->i_uid_high
= cpu_to_le16(high_16_bits(inode
->i_uid
));
916 raw_inode
->i_gid_high
= cpu_to_le16(high_16_bits(inode
->i_gid
));
918 raw_inode
->i_uid_high
= 0;
919 raw_inode
->i_gid_high
= 0;
922 raw_inode
->i_uid_low
= cpu_to_le16(fs_high2lowuid(inode
->i_uid
));
923 raw_inode
->i_gid_low
= cpu_to_le16(fs_high2lowgid(inode
->i_gid
));
924 raw_inode
->i_uid_high
= 0;
925 raw_inode
->i_gid_high
= 0;
927 raw_inode
->i_links_count
= cpu_to_le16(inode
->i_nlink
);
928 raw_inode
->i_size
= cpu_to_le32(inode
->i_size
);
929 raw_inode
->i_atime
= cpu_to_le32(inode
->i_atime
);
930 raw_inode
->i_ctime
= cpu_to_le32(inode
->i_ctime
);
931 raw_inode
->i_mtime
= cpu_to_le32(inode
->i_mtime
);
932 raw_inode
->i_blocks
= cpu_to_le32(inode
->i_blocks
);
933 raw_inode
->i_dtime
= cpu_to_le32(inode
->u
.ext2_i
.i_dtime
);
934 raw_inode
->i_flags
= cpu_to_le32(inode
->u
.ext2_i
.i_flags
);
935 raw_inode
->i_faddr
= cpu_to_le32(inode
->u
.ext2_i
.i_faddr
);
936 raw_inode
->i_frag
= inode
->u
.ext2_i
.i_frag_no
;
937 raw_inode
->i_fsize
= inode
->u
.ext2_i
.i_frag_size
;
938 raw_inode
->i_file_acl
= cpu_to_le32(inode
->u
.ext2_i
.i_file_acl
);
939 if (S_ISDIR(inode
->i_mode
))
940 raw_inode
->i_dir_acl
= cpu_to_le32(inode
->u
.ext2_i
.i_dir_acl
);
942 raw_inode
->i_size_high
= cpu_to_le32(inode
->i_size
>> 32);
944 raw_inode
->i_generation
= cpu_to_le32(inode
->i_generation
);
945 if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
))
946 raw_inode
->i_block
[0] = cpu_to_le32(kdev_t_to_nr(inode
->i_rdev
));
947 else for (block
= 0; block
< EXT2_N_BLOCKS
; block
++)
948 raw_inode
->i_block
[block
] = inode
->u
.ext2_i
.i_data
[block
];
949 mark_buffer_dirty(bh
, 1);
951 ll_rw_block (WRITE
, 1, &bh
);
953 if (buffer_req(bh
) && !buffer_uptodate(bh
)) {
954 printk ("IO error syncing ext2 inode ["
956 bdevname(inode
->i_dev
), inode
->i_ino
);
964 void ext2_write_inode (struct inode
* inode
, int wait
)
967 ext2_update_inode (inode
, 0);
971 int ext2_sync_inode (struct inode
*inode
)
973 return ext2_update_inode (inode
, 1);
976 int ext2_notify_change(struct dentry
*dentry
, struct iattr
*iattr
)
978 struct inode
*inode
= dentry
->d_inode
;
983 if (iattr
->ia_valid
& ATTR_ATTR_FLAG
&&
984 ((!(iattr
->ia_attr_flags
& ATTR_FLAG_APPEND
) !=
985 !(inode
->u
.ext2_i
.i_flags
& EXT2_APPEND_FL
)) ||
986 (!(iattr
->ia_attr_flags
& ATTR_FLAG_IMMUTABLE
) !=
987 !(inode
->u
.ext2_i
.i_flags
& EXT2_IMMUTABLE_FL
)))) {
988 if (!capable(CAP_LINUX_IMMUTABLE
))
990 } else if ((current
->fsuid
!= inode
->i_uid
) && !capable(CAP_FOWNER
))
993 retval
= inode_change_ok(inode
, iattr
);
997 inode_setattr(inode
, iattr
);
999 flags
= iattr
->ia_attr_flags
;
1000 if (flags
& ATTR_FLAG_SYNCRONOUS
) {
1001 inode
->i_flags
|= S_SYNC
;
1002 inode
->u
.ext2_i
.i_flags
|= EXT2_SYNC_FL
;
1004 inode
->i_flags
&= ~S_SYNC
;
1005 inode
->u
.ext2_i
.i_flags
&= ~EXT2_SYNC_FL
;
1007 if (flags
& ATTR_FLAG_NOATIME
) {
1008 inode
->i_flags
|= S_NOATIME
;
1009 inode
->u
.ext2_i
.i_flags
|= EXT2_NOATIME_FL
;
1011 inode
->i_flags
&= ~S_NOATIME
;
1012 inode
->u
.ext2_i
.i_flags
&= ~EXT2_NOATIME_FL
;
1014 if (flags
& ATTR_FLAG_APPEND
) {
1015 inode
->i_flags
|= S_APPEND
;
1016 inode
->u
.ext2_i
.i_flags
|= EXT2_APPEND_FL
;
1018 inode
->i_flags
&= ~S_APPEND
;
1019 inode
->u
.ext2_i
.i_flags
&= ~EXT2_APPEND_FL
;
1021 if (flags
& ATTR_FLAG_IMMUTABLE
) {
1022 inode
->i_flags
|= S_IMMUTABLE
;
1023 inode
->u
.ext2_i
.i_flags
|= EXT2_IMMUTABLE_FL
;
1025 inode
->i_flags
&= ~S_IMMUTABLE
;
1026 inode
->u
.ext2_i
.i_flags
&= ~EXT2_IMMUTABLE_FL
;
1028 mark_inode_dirty(inode
);