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>
34 #include <linux/vserver/xid.h>
38 MODULE_AUTHOR("Remy Card and others");
39 MODULE_DESCRIPTION("Second Extended Filesystem");
40 MODULE_LICENSE("GPL");
42 static int ext2_update_inode(struct inode * inode, int do_sync);
45 * Test whether an inode is a fast symlink.
47 static inline int ext2_inode_is_fast_symlink(struct inode *inode)
49 int ea_blocks = EXT2_I(inode)->i_file_acl ?
50 (inode->i_sb->s_blocksize >> 9) : 0;
52 return (S_ISLNK(inode->i_mode) &&
53 inode->i_blocks - ea_blocks == 0);
57 * Called at each iput().
59 * The inode may be "bad" if ext2_read_inode() saw an error from
60 * ext2_get_inode(), so we need to check that to avoid freeing random disk
63 void ext2_put_inode(struct inode *inode)
65 if (!is_bad_inode(inode))
66 ext2_discard_prealloc(inode);
69 static void ext2_truncate_nocheck (struct inode * inode);
72 * Called at the last iput() if i_nlink is zero.
74 void ext2_delete_inode (struct inode * inode)
76 if (is_bad_inode(inode))
78 EXT2_I(inode)->i_dtime = get_seconds();
79 mark_inode_dirty(inode);
80 ext2_update_inode(inode, inode_needs_sync(inode));
84 ext2_truncate_nocheck(inode);
85 ext2_free_inode (inode);
89 clear_inode(inode); /* We must guarantee clearing of inode... */
92 void ext2_discard_prealloc (struct inode * inode)
94 #ifdef EXT2_PREALLOCATE
95 struct ext2_inode_info *ei = EXT2_I(inode);
96 write_lock(&ei->i_meta_lock);
97 if (ei->i_prealloc_count) {
98 unsigned short total = ei->i_prealloc_count;
99 unsigned long block = ei->i_prealloc_block;
100 ei->i_prealloc_count = 0;
101 ei->i_prealloc_block = 0;
102 write_unlock(&ei->i_meta_lock);
103 ext2_free_blocks (inode, block, total);
106 write_unlock(&ei->i_meta_lock);
110 static int ext2_alloc_block (struct inode * inode, unsigned long goal, int *err)
113 static unsigned long alloc_hits, alloc_attempts;
115 unsigned long result;
118 #ifdef EXT2_PREALLOCATE
119 struct ext2_inode_info *ei = EXT2_I(inode);
120 write_lock(&ei->i_meta_lock);
121 if (ei->i_prealloc_count &&
122 (goal == ei->i_prealloc_block || goal + 1 == ei->i_prealloc_block))
124 result = ei->i_prealloc_block++;
125 ei->i_prealloc_count--;
126 write_unlock(&ei->i_meta_lock);
127 ext2_debug ("preallocation hit (%lu/%lu).\n",
128 ++alloc_hits, ++alloc_attempts);
130 write_unlock(&ei->i_meta_lock);
131 ext2_discard_prealloc (inode);
132 ext2_debug ("preallocation miss (%lu/%lu).\n",
133 alloc_hits, ++alloc_attempts);
134 if (S_ISREG(inode->i_mode))
135 result = ext2_new_block (inode, goal,
136 &ei->i_prealloc_count,
137 &ei->i_prealloc_block, err);
139 result = ext2_new_block (inode, goal, 0, 0, err);
142 result = ext2_new_block (inode, goal, 0, 0, err);
150 struct buffer_head *bh;
153 static inline void add_chain(Indirect *p, struct buffer_head *bh, u32 *v)
155 p->key = *(p->p = v);
159 static inline int verify_chain(Indirect *from, Indirect *to)
161 while (from <= to && from->key == *from->p)
167 * ext2_block_to_path - parse the block number into array of offsets
168 * @inode: inode in question (we are only interested in its superblock)
169 * @i_block: block number to be parsed
170 * @offsets: array to store the offsets in
171 * @boundary: set this non-zero if the referred-to block is likely to be
172 * followed (on disk) by an indirect block.
173 * To store the locations of file's data ext2 uses a data structure common
174 * for UNIX filesystems - tree of pointers anchored in the inode, with
175 * data blocks at leaves and indirect blocks in intermediate nodes.
176 * This function translates the block number into path in that tree -
177 * return value is the path length and @offsets[n] is the offset of
178 * pointer to (n+1)th node in the nth one. If @block is out of range
179 * (negative or too large) warning is printed and zero returned.
181 * Note: function doesn't find node addresses, so no IO is needed. All
182 * we need to know is the capacity of indirect blocks (taken from the
187 * Portability note: the last comparison (check that we fit into triple
188 * indirect block) is spelled differently, because otherwise on an
189 * architecture with 32-bit longs and 8Kb pages we might get into trouble
190 * if our filesystem had 8Kb blocks. We might use long long, but that would
191 * kill us on x86. Oh, well, at least the sign propagation does not matter -
192 * i_block would have to be negative in the very beginning, so we would not
196 static int ext2_block_to_path(struct inode *inode,
197 long i_block, int offsets[4], int *boundary)
199 int ptrs = EXT2_ADDR_PER_BLOCK(inode->i_sb);
200 int ptrs_bits = EXT2_ADDR_PER_BLOCK_BITS(inode->i_sb);
201 const long direct_blocks = EXT2_NDIR_BLOCKS,
202 indirect_blocks = ptrs,
203 double_blocks = (1 << (ptrs_bits * 2));
208 ext2_warning (inode->i_sb, "ext2_block_to_path", "block < 0");
209 } else if (i_block < direct_blocks) {
210 offsets[n++] = i_block;
211 final = direct_blocks;
212 } else if ( (i_block -= direct_blocks) < indirect_blocks) {
213 offsets[n++] = EXT2_IND_BLOCK;
214 offsets[n++] = i_block;
216 } else if ((i_block -= indirect_blocks) < double_blocks) {
217 offsets[n++] = EXT2_DIND_BLOCK;
218 offsets[n++] = i_block >> ptrs_bits;
219 offsets[n++] = i_block & (ptrs - 1);
221 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
222 offsets[n++] = EXT2_TIND_BLOCK;
223 offsets[n++] = i_block >> (ptrs_bits * 2);
224 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
225 offsets[n++] = i_block & (ptrs - 1);
228 ext2_warning (inode->i_sb, "ext2_block_to_path", "block > big");
231 *boundary = (i_block & (ptrs - 1)) == (final - 1);
236 * ext2_get_branch - read the chain of indirect blocks leading to data
237 * @inode: inode in question
238 * @depth: depth of the chain (1 - direct pointer, etc.)
239 * @offsets: offsets of pointers in inode/indirect blocks
240 * @chain: place to store the result
241 * @err: here we store the error value
243 * Function fills the array of triples <key, p, bh> and returns %NULL
244 * if everything went OK or the pointer to the last filled triple
245 * (incomplete one) otherwise. Upon the return chain[i].key contains
246 * the number of (i+1)-th block in the chain (as it is stored in memory,
247 * i.e. little-endian 32-bit), chain[i].p contains the address of that
248 * number (it points into struct inode for i==0 and into the bh->b_data
249 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
250 * block for i>0 and NULL for i==0. In other words, it holds the block
251 * numbers of the chain, addresses they were taken from (and where we can
252 * verify that chain did not change) and buffer_heads hosting these
255 * Function stops when it stumbles upon zero pointer (absent block)
256 * (pointer to last triple returned, *@err == 0)
257 * or when it gets an IO error reading an indirect block
258 * (ditto, *@err == -EIO)
259 * or when it notices that chain had been changed while it was reading
260 * (ditto, *@err == -EAGAIN)
261 * or when it reads all @depth-1 indirect blocks successfully and finds
262 * the whole chain, all way to the data (returns %NULL, *err == 0).
264 static Indirect *ext2_get_branch(struct inode *inode,
270 struct super_block *sb = inode->i_sb;
272 struct buffer_head *bh;
275 /* i_data is not going away, no lock needed */
276 add_chain (chain, NULL, EXT2_I(inode)->i_data + *offsets);
280 bh = sb_bread(sb, le32_to_cpu(p->key));
283 read_lock(&EXT2_I(inode)->i_meta_lock);
284 if (!verify_chain(chain, p))
286 add_chain(++p, bh, (u32*)bh->b_data + *++offsets);
287 read_unlock(&EXT2_I(inode)->i_meta_lock);
294 read_unlock(&EXT2_I(inode)->i_meta_lock);
305 * ext2_find_near - find a place for allocation with sufficient locality
307 * @ind: descriptor of indirect block.
309 * This function returns the prefered place for block allocation.
310 * It is used when heuristic for sequential allocation fails.
312 * + if there is a block to the left of our position - allocate near it.
313 * + if pointer will live in indirect block - allocate near that block.
314 * + if pointer will live in inode - allocate in the same cylinder group.
316 * In the latter case we colour the starting block by the callers PID to
317 * prevent it from clashing with concurrent allocations for a different inode
318 * in the same block group. The PID is used here so that functionally related
319 * files will be close-by on-disk.
321 * Caller must make sure that @ind is valid and will stay that way.
324 static unsigned long ext2_find_near(struct inode *inode, Indirect *ind)
326 struct ext2_inode_info *ei = EXT2_I(inode);
327 u32 *start = ind->bh ? (u32*) ind->bh->b_data : ei->i_data;
329 unsigned long bg_start;
330 unsigned long colour;
332 /* Try to find previous block */
333 for (p = ind->p - 1; p >= start; p--)
335 return le32_to_cpu(*p);
337 /* No such thing, so let's try location of indirect block */
339 return ind->bh->b_blocknr;
342 * It is going to be refered from inode itself? OK, just put it into
343 * the same cylinder group then.
345 bg_start = (ei->i_block_group * EXT2_BLOCKS_PER_GROUP(inode->i_sb)) +
346 le32_to_cpu(EXT2_SB(inode->i_sb)->s_es->s_first_data_block);
347 colour = (current->pid % 16) *
348 (EXT2_BLOCKS_PER_GROUP(inode->i_sb) / 16);
349 return bg_start + colour;
353 * ext2_find_goal - find a prefered place for allocation.
355 * @block: block we want
356 * @chain: chain of indirect blocks
357 * @partial: pointer to the last triple within a chain
358 * @goal: place to store the result.
360 * Normally this function find the prefered place for block allocation,
361 * stores it in *@goal and returns zero. If the branch had been changed
362 * under us we return -EAGAIN.
365 static inline int ext2_find_goal(struct inode *inode,
371 struct ext2_inode_info *ei = EXT2_I(inode);
372 write_lock(&ei->i_meta_lock);
373 if (block == ei->i_next_alloc_block + 1) {
374 ei->i_next_alloc_block++;
375 ei->i_next_alloc_goal++;
377 if (verify_chain(chain, partial)) {
379 * try the heuristic for sequential allocation,
380 * failing that at least try to get decent locality.
382 if (block == ei->i_next_alloc_block)
383 *goal = ei->i_next_alloc_goal;
385 *goal = ext2_find_near(inode, partial);
386 write_unlock(&ei->i_meta_lock);
389 write_unlock(&ei->i_meta_lock);
394 * ext2_alloc_branch - allocate and set up a chain of blocks.
396 * @num: depth of the chain (number of blocks to allocate)
397 * @offsets: offsets (in the blocks) to store the pointers to next.
398 * @branch: place to store the chain in.
400 * This function allocates @num blocks, zeroes out all but the last one,
401 * links them into chain and (if we are synchronous) writes them to disk.
402 * In other words, it prepares a branch that can be spliced onto the
403 * inode. It stores the information about that chain in the branch[], in
404 * the same format as ext2_get_branch() would do. We are calling it after
405 * we had read the existing part of chain and partial points to the last
406 * triple of that (one with zero ->key). Upon the exit we have the same
407 * picture as after the successful ext2_get_block(), excpet that in one
408 * place chain is disconnected - *branch->p is still zero (we did not
409 * set the last link), but branch->key contains the number that should
410 * be placed into *branch->p to fill that gap.
412 * If allocation fails we free all blocks we've allocated (and forget
413 * their buffer_heads) and return the error value the from failed
414 * ext2_alloc_block() (normally -ENOSPC). Otherwise we set the chain
415 * as described above and return 0.
418 static int ext2_alloc_branch(struct inode *inode,
424 int blocksize = inode->i_sb->s_blocksize;
428 int parent = ext2_alloc_block(inode, goal, &err);
430 branch[0].key = cpu_to_le32(parent);
431 if (parent) for (n = 1; n < num; n++) {
432 struct buffer_head *bh;
433 /* Allocate the next block */
434 int nr = ext2_alloc_block(inode, parent, &err);
437 branch[n].key = cpu_to_le32(nr);
439 * Get buffer_head for parent block, zero it out and set
440 * the pointer to new one, then send parent to disk.
442 bh = sb_getblk(inode->i_sb, parent);
444 memset(bh->b_data, 0, blocksize);
446 branch[n].p = (u32*) bh->b_data + offsets[n];
447 *branch[n].p = branch[n].key;
448 set_buffer_uptodate(bh);
450 mark_buffer_dirty_inode(bh, inode);
451 /* We used to sync bh here if IS_SYNC(inode).
452 * But we now rely upon generic_osync_inode()
453 * and b_inode_buffers. But not for directories.
455 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
456 sync_dirty_buffer(bh);
462 /* Allocation failed, free what we already allocated */
463 for (i = 1; i < n; i++)
464 bforget(branch[i].bh);
465 for (i = 0; i < n; i++)
466 ext2_free_blocks(inode, le32_to_cpu(branch[i].key), 1);
471 * ext2_splice_branch - splice the allocated branch onto inode.
473 * @block: (logical) number of block we are adding
474 * @chain: chain of indirect blocks (with a missing link - see
476 * @where: location of missing link
477 * @num: number of blocks we are adding
479 * This function verifies that chain (up to the missing link) had not
480 * changed, fills the missing link and does all housekeeping needed in
481 * inode (->i_blocks, etc.). In case of success we end up with the full
482 * chain to new block and return 0. Otherwise (== chain had been changed)
483 * we free the new blocks (forgetting their buffer_heads, indeed) and
487 static inline int ext2_splice_branch(struct inode *inode,
493 struct ext2_inode_info *ei = EXT2_I(inode);
496 /* Verify that place we are splicing to is still there and vacant */
498 write_lock(&ei->i_meta_lock);
499 if (!verify_chain(chain, where-1) || *where->p)
504 *where->p = where->key;
505 ei->i_next_alloc_block = block;
506 ei->i_next_alloc_goal = le32_to_cpu(where[num-1].key);
508 write_unlock(&ei->i_meta_lock);
510 /* We are done with atomic stuff, now do the rest of housekeeping */
512 inode->i_ctime = CURRENT_TIME;
514 /* had we spliced it onto indirect block? */
516 mark_buffer_dirty_inode(where->bh, inode);
518 mark_inode_dirty(inode);
522 write_unlock(&ei->i_meta_lock);
523 for (i = 1; i < num; i++)
524 bforget(where[i].bh);
525 for (i = 0; i < num; i++)
526 ext2_free_blocks(inode, le32_to_cpu(where[i].key), 1);
531 * Allocation strategy is simple: if we have to allocate something, we will
532 * have to go the whole way to leaf. So let's do it before attaching anything
533 * to tree, set linkage between the newborn blocks, write them if sync is
534 * required, recheck the path, free and repeat if check fails, otherwise
535 * set the last missing link (that will protect us from any truncate-generated
536 * removals - all blocks on the path are immune now) and possibly force the
537 * write on the parent block.
538 * That has a nice additional property: no special recovery from the failed
539 * allocations is needed - we simply release blocks and do not touch anything
540 * reachable from inode.
543 static int ext2_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
552 int depth = ext2_block_to_path(inode, iblock, offsets, &boundary);
558 partial = ext2_get_branch(inode, depth, offsets, chain, &err);
560 /* Simplest case - block found, no allocation needed */
563 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
565 set_buffer_boundary(bh_result);
566 /* Clean up and exit */
567 partial = chain+depth-1; /* the whole chain */
571 /* Next simple case - plain lookup or failed read of indirect block */
572 if (!create || err == -EIO) {
574 while (partial > chain) {
583 * Indirect block might be removed by truncate while we were
584 * reading it. Handling of that case (forget what we've got and
585 * reread) is taken out of the main path.
590 if (ext2_find_goal(inode, iblock, chain, partial, &goal) < 0)
593 left = (chain + depth) - partial;
594 err = ext2_alloc_branch(inode, left, goal,
595 offsets+(partial-chain), partial);
599 if (ext2_splice_branch(inode, iblock, chain, partial, left) < 0)
602 set_buffer_new(bh_result);
606 while (partial > chain) {
613 static int ext2_writepage(struct page *page, struct writeback_control *wbc)
615 return block_write_full_page(page, ext2_get_block, wbc);
618 static int ext2_readpage(struct file *file, struct page *page)
620 return mpage_readpage(page, ext2_get_block);
624 ext2_readpages(struct file *file, struct address_space *mapping,
625 struct list_head *pages, unsigned nr_pages)
627 return mpage_readpages(mapping, pages, nr_pages, ext2_get_block);
631 ext2_prepare_write(struct file *file, struct page *page,
632 unsigned from, unsigned to)
634 return block_prepare_write(page,from,to,ext2_get_block);
638 ext2_nobh_prepare_write(struct file *file, struct page *page,
639 unsigned from, unsigned to)
641 return nobh_prepare_write(page,from,to,ext2_get_block);
644 static sector_t ext2_bmap(struct address_space *mapping, sector_t block)
646 return generic_block_bmap(mapping,block,ext2_get_block);
650 ext2_get_blocks(struct inode *inode, sector_t iblock, unsigned long max_blocks,
651 struct buffer_head *bh_result, int create)
655 ret = ext2_get_block(inode, iblock, bh_result, create);
657 bh_result->b_size = (1 << inode->i_blkbits);
662 ext2_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
663 loff_t offset, unsigned long nr_segs)
665 struct file *file = iocb->ki_filp;
666 struct inode *inode = file->f_mapping->host;
668 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
669 offset, nr_segs, ext2_get_blocks, NULL);
673 ext2_writepages(struct address_space *mapping, struct writeback_control *wbc)
675 return mpage_writepages(mapping, wbc, ext2_get_block);
678 struct address_space_operations ext2_aops = {
679 .readpage = ext2_readpage,
680 .readpages = ext2_readpages,
681 .writepage = ext2_writepage,
682 .sync_page = block_sync_page,
683 .prepare_write = ext2_prepare_write,
684 .commit_write = generic_commit_write,
686 .direct_IO = ext2_direct_IO,
687 .writepages = ext2_writepages,
690 struct address_space_operations ext2_nobh_aops = {
691 .readpage = ext2_readpage,
692 .readpages = ext2_readpages,
693 .writepage = ext2_writepage,
694 .sync_page = block_sync_page,
695 .prepare_write = ext2_nobh_prepare_write,
696 .commit_write = nobh_commit_write,
698 .direct_IO = ext2_direct_IO,
699 .writepages = ext2_writepages,
703 * Probably it should be a library function... search for first non-zero word
704 * or memcmp with zero_page, whatever is better for particular architecture.
707 static inline int all_zeroes(u32 *p, u32 *q)
716 * ext2_find_shared - find the indirect blocks for partial truncation.
717 * @inode: inode in question
718 * @depth: depth of the affected branch
719 * @offsets: offsets of pointers in that branch (see ext2_block_to_path)
720 * @chain: place to store the pointers to partial indirect blocks
721 * @top: place to the (detached) top of branch
723 * This is a helper function used by ext2_truncate().
725 * When we do truncate() we may have to clean the ends of several indirect
726 * blocks but leave the blocks themselves alive. Block is partially
727 * truncated if some data below the new i_size is refered from it (and
728 * it is on the path to the first completely truncated data block, indeed).
729 * We have to free the top of that path along with everything to the right
730 * of the path. Since no allocation past the truncation point is possible
731 * until ext2_truncate() finishes, we may safely do the latter, but top
732 * of branch may require special attention - pageout below the truncation
733 * point might try to populate it.
735 * We atomically detach the top of branch from the tree, store the block
736 * number of its root in *@top, pointers to buffer_heads of partially
737 * truncated blocks - in @chain[].bh and pointers to their last elements
738 * that should not be removed - in @chain[].p. Return value is the pointer
739 * to last filled element of @chain.
741 * The work left to caller to do the actual freeing of subtrees:
742 * a) free the subtree starting from *@top
743 * b) free the subtrees whose roots are stored in
744 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
745 * c) free the subtrees growing from the inode past the @chain[0].p
746 * (no partially truncated stuff there).
749 static Indirect *ext2_find_shared(struct inode *inode,
755 Indirect *partial, *p;
759 for (k = depth; k > 1 && !offsets[k-1]; k--)
761 partial = ext2_get_branch(inode, k, offsets, chain, &err);
763 partial = chain + k-1;
765 * If the branch acquired continuation since we've looked at it -
766 * fine, it should all survive and (new) top doesn't belong to us.
768 write_lock(&EXT2_I(inode)->i_meta_lock);
769 if (!partial->key && *partial->p) {
770 write_unlock(&EXT2_I(inode)->i_meta_lock);
773 for (p=partial; p>chain && all_zeroes((u32*)p->bh->b_data,p->p); p--)
776 * OK, we've found the last block that must survive. The rest of our
777 * branch should be detached before unlocking. However, if that rest
778 * of branch is all ours and does not grow immediately from the inode
779 * it's easier to cheat and just decrement partial->p.
781 if (p == chain + k - 1 && p > chain) {
787 write_unlock(&EXT2_I(inode)->i_meta_lock);
799 * ext2_free_data - free a list of data blocks
800 * @inode: inode we are dealing with
801 * @p: array of block numbers
802 * @q: points immediately past the end of array
804 * We are freeing all blocks refered from that array (numbers are
805 * stored as little-endian 32-bit) and updating @inode->i_blocks
808 static inline void ext2_free_data(struct inode *inode, u32 *p, u32 *q)
810 unsigned long block_to_free = 0, count = 0;
813 for ( ; p < q ; p++) {
814 nr = le32_to_cpu(*p);
817 /* accumulate blocks to free if they're contiguous */
820 else if (block_to_free == nr - count)
823 mark_inode_dirty(inode);
824 ext2_free_blocks (inode, block_to_free, count);
832 mark_inode_dirty(inode);
833 ext2_free_blocks (inode, block_to_free, count);
838 * ext2_free_branches - free an array of branches
839 * @inode: inode we are dealing with
840 * @p: array of block numbers
841 * @q: pointer immediately past the end of array
842 * @depth: depth of the branches to free
844 * We are freeing all blocks refered from these branches (numbers are
845 * stored as little-endian 32-bit) and updating @inode->i_blocks
848 static void ext2_free_branches(struct inode *inode, u32 *p, u32 *q, int depth)
850 struct buffer_head * bh;
854 int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
855 for ( ; p < q ; p++) {
856 nr = le32_to_cpu(*p);
860 bh = sb_bread(inode->i_sb, nr);
862 * A read failure? Report error and clear slot
866 ext2_error(inode->i_sb, "ext2_free_branches",
867 "Read failure, inode=%ld, block=%ld",
871 ext2_free_branches(inode,
873 (u32*)bh->b_data + addr_per_block,
876 ext2_free_blocks(inode, nr, 1);
877 mark_inode_dirty(inode);
880 ext2_free_data(inode, p, q);
883 static void ext2_truncate_nocheck(struct inode * inode)
885 u32 *i_data = EXT2_I(inode)->i_data;
886 int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
895 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
896 S_ISLNK(inode->i_mode)))
898 if (ext2_inode_is_fast_symlink(inode))
901 ext2_discard_prealloc(inode);
903 blocksize = inode->i_sb->s_blocksize;
904 iblock = (inode->i_size + blocksize-1)
905 >> EXT2_BLOCK_SIZE_BITS(inode->i_sb);
907 if (test_opt(inode->i_sb, NOBH))
908 nobh_truncate_page(inode->i_mapping, inode->i_size);
910 block_truncate_page(inode->i_mapping,
911 inode->i_size, ext2_get_block);
913 n = ext2_block_to_path(inode, iblock, offsets, NULL);
918 ext2_free_data(inode, i_data+offsets[0],
919 i_data + EXT2_NDIR_BLOCKS);
923 partial = ext2_find_shared(inode, n, offsets, chain, &nr);
924 /* Kill the top of shared branch (already detached) */
926 if (partial == chain)
927 mark_inode_dirty(inode);
929 mark_buffer_dirty_inode(partial->bh, inode);
930 ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
932 /* Clear the ends of indirect blocks on the shared branch */
933 while (partial > chain) {
934 ext2_free_branches(inode,
936 (u32*)partial->bh->b_data + addr_per_block,
937 (chain+n-1) - partial);
938 mark_buffer_dirty_inode(partial->bh, inode);
939 brelse (partial->bh);
943 /* Kill the remaining (whole) subtrees */
944 switch (offsets[0]) {
946 nr = i_data[EXT2_IND_BLOCK];
948 i_data[EXT2_IND_BLOCK] = 0;
949 mark_inode_dirty(inode);
950 ext2_free_branches(inode, &nr, &nr+1, 1);
953 nr = i_data[EXT2_DIND_BLOCK];
955 i_data[EXT2_DIND_BLOCK] = 0;
956 mark_inode_dirty(inode);
957 ext2_free_branches(inode, &nr, &nr+1, 2);
959 case EXT2_DIND_BLOCK:
960 nr = i_data[EXT2_TIND_BLOCK];
962 i_data[EXT2_TIND_BLOCK] = 0;
963 mark_inode_dirty(inode);
964 ext2_free_branches(inode, &nr, &nr+1, 3);
966 case EXT2_TIND_BLOCK:
969 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
970 if (inode_needs_sync(inode)) {
971 sync_mapping_buffers(inode->i_mapping);
972 ext2_sync_inode (inode);
974 mark_inode_dirty(inode);
978 static struct ext2_inode *ext2_get_inode(struct super_block *sb, ino_t ino,
979 struct buffer_head **p)
981 struct buffer_head * bh;
982 unsigned long block_group;
984 unsigned long offset;
985 struct ext2_group_desc * gdp;
988 if ((ino != EXT2_ROOT_INO && ino < EXT2_FIRST_INO(sb)) ||
989 ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
992 block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
993 gdp = ext2_get_group_desc(sb, block_group, &bh);
997 * Figure out the offset within the block group inode table
999 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb);
1000 block = le32_to_cpu(gdp->bg_inode_table) +
1001 (offset >> EXT2_BLOCK_SIZE_BITS(sb));
1002 if (!(bh = sb_bread(sb, block)))
1006 offset &= (EXT2_BLOCK_SIZE(sb) - 1);
1007 return (struct ext2_inode *) (bh->b_data + offset);
1010 ext2_error(sb, "ext2_get_inode", "bad inode number: %lu",
1011 (unsigned long) ino);
1012 return ERR_PTR(-EINVAL);
1014 ext2_error(sb, "ext2_get_inode",
1015 "unable to read inode block - inode=%lu, block=%lu",
1016 (unsigned long) ino, block);
1018 return ERR_PTR(-EIO);
1021 void ext2_truncate (struct inode * inode)
1023 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1025 ext2_truncate_nocheck(inode);
1028 void ext2_set_inode_flags(struct inode *inode)
1030 unsigned int flags = EXT2_I(inode)->i_flags;
1032 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
1033 if (flags & EXT2_SYNC_FL)
1034 inode->i_flags |= S_SYNC;
1035 if (flags & EXT2_APPEND_FL)
1036 inode->i_flags |= S_APPEND;
1037 if (flags & EXT2_IMMUTABLE_FL)
1038 inode->i_flags |= S_IMMUTABLE;
1039 if (flags & EXT2_IUNLINK_FL)
1040 inode->i_flags |= S_IUNLINK;
1041 if (flags & EXT2_BARRIER_FL)
1042 inode->i_flags |= S_BARRIER;
1043 if (flags & EXT2_NOATIME_FL)
1044 inode->i_flags |= S_NOATIME;
1045 if (flags & EXT2_DIRSYNC_FL)
1046 inode->i_flags |= S_DIRSYNC;
1049 void ext2_read_inode (struct inode * inode)
1051 struct ext2_inode_info *ei = EXT2_I(inode);
1052 ino_t ino = inode->i_ino;
1053 struct buffer_head * bh;
1054 struct ext2_inode * raw_inode = ext2_get_inode(inode->i_sb, ino, &bh);
1059 #ifdef CONFIG_EXT2_FS_POSIX_ACL
1060 ei->i_acl = EXT2_ACL_NOT_CACHED;
1061 ei->i_default_acl = EXT2_ACL_NOT_CACHED;
1063 if (IS_ERR(raw_inode))
1066 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
1067 uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
1068 gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
1069 if (!(test_opt (inode->i_sb, NO_UID32))) {
1070 uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
1071 gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
1073 inode->i_uid = INOXID_UID(uid, gid);
1074 inode->i_gid = INOXID_GID(uid, gid);
1075 if (inode->i_sb->s_flags & MS_TAGXID)
1076 inode->i_xid = INOXID_XID(uid, gid, le16_to_cpu(raw_inode->i_raw_xid));
1078 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
1079 inode->i_size = le32_to_cpu(raw_inode->i_size);
1080 inode->i_atime.tv_sec = le32_to_cpu(raw_inode->i_atime);
1081 inode->i_ctime.tv_sec = le32_to_cpu(raw_inode->i_ctime);
1082 inode->i_mtime.tv_sec = le32_to_cpu(raw_inode->i_mtime);
1083 inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
1084 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
1085 /* We now have enough fields to check if the inode was active or not.
1086 * This is needed because nfsd might try to access dead inodes
1087 * the test is that same one that e2fsck uses
1088 * NeilBrown 1999oct15
1090 if (inode->i_nlink == 0 && (inode->i_mode == 0 || ei->i_dtime)) {
1091 /* this inode is deleted */
1095 inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat), not the fs block size */
1096 inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
1097 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
1098 ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
1099 ei->i_frag_no = raw_inode->i_frag;
1100 ei->i_frag_size = raw_inode->i_fsize;
1101 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
1103 if (S_ISREG(inode->i_mode))
1104 inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
1106 ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
1108 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
1110 ei->i_next_alloc_block = 0;
1111 ei->i_next_alloc_goal = 0;
1112 ei->i_prealloc_count = 0;
1113 ei->i_block_group = (ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
1114 ei->i_dir_start_lookup = 0;
1117 * NOTE! The in-memory inode i_data array is in little-endian order
1118 * even on big-endian machines: we do NOT byteswap the block numbers!
1120 for (n = 0; n < EXT2_N_BLOCKS; n++)
1121 ei->i_data[n] = raw_inode->i_block[n];
1123 if (S_ISREG(inode->i_mode)) {
1124 inode->i_op = &ext2_file_inode_operations;
1125 inode->i_fop = &ext2_file_operations;
1126 if (test_opt(inode->i_sb, NOBH))
1127 inode->i_mapping->a_ops = &ext2_nobh_aops;
1129 inode->i_mapping->a_ops = &ext2_aops;
1130 } else if (S_ISDIR(inode->i_mode)) {
1131 inode->i_op = &ext2_dir_inode_operations;
1132 inode->i_fop = &ext2_dir_operations;
1133 if (test_opt(inode->i_sb, NOBH))
1134 inode->i_mapping->a_ops = &ext2_nobh_aops;
1136 inode->i_mapping->a_ops = &ext2_aops;
1137 } else if (S_ISLNK(inode->i_mode)) {
1138 if (ext2_inode_is_fast_symlink(inode))
1139 inode->i_op = &ext2_fast_symlink_inode_operations;
1141 inode->i_op = &ext2_symlink_inode_operations;
1142 if (test_opt(inode->i_sb, NOBH))
1143 inode->i_mapping->a_ops = &ext2_nobh_aops;
1145 inode->i_mapping->a_ops = &ext2_aops;
1148 inode->i_op = &ext2_special_inode_operations;
1149 if (raw_inode->i_block[0])
1150 init_special_inode(inode, inode->i_mode,
1151 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
1153 init_special_inode(inode, inode->i_mode,
1154 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
1157 ext2_set_inode_flags(inode);
1161 make_bad_inode(inode);
1165 static int ext2_update_inode(struct inode * inode, int do_sync)
1167 struct ext2_inode_info *ei = EXT2_I(inode);
1168 struct super_block *sb = inode->i_sb;
1169 ino_t ino = inode->i_ino;
1170 uid_t uid = XIDINO_UID(inode->i_uid, inode->i_xid);
1171 gid_t gid = XIDINO_GID(inode->i_gid, inode->i_xid);
1172 struct buffer_head * bh;
1173 struct ext2_inode * raw_inode = ext2_get_inode(sb, ino, &bh);
1177 if (IS_ERR(raw_inode))
1180 /* For fields not not tracking in the in-memory inode,
1181 * initialise them to zero for new inodes. */
1182 if (ei->i_state & EXT2_STATE_NEW)
1183 memset(raw_inode, 0, EXT2_SB(sb)->s_inode_size);
1185 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
1186 if (!(test_opt(sb, NO_UID32))) {
1187 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(uid));
1188 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(gid));
1190 * Fix up interoperability with old kernels. Otherwise, old inodes get
1191 * re-used with the upper 16 bits of the uid/gid intact
1194 raw_inode->i_uid_high = cpu_to_le16(high_16_bits(uid));
1195 raw_inode->i_gid_high = cpu_to_le16(high_16_bits(gid));
1197 raw_inode->i_uid_high = 0;
1198 raw_inode->i_gid_high = 0;
1201 raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(uid));
1202 raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(gid));
1203 raw_inode->i_uid_high = 0;
1204 raw_inode->i_gid_high = 0;
1206 #ifdef CONFIG_INOXID_GID32
1207 raw_inode->i_raw_xid = cpu_to_le16(inode->i_xid);
1209 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
1210 raw_inode->i_size = cpu_to_le32(inode->i_size);
1211 raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
1212 raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
1213 raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
1215 raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
1216 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
1217 raw_inode->i_flags = cpu_to_le32(ei->i_flags);
1218 raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
1219 raw_inode->i_frag = ei->i_frag_no;
1220 raw_inode->i_fsize = ei->i_frag_size;
1221 raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
1222 if (!S_ISREG(inode->i_mode))
1223 raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
1225 raw_inode->i_size_high = cpu_to_le32(inode->i_size >> 32);
1226 if (inode->i_size > 0x7fffffffULL) {
1227 if (!EXT2_HAS_RO_COMPAT_FEATURE(sb,
1228 EXT2_FEATURE_RO_COMPAT_LARGE_FILE) ||
1229 EXT2_SB(sb)->s_es->s_rev_level ==
1230 cpu_to_le32(EXT2_GOOD_OLD_REV)) {
1231 /* If this is the first large file
1232 * created, add a flag to the superblock.
1235 ext2_update_dynamic_rev(sb);
1236 EXT2_SET_RO_COMPAT_FEATURE(sb,
1237 EXT2_FEATURE_RO_COMPAT_LARGE_FILE);
1239 ext2_write_super(sb);
1244 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
1245 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1246 if (old_valid_dev(inode->i_rdev)) {
1247 raw_inode->i_block[0] =
1248 cpu_to_le32(old_encode_dev(inode->i_rdev));
1249 raw_inode->i_block[1] = 0;
1251 raw_inode->i_block[0] = 0;
1252 raw_inode->i_block[1] =
1253 cpu_to_le32(new_encode_dev(inode->i_rdev));
1254 raw_inode->i_block[2] = 0;
1256 } else for (n = 0; n < EXT2_N_BLOCKS; n++)
1257 raw_inode->i_block[n] = ei->i_data[n];
1258 mark_buffer_dirty(bh);
1260 sync_dirty_buffer(bh);
1261 if (buffer_req(bh) && !buffer_uptodate(bh)) {
1262 printk ("IO error syncing ext2 inode [%s:%08lx]\n",
1263 sb->s_id, (unsigned long) ino);
1267 ei->i_state &= ~EXT2_STATE_NEW;
1272 void ext2_write_inode(struct inode *inode, int wait)
1274 ext2_update_inode(inode, wait);
1277 int ext2_sync_inode(struct inode *inode)
1279 struct writeback_control wbc = {
1280 .sync_mode = WB_SYNC_ALL,
1281 .nr_to_write = 0, /* sys_fsync did this */
1283 return sync_inode(inode, &wbc);
1286 int ext2_setattr_flags(struct inode *inode, unsigned int flags)
1288 unsigned int oldflags, newflags;
1290 oldflags = EXT2_I(inode)->i_flags;
1291 newflags = oldflags &
1292 ~(EXT2_IMMUTABLE_FL | EXT2_IUNLINK_FL | EXT2_BARRIER_FL);
1293 if (flags & ATTR_FLAG_IMMUTABLE)
1294 newflags |= EXT2_IMMUTABLE_FL;
1295 if (flags & ATTR_FLAG_IUNLINK)
1296 newflags |= EXT2_IUNLINK_FL;
1297 if (flags & ATTR_FLAG_BARRIER)
1298 newflags |= EXT2_BARRIER_FL;
1300 if (oldflags ^ newflags) {
1301 EXT2_I(inode)->i_flags = newflags;
1302 inode->i_ctime = CURRENT_TIME;
1307 int ext2_setattr(struct dentry *dentry, struct iattr *iattr)
1309 struct inode *inode = dentry->d_inode;
1312 error = inode_change_ok(inode, iattr);
1315 if ((iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) ||
1316 (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)) {
1317 error = DQUOT_TRANSFER(inode, iattr) ? -EDQUOT : 0;
1321 if (iattr->ia_valid & ATTR_ATTR_FLAG)
1322 ext2_setattr_flags(inode, iattr->ia_attr_flags);
1324 inode_setattr(inode, iattr);
1325 if (iattr->ia_valid & ATTR_MODE)
1326 error = ext2_acl_chmod(inode);