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
25 #include <linux/smp_lock.h>
26 #include <linux/time.h>
27 #include <linux/highuid.h>
28 #include <linux/pagemap.h>
29 #include <linux/quotaops.h>
30 #include <linux/module.h>
31 #include <linux/writeback.h>
32 #include <linux/buffer_head.h>
33 #include <linux/mpage.h>
37 MODULE_AUTHOR("Remy Card and others");
38 MODULE_DESCRIPTION("Second Extended Filesystem");
39 MODULE_LICENSE("GPL");
41 static int ext2_update_inode(struct inode
* inode
, int do_sync
);
44 * Test whether an inode is a fast symlink.
46 static inline int ext2_inode_is_fast_symlink(struct inode
*inode
)
48 int ea_blocks
= EXT2_I(inode
)->i_file_acl
?
49 (inode
->i_sb
->s_blocksize
>> 9) : 0;
51 return (S_ISLNK(inode
->i_mode
) &&
52 inode
->i_blocks
- ea_blocks
== 0);
56 * Called at the last iput() if i_nlink is zero.
58 void ext2_delete_inode (struct inode
* inode
)
60 if (is_bad_inode(inode
))
62 EXT2_I(inode
)->i_dtime
= get_seconds();
63 mark_inode_dirty(inode
);
64 ext2_update_inode(inode
, inode_needs_sync(inode
));
68 ext2_truncate (inode
);
69 ext2_free_inode (inode
);
73 clear_inode(inode
); /* We must guarantee clearing of inode... */
76 void ext2_discard_prealloc (struct inode
* inode
)
78 #ifdef EXT2_PREALLOCATE
79 struct ext2_inode_info
*ei
= EXT2_I(inode
);
80 write_lock(&ei
->i_meta_lock
);
81 if (ei
->i_prealloc_count
) {
82 unsigned short total
= ei
->i_prealloc_count
;
83 unsigned long block
= ei
->i_prealloc_block
;
84 ei
->i_prealloc_count
= 0;
85 ei
->i_prealloc_block
= 0;
86 write_unlock(&ei
->i_meta_lock
);
87 ext2_free_blocks (inode
, block
, total
);
90 write_unlock(&ei
->i_meta_lock
);
94 static int ext2_alloc_block (struct inode
* inode
, unsigned long goal
, int *err
)
97 static unsigned long alloc_hits
, alloc_attempts
;
102 #ifdef EXT2_PREALLOCATE
103 struct ext2_inode_info
*ei
= EXT2_I(inode
);
104 write_lock(&ei
->i_meta_lock
);
105 if (ei
->i_prealloc_count
&&
106 (goal
== ei
->i_prealloc_block
|| goal
+ 1 == ei
->i_prealloc_block
))
108 result
= ei
->i_prealloc_block
++;
109 ei
->i_prealloc_count
--;
110 write_unlock(&ei
->i_meta_lock
);
111 ext2_debug ("preallocation hit (%lu/%lu).\n",
112 ++alloc_hits
, ++alloc_attempts
);
114 write_unlock(&ei
->i_meta_lock
);
115 ext2_discard_prealloc (inode
);
116 ext2_debug ("preallocation miss (%lu/%lu).\n",
117 alloc_hits
, ++alloc_attempts
);
118 if (S_ISREG(inode
->i_mode
))
119 result
= ext2_new_block (inode
, goal
,
120 &ei
->i_prealloc_count
,
121 &ei
->i_prealloc_block
, err
);
123 result
= ext2_new_block(inode
, goal
, NULL
, NULL
, err
);
126 result
= ext2_new_block (inode
, goal
, 0, 0, err
);
134 struct buffer_head
*bh
;
137 static inline void add_chain(Indirect
*p
, struct buffer_head
*bh
, __le32
*v
)
139 p
->key
= *(p
->p
= v
);
143 static inline int verify_chain(Indirect
*from
, Indirect
*to
)
145 while (from
<= to
&& from
->key
== *from
->p
)
151 * ext2_block_to_path - parse the block number into array of offsets
152 * @inode: inode in question (we are only interested in its superblock)
153 * @i_block: block number to be parsed
154 * @offsets: array to store the offsets in
155 * @boundary: set this non-zero if the referred-to block is likely to be
156 * followed (on disk) by an indirect block.
157 * To store the locations of file's data ext2 uses a data structure common
158 * for UNIX filesystems - tree of pointers anchored in the inode, with
159 * data blocks at leaves and indirect blocks in intermediate nodes.
160 * This function translates the block number into path in that tree -
161 * return value is the path length and @offsets[n] is the offset of
162 * pointer to (n+1)th node in the nth one. If @block is out of range
163 * (negative or too large) warning is printed and zero returned.
165 * Note: function doesn't find node addresses, so no IO is needed. All
166 * we need to know is the capacity of indirect blocks (taken from the
171 * Portability note: the last comparison (check that we fit into triple
172 * indirect block) is spelled differently, because otherwise on an
173 * architecture with 32-bit longs and 8Kb pages we might get into trouble
174 * if our filesystem had 8Kb blocks. We might use long long, but that would
175 * kill us on x86. Oh, well, at least the sign propagation does not matter -
176 * i_block would have to be negative in the very beginning, so we would not
180 static int ext2_block_to_path(struct inode
*inode
,
181 long i_block
, int offsets
[4], int *boundary
)
183 int ptrs
= EXT2_ADDR_PER_BLOCK(inode
->i_sb
);
184 int ptrs_bits
= EXT2_ADDR_PER_BLOCK_BITS(inode
->i_sb
);
185 const long direct_blocks
= EXT2_NDIR_BLOCKS
,
186 indirect_blocks
= ptrs
,
187 double_blocks
= (1 << (ptrs_bits
* 2));
192 ext2_warning (inode
->i_sb
, "ext2_block_to_path", "block < 0");
193 } else if (i_block
< direct_blocks
) {
194 offsets
[n
++] = i_block
;
195 final
= direct_blocks
;
196 } else if ( (i_block
-= direct_blocks
) < indirect_blocks
) {
197 offsets
[n
++] = EXT2_IND_BLOCK
;
198 offsets
[n
++] = i_block
;
200 } else if ((i_block
-= indirect_blocks
) < double_blocks
) {
201 offsets
[n
++] = EXT2_DIND_BLOCK
;
202 offsets
[n
++] = i_block
>> ptrs_bits
;
203 offsets
[n
++] = i_block
& (ptrs
- 1);
205 } else if (((i_block
-= double_blocks
) >> (ptrs_bits
* 2)) < ptrs
) {
206 offsets
[n
++] = EXT2_TIND_BLOCK
;
207 offsets
[n
++] = i_block
>> (ptrs_bits
* 2);
208 offsets
[n
++] = (i_block
>> ptrs_bits
) & (ptrs
- 1);
209 offsets
[n
++] = i_block
& (ptrs
- 1);
212 ext2_warning (inode
->i_sb
, "ext2_block_to_path", "block > big");
215 *boundary
= (i_block
& (ptrs
- 1)) == (final
- 1);
220 * ext2_get_branch - read the chain of indirect blocks leading to data
221 * @inode: inode in question
222 * @depth: depth of the chain (1 - direct pointer, etc.)
223 * @offsets: offsets of pointers in inode/indirect blocks
224 * @chain: place to store the result
225 * @err: here we store the error value
227 * Function fills the array of triples <key, p, bh> and returns %NULL
228 * if everything went OK or the pointer to the last filled triple
229 * (incomplete one) otherwise. Upon the return chain[i].key contains
230 * the number of (i+1)-th block in the chain (as it is stored in memory,
231 * i.e. little-endian 32-bit), chain[i].p contains the address of that
232 * number (it points into struct inode for i==0 and into the bh->b_data
233 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
234 * block for i>0 and NULL for i==0. In other words, it holds the block
235 * numbers of the chain, addresses they were taken from (and where we can
236 * verify that chain did not change) and buffer_heads hosting these
239 * Function stops when it stumbles upon zero pointer (absent block)
240 * (pointer to last triple returned, *@err == 0)
241 * or when it gets an IO error reading an indirect block
242 * (ditto, *@err == -EIO)
243 * or when it notices that chain had been changed while it was reading
244 * (ditto, *@err == -EAGAIN)
245 * or when it reads all @depth-1 indirect blocks successfully and finds
246 * the whole chain, all way to the data (returns %NULL, *err == 0).
248 static Indirect
*ext2_get_branch(struct inode
*inode
,
254 struct super_block
*sb
= inode
->i_sb
;
256 struct buffer_head
*bh
;
259 /* i_data is not going away, no lock needed */
260 add_chain (chain
, NULL
, EXT2_I(inode
)->i_data
+ *offsets
);
264 bh
= sb_bread(sb
, le32_to_cpu(p
->key
));
267 read_lock(&EXT2_I(inode
)->i_meta_lock
);
268 if (!verify_chain(chain
, p
))
270 add_chain(++p
, bh
, (__le32
*)bh
->b_data
+ *++offsets
);
271 read_unlock(&EXT2_I(inode
)->i_meta_lock
);
278 read_unlock(&EXT2_I(inode
)->i_meta_lock
);
289 * ext2_find_near - find a place for allocation with sufficient locality
291 * @ind: descriptor of indirect block.
293 * This function returns the prefered place for block allocation.
294 * It is used when heuristic for sequential allocation fails.
296 * + if there is a block to the left of our position - allocate near it.
297 * + if pointer will live in indirect block - allocate near that block.
298 * + if pointer will live in inode - allocate in the same cylinder group.
300 * In the latter case we colour the starting block by the callers PID to
301 * prevent it from clashing with concurrent allocations for a different inode
302 * in the same block group. The PID is used here so that functionally related
303 * files will be close-by on-disk.
305 * Caller must make sure that @ind is valid and will stay that way.
308 static unsigned long ext2_find_near(struct inode
*inode
, Indirect
*ind
)
310 struct ext2_inode_info
*ei
= EXT2_I(inode
);
311 __le32
*start
= ind
->bh
? (__le32
*) ind
->bh
->b_data
: ei
->i_data
;
313 unsigned long bg_start
;
314 unsigned long colour
;
316 /* Try to find previous block */
317 for (p
= ind
->p
- 1; p
>= start
; p
--)
319 return le32_to_cpu(*p
);
321 /* No such thing, so let's try location of indirect block */
323 return ind
->bh
->b_blocknr
;
326 * It is going to be refered from inode itself? OK, just put it into
327 * the same cylinder group then.
329 bg_start
= (ei
->i_block_group
* EXT2_BLOCKS_PER_GROUP(inode
->i_sb
)) +
330 le32_to_cpu(EXT2_SB(inode
->i_sb
)->s_es
->s_first_data_block
);
331 colour
= (current
->pid
% 16) *
332 (EXT2_BLOCKS_PER_GROUP(inode
->i_sb
) / 16);
333 return bg_start
+ colour
;
337 * ext2_find_goal - find a prefered place for allocation.
339 * @block: block we want
340 * @chain: chain of indirect blocks
341 * @partial: pointer to the last triple within a chain
342 * @goal: place to store the result.
344 * Normally this function find the prefered place for block allocation,
345 * stores it in *@goal and returns zero. If the branch had been changed
346 * under us we return -EAGAIN.
349 static inline int ext2_find_goal(struct inode
*inode
,
355 struct ext2_inode_info
*ei
= EXT2_I(inode
);
356 write_lock(&ei
->i_meta_lock
);
357 if ((block
== ei
->i_next_alloc_block
+ 1) && ei
->i_next_alloc_goal
) {
358 ei
->i_next_alloc_block
++;
359 ei
->i_next_alloc_goal
++;
361 if (verify_chain(chain
, partial
)) {
363 * try the heuristic for sequential allocation,
364 * failing that at least try to get decent locality.
366 if (block
== ei
->i_next_alloc_block
)
367 *goal
= ei
->i_next_alloc_goal
;
369 *goal
= ext2_find_near(inode
, partial
);
370 write_unlock(&ei
->i_meta_lock
);
373 write_unlock(&ei
->i_meta_lock
);
378 * ext2_alloc_branch - allocate and set up a chain of blocks.
380 * @num: depth of the chain (number of blocks to allocate)
381 * @offsets: offsets (in the blocks) to store the pointers to next.
382 * @branch: place to store the chain in.
384 * This function allocates @num blocks, zeroes out all but the last one,
385 * links them into chain and (if we are synchronous) writes them to disk.
386 * In other words, it prepares a branch that can be spliced onto the
387 * inode. It stores the information about that chain in the branch[], in
388 * the same format as ext2_get_branch() would do. We are calling it after
389 * we had read the existing part of chain and partial points to the last
390 * triple of that (one with zero ->key). Upon the exit we have the same
391 * picture as after the successful ext2_get_block(), excpet that in one
392 * place chain is disconnected - *branch->p is still zero (we did not
393 * set the last link), but branch->key contains the number that should
394 * be placed into *branch->p to fill that gap.
396 * If allocation fails we free all blocks we've allocated (and forget
397 * their buffer_heads) and return the error value the from failed
398 * ext2_alloc_block() (normally -ENOSPC). Otherwise we set the chain
399 * as described above and return 0.
402 static int ext2_alloc_branch(struct inode
*inode
,
408 int blocksize
= inode
->i_sb
->s_blocksize
;
412 int parent
= ext2_alloc_block(inode
, goal
, &err
);
414 branch
[0].key
= cpu_to_le32(parent
);
415 if (parent
) for (n
= 1; n
< num
; n
++) {
416 struct buffer_head
*bh
;
417 /* Allocate the next block */
418 int nr
= ext2_alloc_block(inode
, parent
, &err
);
421 branch
[n
].key
= cpu_to_le32(nr
);
423 * Get buffer_head for parent block, zero it out and set
424 * the pointer to new one, then send parent to disk.
426 bh
= sb_getblk(inode
->i_sb
, parent
);
428 memset(bh
->b_data
, 0, blocksize
);
430 branch
[n
].p
= (__le32
*) bh
->b_data
+ offsets
[n
];
431 *branch
[n
].p
= branch
[n
].key
;
432 set_buffer_uptodate(bh
);
434 mark_buffer_dirty_inode(bh
, inode
);
435 /* We used to sync bh here if IS_SYNC(inode).
436 * But we now rely upon generic_osync_inode()
437 * and b_inode_buffers. But not for directories.
439 if (S_ISDIR(inode
->i_mode
) && IS_DIRSYNC(inode
))
440 sync_dirty_buffer(bh
);
446 /* Allocation failed, free what we already allocated */
447 for (i
= 1; i
< n
; i
++)
448 bforget(branch
[i
].bh
);
449 for (i
= 0; i
< n
; i
++)
450 ext2_free_blocks(inode
, le32_to_cpu(branch
[i
].key
), 1);
455 * ext2_splice_branch - splice the allocated branch onto inode.
457 * @block: (logical) number of block we are adding
458 * @chain: chain of indirect blocks (with a missing link - see
460 * @where: location of missing link
461 * @num: number of blocks we are adding
463 * This function verifies that chain (up to the missing link) had not
464 * changed, fills the missing link and does all housekeeping needed in
465 * inode (->i_blocks, etc.). In case of success we end up with the full
466 * chain to new block and return 0. Otherwise (== chain had been changed)
467 * we free the new blocks (forgetting their buffer_heads, indeed) and
471 static inline int ext2_splice_branch(struct inode
*inode
,
477 struct ext2_inode_info
*ei
= EXT2_I(inode
);
480 /* Verify that place we are splicing to is still there and vacant */
482 write_lock(&ei
->i_meta_lock
);
483 if (!verify_chain(chain
, where
-1) || *where
->p
)
488 *where
->p
= where
->key
;
489 ei
->i_next_alloc_block
= block
;
490 ei
->i_next_alloc_goal
= le32_to_cpu(where
[num
-1].key
);
492 write_unlock(&ei
->i_meta_lock
);
494 /* We are done with atomic stuff, now do the rest of housekeeping */
496 inode
->i_ctime
= CURRENT_TIME_SEC
;
498 /* had we spliced it onto indirect block? */
500 mark_buffer_dirty_inode(where
->bh
, inode
);
502 mark_inode_dirty(inode
);
506 write_unlock(&ei
->i_meta_lock
);
507 for (i
= 1; i
< num
; i
++)
508 bforget(where
[i
].bh
);
509 for (i
= 0; i
< num
; i
++)
510 ext2_free_blocks(inode
, le32_to_cpu(where
[i
].key
), 1);
515 * Allocation strategy is simple: if we have to allocate something, we will
516 * have to go the whole way to leaf. So let's do it before attaching anything
517 * to tree, set linkage between the newborn blocks, write them if sync is
518 * required, recheck the path, free and repeat if check fails, otherwise
519 * set the last missing link (that will protect us from any truncate-generated
520 * removals - all blocks on the path are immune now) and possibly force the
521 * write on the parent block.
522 * That has a nice additional property: no special recovery from the failed
523 * allocations is needed - we simply release blocks and do not touch anything
524 * reachable from inode.
527 int ext2_get_block(struct inode
*inode
, sector_t iblock
, struct buffer_head
*bh_result
, int create
)
536 int depth
= ext2_block_to_path(inode
, iblock
, offsets
, &boundary
);
542 partial
= ext2_get_branch(inode
, depth
, offsets
, chain
, &err
);
544 /* Simplest case - block found, no allocation needed */
547 map_bh(bh_result
, inode
->i_sb
, le32_to_cpu(chain
[depth
-1].key
));
549 set_buffer_boundary(bh_result
);
550 /* Clean up and exit */
551 partial
= chain
+depth
-1; /* the whole chain */
555 /* Next simple case - plain lookup or failed read of indirect block */
556 if (!create
|| err
== -EIO
) {
558 while (partial
> chain
) {
567 * Indirect block might be removed by truncate while we were
568 * reading it. Handling of that case (forget what we've got and
569 * reread) is taken out of the main path.
575 if (ext2_find_goal(inode
, iblock
, chain
, partial
, &goal
) < 0)
578 left
= (chain
+ depth
) - partial
;
579 err
= ext2_alloc_branch(inode
, left
, goal
,
580 offsets
+(partial
-chain
), partial
);
584 if (ext2_splice_branch(inode
, iblock
, chain
, partial
, left
) < 0)
587 set_buffer_new(bh_result
);
591 while (partial
> chain
) {
598 static int ext2_writepage(struct page
*page
, struct writeback_control
*wbc
)
600 return block_write_full_page(page
, ext2_get_block
, wbc
);
603 static int ext2_readpage(struct file
*file
, struct page
*page
)
605 return mpage_readpage(page
, ext2_get_block
);
609 ext2_readpages(struct file
*file
, struct address_space
*mapping
,
610 struct list_head
*pages
, unsigned nr_pages
)
612 return mpage_readpages(mapping
, pages
, nr_pages
, ext2_get_block
);
616 ext2_prepare_write(struct file
*file
, struct page
*page
,
617 unsigned from
, unsigned to
)
619 return block_prepare_write(page
,from
,to
,ext2_get_block
);
623 ext2_nobh_prepare_write(struct file
*file
, struct page
*page
,
624 unsigned from
, unsigned to
)
626 return nobh_prepare_write(page
,from
,to
,ext2_get_block
);
629 static int ext2_nobh_writepage(struct page
*page
,
630 struct writeback_control
*wbc
)
632 return nobh_writepage(page
, ext2_get_block
, wbc
);
635 static sector_t
ext2_bmap(struct address_space
*mapping
, sector_t block
)
637 return generic_block_bmap(mapping
,block
,ext2_get_block
);
641 ext2_get_blocks(struct inode
*inode
, sector_t iblock
, unsigned long max_blocks
,
642 struct buffer_head
*bh_result
, int create
)
646 ret
= ext2_get_block(inode
, iblock
, bh_result
, create
);
648 bh_result
->b_size
= (1 << inode
->i_blkbits
);
653 ext2_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
654 loff_t offset
, unsigned long nr_segs
)
656 struct file
*file
= iocb
->ki_filp
;
657 struct inode
*inode
= file
->f_mapping
->host
;
659 return blockdev_direct_IO(rw
, iocb
, inode
, inode
->i_sb
->s_bdev
, iov
,
660 offset
, nr_segs
, ext2_get_blocks
, NULL
);
664 ext2_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
666 return mpage_writepages(mapping
, wbc
, ext2_get_block
);
669 struct address_space_operations ext2_aops
= {
670 .readpage
= ext2_readpage
,
671 .readpages
= ext2_readpages
,
672 .writepage
= ext2_writepage
,
673 .sync_page
= block_sync_page
,
674 .prepare_write
= ext2_prepare_write
,
675 .commit_write
= generic_commit_write
,
677 .direct_IO
= ext2_direct_IO
,
678 .writepages
= ext2_writepages
,
681 struct address_space_operations ext2_nobh_aops
= {
682 .readpage
= ext2_readpage
,
683 .readpages
= ext2_readpages
,
684 .writepage
= ext2_nobh_writepage
,
685 .sync_page
= block_sync_page
,
686 .prepare_write
= ext2_nobh_prepare_write
,
687 .commit_write
= nobh_commit_write
,
689 .direct_IO
= ext2_direct_IO
,
690 .writepages
= ext2_writepages
,
694 * Probably it should be a library function... search for first non-zero word
695 * or memcmp with zero_page, whatever is better for particular architecture.
698 static inline int all_zeroes(__le32
*p
, __le32
*q
)
707 * ext2_find_shared - find the indirect blocks for partial truncation.
708 * @inode: inode in question
709 * @depth: depth of the affected branch
710 * @offsets: offsets of pointers in that branch (see ext2_block_to_path)
711 * @chain: place to store the pointers to partial indirect blocks
712 * @top: place to the (detached) top of branch
714 * This is a helper function used by ext2_truncate().
716 * When we do truncate() we may have to clean the ends of several indirect
717 * blocks but leave the blocks themselves alive. Block is partially
718 * truncated if some data below the new i_size is refered from it (and
719 * it is on the path to the first completely truncated data block, indeed).
720 * We have to free the top of that path along with everything to the right
721 * of the path. Since no allocation past the truncation point is possible
722 * until ext2_truncate() finishes, we may safely do the latter, but top
723 * of branch may require special attention - pageout below the truncation
724 * point might try to populate it.
726 * We atomically detach the top of branch from the tree, store the block
727 * number of its root in *@top, pointers to buffer_heads of partially
728 * truncated blocks - in @chain[].bh and pointers to their last elements
729 * that should not be removed - in @chain[].p. Return value is the pointer
730 * to last filled element of @chain.
732 * The work left to caller to do the actual freeing of subtrees:
733 * a) free the subtree starting from *@top
734 * b) free the subtrees whose roots are stored in
735 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
736 * c) free the subtrees growing from the inode past the @chain[0].p
737 * (no partially truncated stuff there).
740 static Indirect
*ext2_find_shared(struct inode
*inode
,
746 Indirect
*partial
, *p
;
750 for (k
= depth
; k
> 1 && !offsets
[k
-1]; k
--)
752 partial
= ext2_get_branch(inode
, k
, offsets
, chain
, &err
);
754 partial
= chain
+ k
-1;
756 * If the branch acquired continuation since we've looked at it -
757 * fine, it should all survive and (new) top doesn't belong to us.
759 write_lock(&EXT2_I(inode
)->i_meta_lock
);
760 if (!partial
->key
&& *partial
->p
) {
761 write_unlock(&EXT2_I(inode
)->i_meta_lock
);
764 for (p
=partial
; p
>chain
&& all_zeroes((__le32
*)p
->bh
->b_data
,p
->p
); p
--)
767 * OK, we've found the last block that must survive. The rest of our
768 * branch should be detached before unlocking. However, if that rest
769 * of branch is all ours and does not grow immediately from the inode
770 * it's easier to cheat and just decrement partial->p.
772 if (p
== chain
+ k
- 1 && p
> chain
) {
778 write_unlock(&EXT2_I(inode
)->i_meta_lock
);
790 * ext2_free_data - free a list of data blocks
791 * @inode: inode we are dealing with
792 * @p: array of block numbers
793 * @q: points immediately past the end of array
795 * We are freeing all blocks refered from that array (numbers are
796 * stored as little-endian 32-bit) and updating @inode->i_blocks
799 static inline void ext2_free_data(struct inode
*inode
, __le32
*p
, __le32
*q
)
801 unsigned long block_to_free
= 0, count
= 0;
804 for ( ; p
< q
; p
++) {
805 nr
= le32_to_cpu(*p
);
808 /* accumulate blocks to free if they're contiguous */
811 else if (block_to_free
== nr
- count
)
814 mark_inode_dirty(inode
);
815 ext2_free_blocks (inode
, block_to_free
, count
);
823 mark_inode_dirty(inode
);
824 ext2_free_blocks (inode
, block_to_free
, count
);
829 * ext2_free_branches - free an array of branches
830 * @inode: inode we are dealing with
831 * @p: array of block numbers
832 * @q: pointer immediately past the end of array
833 * @depth: depth of the branches to free
835 * We are freeing all blocks refered from these branches (numbers are
836 * stored as little-endian 32-bit) and updating @inode->i_blocks
839 static void ext2_free_branches(struct inode
*inode
, __le32
*p
, __le32
*q
, int depth
)
841 struct buffer_head
* bh
;
845 int addr_per_block
= EXT2_ADDR_PER_BLOCK(inode
->i_sb
);
846 for ( ; p
< q
; p
++) {
847 nr
= le32_to_cpu(*p
);
851 bh
= sb_bread(inode
->i_sb
, nr
);
853 * A read failure? Report error and clear slot
857 ext2_error(inode
->i_sb
, "ext2_free_branches",
858 "Read failure, inode=%ld, block=%ld",
862 ext2_free_branches(inode
,
864 (__le32
*)bh
->b_data
+ addr_per_block
,
867 ext2_free_blocks(inode
, nr
, 1);
868 mark_inode_dirty(inode
);
871 ext2_free_data(inode
, p
, q
);
874 void ext2_truncate (struct inode
* inode
)
876 __le32
*i_data
= EXT2_I(inode
)->i_data
;
877 int addr_per_block
= EXT2_ADDR_PER_BLOCK(inode
->i_sb
);
886 if (!(S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
887 S_ISLNK(inode
->i_mode
)))
889 if (ext2_inode_is_fast_symlink(inode
))
891 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
894 ext2_discard_prealloc(inode
);
896 blocksize
= inode
->i_sb
->s_blocksize
;
897 iblock
= (inode
->i_size
+ blocksize
-1)
898 >> EXT2_BLOCK_SIZE_BITS(inode
->i_sb
);
900 if (test_opt(inode
->i_sb
, NOBH
))
901 nobh_truncate_page(inode
->i_mapping
, inode
->i_size
);
903 block_truncate_page(inode
->i_mapping
,
904 inode
->i_size
, ext2_get_block
);
906 n
= ext2_block_to_path(inode
, iblock
, offsets
, NULL
);
911 ext2_free_data(inode
, i_data
+offsets
[0],
912 i_data
+ EXT2_NDIR_BLOCKS
);
916 partial
= ext2_find_shared(inode
, n
, offsets
, chain
, &nr
);
917 /* Kill the top of shared branch (already detached) */
919 if (partial
== chain
)
920 mark_inode_dirty(inode
);
922 mark_buffer_dirty_inode(partial
->bh
, inode
);
923 ext2_free_branches(inode
, &nr
, &nr
+1, (chain
+n
-1) - partial
);
925 /* Clear the ends of indirect blocks on the shared branch */
926 while (partial
> chain
) {
927 ext2_free_branches(inode
,
929 (__le32
*)partial
->bh
->b_data
+addr_per_block
,
930 (chain
+n
-1) - partial
);
931 mark_buffer_dirty_inode(partial
->bh
, inode
);
932 brelse (partial
->bh
);
936 /* Kill the remaining (whole) subtrees */
937 switch (offsets
[0]) {
939 nr
= i_data
[EXT2_IND_BLOCK
];
941 i_data
[EXT2_IND_BLOCK
] = 0;
942 mark_inode_dirty(inode
);
943 ext2_free_branches(inode
, &nr
, &nr
+1, 1);
946 nr
= i_data
[EXT2_DIND_BLOCK
];
948 i_data
[EXT2_DIND_BLOCK
] = 0;
949 mark_inode_dirty(inode
);
950 ext2_free_branches(inode
, &nr
, &nr
+1, 2);
952 case EXT2_DIND_BLOCK
:
953 nr
= i_data
[EXT2_TIND_BLOCK
];
955 i_data
[EXT2_TIND_BLOCK
] = 0;
956 mark_inode_dirty(inode
);
957 ext2_free_branches(inode
, &nr
, &nr
+1, 3);
959 case EXT2_TIND_BLOCK
:
962 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME_SEC
;
963 if (inode_needs_sync(inode
)) {
964 sync_mapping_buffers(inode
->i_mapping
);
965 ext2_sync_inode (inode
);
967 mark_inode_dirty(inode
);
971 static struct ext2_inode
*ext2_get_inode(struct super_block
*sb
, ino_t ino
,
972 struct buffer_head
**p
)
974 struct buffer_head
* bh
;
975 unsigned long block_group
;
977 unsigned long offset
;
978 struct ext2_group_desc
* gdp
;
981 if ((ino
!= EXT2_ROOT_INO
&& ino
< EXT2_FIRST_INO(sb
)) ||
982 ino
> le32_to_cpu(EXT2_SB(sb
)->s_es
->s_inodes_count
))
985 block_group
= (ino
- 1) / EXT2_INODES_PER_GROUP(sb
);
986 gdp
= ext2_get_group_desc(sb
, block_group
, &bh
);
990 * Figure out the offset within the block group inode table
992 offset
= ((ino
- 1) % EXT2_INODES_PER_GROUP(sb
)) * EXT2_INODE_SIZE(sb
);
993 block
= le32_to_cpu(gdp
->bg_inode_table
) +
994 (offset
>> EXT2_BLOCK_SIZE_BITS(sb
));
995 if (!(bh
= sb_bread(sb
, block
)))
999 offset
&= (EXT2_BLOCK_SIZE(sb
) - 1);
1000 return (struct ext2_inode
*) (bh
->b_data
+ offset
);
1003 ext2_error(sb
, "ext2_get_inode", "bad inode number: %lu",
1004 (unsigned long) ino
);
1005 return ERR_PTR(-EINVAL
);
1007 ext2_error(sb
, "ext2_get_inode",
1008 "unable to read inode block - inode=%lu, block=%lu",
1009 (unsigned long) ino
, block
);
1011 return ERR_PTR(-EIO
);
1014 void ext2_set_inode_flags(struct inode
*inode
)
1016 unsigned int flags
= EXT2_I(inode
)->i_flags
;
1018 inode
->i_flags
&= ~(S_SYNC
|S_APPEND
|S_IMMUTABLE
|S_NOATIME
|S_DIRSYNC
);
1019 if (flags
& EXT2_SYNC_FL
)
1020 inode
->i_flags
|= S_SYNC
;
1021 if (flags
& EXT2_APPEND_FL
)
1022 inode
->i_flags
|= S_APPEND
;
1023 if (flags
& EXT2_IMMUTABLE_FL
)
1024 inode
->i_flags
|= S_IMMUTABLE
;
1025 if (flags
& EXT2_NOATIME_FL
)
1026 inode
->i_flags
|= S_NOATIME
;
1027 if (flags
& EXT2_DIRSYNC_FL
)
1028 inode
->i_flags
|= S_DIRSYNC
;
1031 void ext2_read_inode (struct inode
* inode
)
1033 struct ext2_inode_info
*ei
= EXT2_I(inode
);
1034 ino_t ino
= inode
->i_ino
;
1035 struct buffer_head
* bh
;
1036 struct ext2_inode
* raw_inode
= ext2_get_inode(inode
->i_sb
, ino
, &bh
);
1039 #ifdef CONFIG_EXT2_FS_POSIX_ACL
1040 ei
->i_acl
= EXT2_ACL_NOT_CACHED
;
1041 ei
->i_default_acl
= EXT2_ACL_NOT_CACHED
;
1043 if (IS_ERR(raw_inode
))
1046 inode
->i_mode
= le16_to_cpu(raw_inode
->i_mode
);
1047 inode
->i_uid
= (uid_t
)le16_to_cpu(raw_inode
->i_uid_low
);
1048 inode
->i_gid
= (gid_t
)le16_to_cpu(raw_inode
->i_gid_low
);
1049 if (!(test_opt (inode
->i_sb
, NO_UID32
))) {
1050 inode
->i_uid
|= le16_to_cpu(raw_inode
->i_uid_high
) << 16;
1051 inode
->i_gid
|= le16_to_cpu(raw_inode
->i_gid_high
) << 16;
1053 inode
->i_nlink
= le16_to_cpu(raw_inode
->i_links_count
);
1054 inode
->i_size
= le32_to_cpu(raw_inode
->i_size
);
1055 inode
->i_atime
.tv_sec
= le32_to_cpu(raw_inode
->i_atime
);
1056 inode
->i_ctime
.tv_sec
= le32_to_cpu(raw_inode
->i_ctime
);
1057 inode
->i_mtime
.tv_sec
= le32_to_cpu(raw_inode
->i_mtime
);
1058 inode
->i_atime
.tv_nsec
= inode
->i_mtime
.tv_nsec
= inode
->i_ctime
.tv_nsec
= 0;
1059 ei
->i_dtime
= le32_to_cpu(raw_inode
->i_dtime
);
1060 /* We now have enough fields to check if the inode was active or not.
1061 * This is needed because nfsd might try to access dead inodes
1062 * the test is that same one that e2fsck uses
1063 * NeilBrown 1999oct15
1065 if (inode
->i_nlink
== 0 && (inode
->i_mode
== 0 || ei
->i_dtime
)) {
1066 /* this inode is deleted */
1070 inode
->i_blksize
= PAGE_SIZE
; /* This is the optimal IO size (for stat), not the fs block size */
1071 inode
->i_blocks
= le32_to_cpu(raw_inode
->i_blocks
);
1072 ei
->i_flags
= le32_to_cpu(raw_inode
->i_flags
);
1073 ei
->i_faddr
= le32_to_cpu(raw_inode
->i_faddr
);
1074 ei
->i_frag_no
= raw_inode
->i_frag
;
1075 ei
->i_frag_size
= raw_inode
->i_fsize
;
1076 ei
->i_file_acl
= le32_to_cpu(raw_inode
->i_file_acl
);
1078 if (S_ISREG(inode
->i_mode
))
1079 inode
->i_size
|= ((__u64
)le32_to_cpu(raw_inode
->i_size_high
)) << 32;
1081 ei
->i_dir_acl
= le32_to_cpu(raw_inode
->i_dir_acl
);
1083 inode
->i_generation
= le32_to_cpu(raw_inode
->i_generation
);
1085 ei
->i_next_alloc_block
= 0;
1086 ei
->i_next_alloc_goal
= 0;
1087 ei
->i_prealloc_count
= 0;
1088 ei
->i_block_group
= (ino
- 1) / EXT2_INODES_PER_GROUP(inode
->i_sb
);
1089 ei
->i_dir_start_lookup
= 0;
1092 * NOTE! The in-memory inode i_data array is in little-endian order
1093 * even on big-endian machines: we do NOT byteswap the block numbers!
1095 for (n
= 0; n
< EXT2_N_BLOCKS
; n
++)
1096 ei
->i_data
[n
] = raw_inode
->i_block
[n
];
1098 if (S_ISREG(inode
->i_mode
)) {
1099 inode
->i_op
= &ext2_file_inode_operations
;
1100 inode
->i_fop
= &ext2_file_operations
;
1101 if (test_opt(inode
->i_sb
, NOBH
))
1102 inode
->i_mapping
->a_ops
= &ext2_nobh_aops
;
1104 inode
->i_mapping
->a_ops
= &ext2_aops
;
1105 } else if (S_ISDIR(inode
->i_mode
)) {
1106 inode
->i_op
= &ext2_dir_inode_operations
;
1107 inode
->i_fop
= &ext2_dir_operations
;
1108 if (test_opt(inode
->i_sb
, NOBH
))
1109 inode
->i_mapping
->a_ops
= &ext2_nobh_aops
;
1111 inode
->i_mapping
->a_ops
= &ext2_aops
;
1112 } else if (S_ISLNK(inode
->i_mode
)) {
1113 if (ext2_inode_is_fast_symlink(inode
))
1114 inode
->i_op
= &ext2_fast_symlink_inode_operations
;
1116 inode
->i_op
= &ext2_symlink_inode_operations
;
1117 if (test_opt(inode
->i_sb
, NOBH
))
1118 inode
->i_mapping
->a_ops
= &ext2_nobh_aops
;
1120 inode
->i_mapping
->a_ops
= &ext2_aops
;
1123 inode
->i_op
= &ext2_special_inode_operations
;
1124 if (raw_inode
->i_block
[0])
1125 init_special_inode(inode
, inode
->i_mode
,
1126 old_decode_dev(le32_to_cpu(raw_inode
->i_block
[0])));
1128 init_special_inode(inode
, inode
->i_mode
,
1129 new_decode_dev(le32_to_cpu(raw_inode
->i_block
[1])));
1132 ext2_set_inode_flags(inode
);
1136 make_bad_inode(inode
);
1140 static int ext2_update_inode(struct inode
* inode
, int do_sync
)
1142 struct ext2_inode_info
*ei
= EXT2_I(inode
);
1143 struct super_block
*sb
= inode
->i_sb
;
1144 ino_t ino
= inode
->i_ino
;
1145 uid_t uid
= inode
->i_uid
;
1146 gid_t gid
= inode
->i_gid
;
1147 struct buffer_head
* bh
;
1148 struct ext2_inode
* raw_inode
= ext2_get_inode(sb
, ino
, &bh
);
1152 if (IS_ERR(raw_inode
))
1155 /* For fields not not tracking in the in-memory inode,
1156 * initialise them to zero for new inodes. */
1157 if (ei
->i_state
& EXT2_STATE_NEW
)
1158 memset(raw_inode
, 0, EXT2_SB(sb
)->s_inode_size
);
1160 raw_inode
->i_mode
= cpu_to_le16(inode
->i_mode
);
1161 if (!(test_opt(sb
, NO_UID32
))) {
1162 raw_inode
->i_uid_low
= cpu_to_le16(low_16_bits(uid
));
1163 raw_inode
->i_gid_low
= cpu_to_le16(low_16_bits(gid
));
1165 * Fix up interoperability with old kernels. Otherwise, old inodes get
1166 * re-used with the upper 16 bits of the uid/gid intact
1169 raw_inode
->i_uid_high
= cpu_to_le16(high_16_bits(uid
));
1170 raw_inode
->i_gid_high
= cpu_to_le16(high_16_bits(gid
));
1172 raw_inode
->i_uid_high
= 0;
1173 raw_inode
->i_gid_high
= 0;
1176 raw_inode
->i_uid_low
= cpu_to_le16(fs_high2lowuid(uid
));
1177 raw_inode
->i_gid_low
= cpu_to_le16(fs_high2lowgid(gid
));
1178 raw_inode
->i_uid_high
= 0;
1179 raw_inode
->i_gid_high
= 0;
1181 raw_inode
->i_links_count
= cpu_to_le16(inode
->i_nlink
);
1182 raw_inode
->i_size
= cpu_to_le32(inode
->i_size
);
1183 raw_inode
->i_atime
= cpu_to_le32(inode
->i_atime
.tv_sec
);
1184 raw_inode
->i_ctime
= cpu_to_le32(inode
->i_ctime
.tv_sec
);
1185 raw_inode
->i_mtime
= cpu_to_le32(inode
->i_mtime
.tv_sec
);
1187 raw_inode
->i_blocks
= cpu_to_le32(inode
->i_blocks
);
1188 raw_inode
->i_dtime
= cpu_to_le32(ei
->i_dtime
);
1189 raw_inode
->i_flags
= cpu_to_le32(ei
->i_flags
);
1190 raw_inode
->i_faddr
= cpu_to_le32(ei
->i_faddr
);
1191 raw_inode
->i_frag
= ei
->i_frag_no
;
1192 raw_inode
->i_fsize
= ei
->i_frag_size
;
1193 raw_inode
->i_file_acl
= cpu_to_le32(ei
->i_file_acl
);
1194 if (!S_ISREG(inode
->i_mode
))
1195 raw_inode
->i_dir_acl
= cpu_to_le32(ei
->i_dir_acl
);
1197 raw_inode
->i_size_high
= cpu_to_le32(inode
->i_size
>> 32);
1198 if (inode
->i_size
> 0x7fffffffULL
) {
1199 if (!EXT2_HAS_RO_COMPAT_FEATURE(sb
,
1200 EXT2_FEATURE_RO_COMPAT_LARGE_FILE
) ||
1201 EXT2_SB(sb
)->s_es
->s_rev_level
==
1202 cpu_to_le32(EXT2_GOOD_OLD_REV
)) {
1203 /* If this is the first large file
1204 * created, add a flag to the superblock.
1207 ext2_update_dynamic_rev(sb
);
1208 EXT2_SET_RO_COMPAT_FEATURE(sb
,
1209 EXT2_FEATURE_RO_COMPAT_LARGE_FILE
);
1211 ext2_write_super(sb
);
1216 raw_inode
->i_generation
= cpu_to_le32(inode
->i_generation
);
1217 if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
)) {
1218 if (old_valid_dev(inode
->i_rdev
)) {
1219 raw_inode
->i_block
[0] =
1220 cpu_to_le32(old_encode_dev(inode
->i_rdev
));
1221 raw_inode
->i_block
[1] = 0;
1223 raw_inode
->i_block
[0] = 0;
1224 raw_inode
->i_block
[1] =
1225 cpu_to_le32(new_encode_dev(inode
->i_rdev
));
1226 raw_inode
->i_block
[2] = 0;
1228 } else for (n
= 0; n
< EXT2_N_BLOCKS
; n
++)
1229 raw_inode
->i_block
[n
] = ei
->i_data
[n
];
1230 mark_buffer_dirty(bh
);
1232 sync_dirty_buffer(bh
);
1233 if (buffer_req(bh
) && !buffer_uptodate(bh
)) {
1234 printk ("IO error syncing ext2 inode [%s:%08lx]\n",
1235 sb
->s_id
, (unsigned long) ino
);
1239 ei
->i_state
&= ~EXT2_STATE_NEW
;
1244 int ext2_write_inode(struct inode
*inode
, int wait
)
1246 return ext2_update_inode(inode
, wait
);
1249 int ext2_sync_inode(struct inode
*inode
)
1251 struct writeback_control wbc
= {
1252 .sync_mode
= WB_SYNC_ALL
,
1253 .nr_to_write
= 0, /* sys_fsync did this */
1255 return sync_inode(inode
, &wbc
);
1258 int ext2_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
1260 struct inode
*inode
= dentry
->d_inode
;
1263 error
= inode_change_ok(inode
, iattr
);
1266 if ((iattr
->ia_valid
& ATTR_UID
&& iattr
->ia_uid
!= inode
->i_uid
) ||
1267 (iattr
->ia_valid
& ATTR_GID
&& iattr
->ia_gid
!= inode
->i_gid
)) {
1268 error
= DQUOT_TRANSFER(inode
, iattr
) ? -EDQUOT
: 0;
1272 error
= inode_setattr(inode
, iattr
);
1273 if (!error
&& (iattr
->ia_valid
& ATTR_MODE
))
1274 error
= ext2_acl_chmod(inode
);