- Peter Anvin: more P4 configuration parsing
[davej-history.git] / fs / ext2 / inode.c
blob168a5503fcc5827874aab919b5741f9c04a471d9
1 /*
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)
9 * from
11 * linux/fs/minix/inode.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
15 * Goal-directed block allocation by Stephen Tweedie
16 * (sct@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/fs.h>
26 #include <linux/ext2_fs.h>
27 #include <linux/locks.h>
28 #include <linux/smp_lock.h>
29 #include <linux/sched.h>
30 #include <linux/highuid.h>
32 static int ext2_update_inode(struct inode * inode, int do_sync);
35 * Called at each iput()
37 void ext2_put_inode (struct inode * inode)
39 ext2_discard_prealloc (inode);
43 * Called at the last iput() if i_nlink is zero.
45 void ext2_delete_inode (struct inode * inode)
47 lock_kernel();
49 if (is_bad_inode(inode) ||
50 inode->i_ino == EXT2_ACL_IDX_INO ||
51 inode->i_ino == EXT2_ACL_DATA_INO)
52 goto no_delete;
53 inode->u.ext2_i.i_dtime = CURRENT_TIME;
54 mark_inode_dirty(inode);
55 ext2_update_inode(inode, IS_SYNC(inode));
56 inode->i_size = 0;
57 if (inode->i_blocks)
58 ext2_truncate (inode);
59 ext2_free_inode (inode);
61 unlock_kernel();
62 return;
63 no_delete:
64 unlock_kernel();
65 clear_inode(inode); /* We must guarantee clearing of inode... */
68 void ext2_discard_prealloc (struct inode * inode)
70 #ifdef EXT2_PREALLOCATE
71 lock_kernel();
72 /* Writer: ->i_prealloc* */
73 if (inode->u.ext2_i.i_prealloc_count) {
74 unsigned short total = inode->u.ext2_i.i_prealloc_count;
75 unsigned long block = inode->u.ext2_i.i_prealloc_block;
76 inode->u.ext2_i.i_prealloc_count = 0;
77 inode->u.ext2_i.i_prealloc_block = 0;
78 /* Writer: end */
79 ext2_free_blocks (inode, block, total);
81 unlock_kernel();
82 #endif
85 static int ext2_alloc_block (struct inode * inode, unsigned long goal, int *err)
87 #ifdef EXT2FS_DEBUG
88 static unsigned long alloc_hits = 0, alloc_attempts = 0;
89 #endif
90 unsigned long result;
93 #ifdef EXT2_PREALLOCATE
94 /* Writer: ->i_prealloc* */
95 if (inode->u.ext2_i.i_prealloc_count &&
96 (goal == inode->u.ext2_i.i_prealloc_block ||
97 goal + 1 == inode->u.ext2_i.i_prealloc_block))
99 result = inode->u.ext2_i.i_prealloc_block++;
100 inode->u.ext2_i.i_prealloc_count--;
101 /* Writer: end */
102 #ifdef EXT2FS_DEBUG
103 ext2_debug ("preallocation hit (%lu/%lu).\n",
104 ++alloc_hits, ++alloc_attempts);
105 #endif
106 } else {
107 ext2_discard_prealloc (inode);
108 #ifdef EXT2FS_DEBUG
109 ext2_debug ("preallocation miss (%lu/%lu).\n",
110 alloc_hits, ++alloc_attempts);
111 #endif
112 if (S_ISREG(inode->i_mode))
113 result = ext2_new_block (inode, goal,
114 &inode->u.ext2_i.i_prealloc_count,
115 &inode->u.ext2_i.i_prealloc_block, err);
116 else
117 result = ext2_new_block (inode, goal, 0, 0, err);
119 #else
120 result = ext2_new_block (inode, goal, 0, 0, err);
121 #endif
122 return result;
125 typedef struct {
126 u32 *p;
127 u32 key;
128 struct buffer_head *bh;
129 } Indirect;
131 static inline void add_chain(Indirect *p, struct buffer_head *bh, u32 *v)
133 p->key = *(p->p = v);
134 p->bh = bh;
137 static inline int verify_chain(Indirect *from, Indirect *to)
139 while (from <= to && from->key == *from->p)
140 from++;
141 return (from > to);
145 * ext2_block_to_path - parse the block number into array of offsets
146 * @inode: inode in question (we are only interested in its superblock)
147 * @i_block: block number to be parsed
148 * @offsets: array to store the offsets in
150 * To store the locations of file's data ext2 uses a data structure common
151 * for UNIX filesystems - tree of pointers anchored in the inode, with
152 * data blocks at leaves and indirect blocks in intermediate nodes.
153 * This function translates the block number into path in that tree -
154 * return value is the path length and @offsets[n] is the offset of
155 * pointer to (n+1)th node in the nth one. If @block is out of range
156 * (negative or too large) warning is printed and zero returned.
158 * Note: function doesn't find node addresses, so no IO is needed. All
159 * we need to know is the capacity of indirect blocks (taken from the
160 * inode->i_sb).
164 * Portability note: the last comparison (check that we fit into triple
165 * indirect block) is spelled differently, because otherwise on an
166 * architecture with 32-bit longs and 8Kb pages we might get into trouble
167 * if our filesystem had 8Kb blocks. We might use long long, but that would
168 * kill us on x86. Oh, well, at least the sign propagation does not matter -
169 * i_block would have to be negative in the very beginning, so we would not
170 * get there at all.
173 static int ext2_block_to_path(struct inode *inode, long i_block, int offsets[4])
175 int ptrs = EXT2_ADDR_PER_BLOCK(inode->i_sb);
176 int ptrs_bits = EXT2_ADDR_PER_BLOCK_BITS(inode->i_sb);
177 const long direct_blocks = EXT2_NDIR_BLOCKS,
178 indirect_blocks = ptrs,
179 double_blocks = (1 << (ptrs_bits * 2));
180 int n = 0;
182 if (i_block < 0) {
183 ext2_warning (inode->i_sb, "ext2_block_to_path", "block < 0");
184 } else if (i_block < direct_blocks) {
185 offsets[n++] = i_block;
186 } else if ( (i_block -= direct_blocks) < indirect_blocks) {
187 offsets[n++] = EXT2_IND_BLOCK;
188 offsets[n++] = i_block;
189 } else if ((i_block -= indirect_blocks) < double_blocks) {
190 offsets[n++] = EXT2_DIND_BLOCK;
191 offsets[n++] = i_block >> ptrs_bits;
192 offsets[n++] = i_block & (ptrs - 1);
193 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
194 offsets[n++] = EXT2_TIND_BLOCK;
195 offsets[n++] = i_block >> (ptrs_bits * 2);
196 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
197 offsets[n++] = i_block & (ptrs - 1);
198 } else {
199 ext2_warning (inode->i_sb, "ext2_block_to_path", "block > big");
201 return n;
205 * ext2_get_branch - read the chain of indirect blocks leading to data
206 * @inode: inode in question
207 * @depth: depth of the chain (1 - direct pointer, etc.)
208 * @offsets: offsets of pointers in inode/indirect blocks
209 * @chain: place to store the result
210 * @err: here we store the error value
212 * Function fills the array of triples <key, p, bh> and returns %NULL
213 * if everything went OK or the pointer to the last filled triple
214 * (incomplete one) otherwise. Upon the return chain[i].key contains
215 * the number of (i+1)-th block in the chain (as it is stored in memory,
216 * i.e. little-endian 32-bit), chain[i].p contains the address of that
217 * number (it points into struct inode for i==0 and into the bh->b_data
218 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
219 * block for i>0 and NULL for i==0. In other words, it holds the block
220 * numbers of the chain, addresses they were taken from (and where we can
221 * verify that chain did not change) and buffer_heads hosting these
222 * numbers.
224 * Function stops when it stumbles upon zero pointer (absent block)
225 * (pointer to last triple returned, *@err == 0)
226 * or when it gets an IO error reading an indirect block
227 * (ditto, *@err == -EIO)
228 * or when it notices that chain had been changed while it was reading
229 * (ditto, *@err == -EAGAIN)
230 * or when it reads all @depth-1 indirect blocks successfully and finds
231 * the whole chain, all way to the data (returns %NULL, *err == 0).
233 static inline Indirect *ext2_get_branch(struct inode *inode,
234 int depth,
235 int *offsets,
236 Indirect chain[4],
237 int *err)
239 kdev_t dev = inode->i_dev;
240 int size = inode->i_sb->s_blocksize;
241 Indirect *p = chain;
242 struct buffer_head *bh;
244 *err = 0;
245 /* i_data is not going away, no lock needed */
246 add_chain (chain, NULL, inode->u.ext2_i.i_data + *offsets);
247 if (!p->key)
248 goto no_block;
249 while (--depth) {
250 bh = bread(dev, le32_to_cpu(p->key), size);
251 if (!bh)
252 goto failure;
253 /* Reader: pointers */
254 if (!verify_chain(chain, p))
255 goto changed;
256 add_chain(++p, bh, (u32*)bh->b_data + *++offsets);
257 /* Reader: end */
258 if (!p->key)
259 goto no_block;
261 return NULL;
263 changed:
264 *err = -EAGAIN;
265 goto no_block;
266 failure:
267 *err = -EIO;
268 no_block:
269 return p;
273 * ext2_find_near - find a place for allocation with sufficient locality
274 * @inode: owner
275 * @ind: descriptor of indirect block.
277 * This function returns the prefered place for block allocation.
278 * It is used when heuristic for sequential allocation fails.
279 * Rules are:
280 * + if there is a block to the left of our position - allocate near it.
281 * + if pointer will live in indirect block - allocate near that block.
282 * + if pointer will live in inode - allocate in the same cylinder group.
283 * Caller must make sure that @ind is valid and will stay that way.
286 static inline unsigned long ext2_find_near(struct inode *inode, Indirect *ind)
288 u32 *start = ind->bh ? (u32*) ind->bh->b_data : inode->u.ext2_i.i_data;
289 u32 *p;
291 /* Try to find previous block */
292 for (p = ind->p - 1; p >= start; p--)
293 if (*p)
294 return le32_to_cpu(*p);
296 /* No such thing, so let's try location of indirect block */
297 if (ind->bh)
298 return ind->bh->b_blocknr;
301 * It is going to be refered from inode itself? OK, just put it into
302 * the same cylinder group then.
304 return (inode->u.ext2_i.i_block_group *
305 EXT2_BLOCKS_PER_GROUP(inode->i_sb)) +
306 le32_to_cpu(inode->i_sb->u.ext2_sb.s_es->s_first_data_block);
310 * ext2_find_goal - find a prefered place for allocation.
311 * @inode: owner
312 * @block: block we want
313 * @chain: chain of indirect blocks
314 * @partial: pointer to the last triple within a chain
315 * @goal: place to store the result.
317 * Normally this function find the prefered place for block allocation,
318 * stores it in *@goal and returns zero. If the branch had been changed
319 * under us we return -EAGAIN.
322 static inline int ext2_find_goal(struct inode *inode,
323 long block,
324 Indirect chain[4],
325 Indirect *partial,
326 unsigned long *goal)
328 /* Writer: ->i_next_alloc* */
329 if (block == inode->u.ext2_i.i_next_alloc_block + 1) {
330 inode->u.ext2_i.i_next_alloc_block++;
331 inode->u.ext2_i.i_next_alloc_goal++;
333 /* Writer: end */
334 /* Reader: pointers, ->i_next_alloc* */
335 if (verify_chain(chain, partial)) {
337 * try the heuristic for sequential allocation,
338 * failing that at least try to get decent locality.
340 if (block == inode->u.ext2_i.i_next_alloc_block)
341 *goal = inode->u.ext2_i.i_next_alloc_goal;
342 if (!*goal)
343 *goal = ext2_find_near(inode, partial);
344 return 0;
346 /* Reader: end */
347 return -EAGAIN;
351 * ext2_alloc_branch - allocate and set up a chain of blocks.
352 * @inode: owner
353 * @num: depth of the chain (number of blocks to allocate)
354 * @offsets: offsets (in the blocks) to store the pointers to next.
355 * @branch: place to store the chain in.
357 * This function allocates @num blocks, zeroes out all but the last one,
358 * links them into chain and (if we are synchronous) writes them to disk.
359 * In other words, it prepares a branch that can be spliced onto the
360 * inode. It stores the information about that chain in the branch[], in
361 * the same format as ext2_get_branch() would do. We are calling it after
362 * we had read the existing part of chain and partial points to the last
363 * triple of that (one with zero ->key). Upon the exit we have the same
364 * picture as after the successful ext2_get_block(), excpet that in one
365 * place chain is disconnected - *branch->p is still zero (we did not
366 * set the last link), but branch->key contains the number that should
367 * be placed into *branch->p to fill that gap.
369 * If allocation fails we free all blocks we've allocated (and forget
370 * ther buffer_heads) and return the error value the from failed
371 * ext2_alloc_block() (normally -ENOSPC). Otherwise we set the chain
372 * as described above and return 0.
375 static int ext2_alloc_branch(struct inode *inode,
376 int num,
377 unsigned long goal,
378 int *offsets,
379 Indirect *branch)
381 int blocksize = inode->i_sb->s_blocksize;
382 int n = 0;
383 int err;
384 int i;
385 int parent = ext2_alloc_block(inode, goal, &err);
387 branch[0].key = cpu_to_le32(parent);
388 if (parent) for (n = 1; n < num; n++) {
389 struct buffer_head *bh;
390 /* Allocate the next block */
391 int nr = ext2_alloc_block(inode, parent, &err);
392 if (!nr)
393 break;
394 branch[n].key = cpu_to_le32(nr);
396 * Get buffer_head for parent block, zero it out and set
397 * the pointer to new one, then send parent to disk.
399 bh = getblk(inode->i_dev, parent, blocksize);
400 if (!buffer_uptodate(bh))
401 wait_on_buffer(bh);
402 memset(bh->b_data, 0, blocksize);
403 branch[n].bh = bh;
404 branch[n].p = (u32*) bh->b_data + offsets[n];
405 *branch[n].p = branch[n].key;
406 mark_buffer_uptodate(bh, 1);
407 mark_buffer_dirty_inode(bh, inode);
408 if (IS_SYNC(inode) || inode->u.ext2_i.i_osync) {
409 ll_rw_block (WRITE, 1, &bh);
410 wait_on_buffer (bh);
412 parent = nr;
414 if (n == num)
415 return 0;
417 /* Allocation failed, free what we already allocated */
418 for (i = 1; i < n; i++)
419 bforget(branch[i].bh);
420 for (i = 0; i < n; i++)
421 ext2_free_blocks(inode, le32_to_cpu(branch[i].key), 1);
422 return err;
426 * ext2_splice_branch - splice the allocated branch onto inode.
427 * @inode: owner
428 * @block: (logical) number of block we are adding
429 * @chain: chain of indirect blocks (with a missing link - see
430 * ext2_alloc_branch)
431 * @where: location of missing link
432 * @num: number of blocks we are adding
434 * This function verifies that chain (up to the missing link) had not
435 * changed, fills the missing link and does all housekeeping needed in
436 * inode (->i_blocks, etc.). In case of success we end up with the full
437 * chain to new block and return 0. Otherwise (== chain had been changed)
438 * we free the new blocks (forgetting their buffer_heads, indeed) and
439 * return -EAGAIN.
442 static inline int ext2_splice_branch(struct inode *inode,
443 long block,
444 Indirect chain[4],
445 Indirect *where,
446 int num)
448 int i;
450 /* Verify that place we are splicing to is still there and vacant */
452 /* Writer: pointers, ->i_next_alloc*, ->i_blocks */
453 if (!verify_chain(chain, where-1) || *where->p)
454 /* Writer: end */
455 goto changed;
457 /* That's it */
459 *where->p = where->key;
460 inode->u.ext2_i.i_next_alloc_block = block;
461 inode->u.ext2_i.i_next_alloc_goal = le32_to_cpu(where[num-1].key);
462 inode->i_blocks += num * inode->i_sb->s_blocksize/512;
464 /* Writer: end */
466 /* We are done with atomic stuff, now do the rest of housekeeping */
468 inode->i_ctime = CURRENT_TIME;
470 /* had we spliced it onto indirect block? */
471 if (where->bh) {
472 mark_buffer_dirty_inode(where->bh, inode);
473 if (IS_SYNC(inode) || inode->u.ext2_i.i_osync) {
474 ll_rw_block (WRITE, 1, &where->bh);
475 wait_on_buffer(where->bh);
479 if (IS_SYNC(inode) || inode->u.ext2_i.i_osync)
480 ext2_sync_inode (inode);
481 else
482 mark_inode_dirty(inode);
483 return 0;
485 changed:
486 for (i = 1; i < num; i++)
487 bforget(where[i].bh);
488 for (i = 0; i < num; i++)
489 ext2_free_blocks(inode, le32_to_cpu(where[i].key), 1);
490 return -EAGAIN;
494 * Allocation strategy is simple: if we have to allocate something, we will
495 * have to go the whole way to leaf. So let's do it before attaching anything
496 * to tree, set linkage between the newborn blocks, write them if sync is
497 * required, recheck the path, free and repeat if check fails, otherwise
498 * set the last missing link (that will protect us from any truncate-generated
499 * removals - all blocks on the path are immune now) and possibly force the
500 * write on the parent block.
501 * That has a nice additional property: no special recovery from the failed
502 * allocations is needed - we simply release blocks and do not touch anything
503 * reachable from inode.
506 static int ext2_get_block(struct inode *inode, long iblock, struct buffer_head *bh_result, int create)
508 int err = -EIO;
509 int offsets[4];
510 Indirect chain[4];
511 Indirect *partial;
512 unsigned long goal;
513 int left;
514 int depth = ext2_block_to_path(inode, iblock, offsets);
516 if (depth == 0)
517 goto out;
519 lock_kernel();
520 reread:
521 partial = ext2_get_branch(inode, depth, offsets, chain, &err);
523 /* Simplest case - block found, no allocation needed */
524 if (!partial) {
525 got_it:
526 bh_result->b_dev = inode->i_dev;
527 bh_result->b_blocknr = le32_to_cpu(chain[depth-1].key);
528 bh_result->b_state |= (1UL << BH_Mapped);
529 /* Clean up and exit */
530 partial = chain+depth-1; /* the whole chain */
531 goto cleanup;
534 /* Next simple case - plain lookup or failed read of indirect block */
535 if (!create || err == -EIO) {
536 cleanup:
537 while (partial > chain) {
538 brelse(partial->bh);
539 partial--;
541 unlock_kernel();
542 out:
543 return err;
547 * Indirect block might be removed by truncate while we were
548 * reading it. Handling of that case (forget what we've got and
549 * reread) is taken out of the main path.
551 if (err == -EAGAIN)
552 goto changed;
554 if (ext2_find_goal(inode, iblock, chain, partial, &goal) < 0)
555 goto changed;
557 left = (chain + depth) - partial;
558 err = ext2_alloc_branch(inode, left, goal,
559 offsets+(partial-chain), partial);
560 if (err)
561 goto cleanup;
563 if (ext2_splice_branch(inode, iblock, chain, partial, left) < 0)
564 goto changed;
566 bh_result->b_state |= (1UL << BH_New);
567 goto got_it;
569 changed:
570 while (partial > chain) {
571 bforget(partial->bh);
572 partial--;
574 goto reread;
577 struct buffer_head * ext2_getblk(struct inode * inode, long block, int create, int * err)
579 struct buffer_head dummy;
580 int error;
582 dummy.b_state = 0;
583 dummy.b_blocknr = -1000;
584 error = ext2_get_block(inode, block, &dummy, create);
585 *err = error;
586 if (!error && buffer_mapped(&dummy)) {
587 struct buffer_head *bh;
588 bh = getblk(dummy.b_dev, dummy.b_blocknr, inode->i_sb->s_blocksize);
589 if (buffer_new(&dummy)) {
590 if (!buffer_uptodate(bh))
591 wait_on_buffer(bh);
592 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
593 mark_buffer_uptodate(bh, 1);
594 mark_buffer_dirty_inode(bh, inode);
596 return bh;
598 return NULL;
601 struct buffer_head * ext2_bread (struct inode * inode, int block,
602 int create, int *err)
604 struct buffer_head * bh;
605 int prev_blocks;
607 prev_blocks = inode->i_blocks;
609 bh = ext2_getblk (inode, block, create, err);
610 if (!bh)
611 return bh;
614 * If the inode has grown, and this is a directory, then perform
615 * preallocation of a few more blocks to try to keep directory
616 * fragmentation down.
618 if (create &&
619 S_ISDIR(inode->i_mode) &&
620 inode->i_blocks > prev_blocks &&
621 EXT2_HAS_COMPAT_FEATURE(inode->i_sb,
622 EXT2_FEATURE_COMPAT_DIR_PREALLOC)) {
623 int i;
624 struct buffer_head *tmp_bh;
626 for (i = 1;
627 i < EXT2_SB(inode->i_sb)->s_es->s_prealloc_dir_blocks;
628 i++) {
630 * ext2_getblk will zero out the contents of the
631 * directory for us
633 tmp_bh = ext2_getblk(inode, block+i, create, err);
634 if (!tmp_bh) {
635 brelse (bh);
636 return 0;
638 brelse (tmp_bh);
642 if (buffer_uptodate(bh))
643 return bh;
644 ll_rw_block (READ, 1, &bh);
645 wait_on_buffer (bh);
646 if (buffer_uptodate(bh))
647 return bh;
648 brelse (bh);
649 *err = -EIO;
650 return NULL;
653 static int ext2_writepage(struct file *file, struct page *page)
655 return block_write_full_page(page,ext2_get_block);
657 static int ext2_readpage(struct file *file, struct page *page)
659 return block_read_full_page(page,ext2_get_block);
661 static int ext2_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
663 return block_prepare_write(page,from,to,ext2_get_block);
665 static int ext2_bmap(struct address_space *mapping, long block)
667 return generic_block_bmap(mapping,block,ext2_get_block);
669 struct address_space_operations ext2_aops = {
670 readpage: ext2_readpage,
671 writepage: ext2_writepage,
672 sync_page: block_sync_page,
673 prepare_write: ext2_prepare_write,
674 commit_write: generic_commit_write,
675 bmap: ext2_bmap
679 * Probably it should be a library function... search for first non-zero word
680 * or memcmp with zero_page, whatever is better for particular architecture.
681 * Linus?
683 static inline int all_zeroes(u32 *p, u32 *q)
685 while (p < q)
686 if (*p++)
687 return 0;
688 return 1;
692 * ext2_find_shared - find the indirect blocks for partial truncation.
693 * @inode: inode in question
694 * @depth: depth of the affected branch
695 * @offsets: offsets of pointers in that branch (see ext2_block_to_path)
696 * @chain: place to store the pointers to partial indirect blocks
697 * @top: place to the (detached) top of branch
699 * This is a helper function used by ext2_truncate().
701 * When we do truncate() we may have to clean the ends of several indirect
702 * blocks but leave the blocks themselves alive. Block is partially
703 * truncated if some data below the new i_size is refered from it (and
704 * it is on the path to the first completely truncated data block, indeed).
705 * We have to free the top of that path along with everything to the right
706 * of the path. Since no allocation past the truncation point is possible
707 * until ext2_truncate() finishes, we may safely do the latter, but top
708 * of branch may require special attention - pageout below the truncation
709 * point might try to populate it.
711 * We atomically detach the top of branch from the tree, store the block
712 * number of its root in *@top, pointers to buffer_heads of partially
713 * truncated blocks - in @chain[].bh and pointers to their last elements
714 * that should not be removed - in @chain[].p. Return value is the pointer
715 * to last filled element of @chain.
717 * The work left to caller to do the actual freeing of subtrees:
718 * a) free the subtree starting from *@top
719 * b) free the subtrees whose roots are stored in
720 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
721 * c) free the subtrees growing from the inode past the @chain[0].p
722 * (no partially truncated stuff there).
725 static Indirect *ext2_find_shared(struct inode *inode,
726 int depth,
727 int offsets[4],
728 Indirect chain[4],
729 u32 *top)
731 Indirect *partial, *p;
732 int k, err;
734 *top = 0;
735 for (k = depth; k > 1 && !offsets[k-1]; k--)
737 partial = ext2_get_branch(inode, k, offsets, chain, &err);
738 /* Writer: pointers */
739 if (!partial)
740 partial = chain + k-1;
742 * If the branch acquired continuation since we've looked at it -
743 * fine, it should all survive and (new) top doesn't belong to us.
745 if (!partial->key && *partial->p)
746 /* Writer: end */
747 goto no_top;
748 for (p=partial; p>chain && all_zeroes((u32*)p->bh->b_data,p->p); p--)
751 * OK, we've found the last block that must survive. The rest of our
752 * branch should be detached before unlocking. However, if that rest
753 * of branch is all ours and does not grow immediately from the inode
754 * it's easier to cheat and just decrement partial->p.
756 if (p == chain + k - 1 && p > chain) {
757 p->p--;
758 } else {
759 *top = *p->p;
760 *p->p = 0;
762 /* Writer: end */
764 while(partial > p)
766 brelse(partial->bh);
767 partial--;
769 no_top:
770 return partial;
774 * ext2_free_data - free a list of data blocks
775 * @inode: inode we are dealing with
776 * @p: array of block numbers
777 * @q: points immediately past the end of array
779 * We are freeing all blocks refered from that array (numbers are
780 * stored as little-endian 32-bit) and updating @inode->i_blocks
781 * appropriately.
783 static inline void ext2_free_data(struct inode *inode, u32 *p, u32 *q)
785 int blocks = inode->i_sb->s_blocksize / 512;
786 unsigned long block_to_free = 0, count = 0;
787 unsigned long nr;
789 for ( ; p < q ; p++) {
790 nr = le32_to_cpu(*p);
791 if (nr) {
792 *p = 0;
793 /* accumulate blocks to free if they're contiguous */
794 if (count == 0)
795 goto free_this;
796 else if (block_to_free == nr - count)
797 count++;
798 else {
799 /* Writer: ->i_blocks */
800 inode->i_blocks -= blocks * count;
801 /* Writer: end */
802 ext2_free_blocks (inode, block_to_free, count);
803 mark_inode_dirty(inode);
804 free_this:
805 block_to_free = nr;
806 count = 1;
810 if (count > 0) {
811 /* Writer: ->i_blocks */
812 inode->i_blocks -= blocks * count;
813 /* Writer: end */
814 ext2_free_blocks (inode, block_to_free, count);
815 mark_inode_dirty(inode);
820 * ext2_free_branches - free an array of branches
821 * @inode: inode we are dealing with
822 * @p: array of block numbers
823 * @q: pointer immediately past the end of array
824 * @depth: depth of the branches to free
826 * We are freeing all blocks refered from these branches (numbers are
827 * stored as little-endian 32-bit) and updating @inode->i_blocks
828 * appropriately.
830 static void ext2_free_branches(struct inode *inode, u32 *p, u32 *q, int depth)
832 struct buffer_head * bh;
833 unsigned long nr;
835 if (depth--) {
836 int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
837 for ( ; p < q ; p++) {
838 nr = le32_to_cpu(*p);
839 if (!nr)
840 continue;
841 *p = 0;
842 bh = bread (inode->i_dev, nr, inode->i_sb->s_blocksize);
844 * A read failure? Report error and clear slot
845 * (should be rare).
847 if (!bh) {
848 ext2_error(inode->i_sb, "ext2_free_branches",
849 "Read failure, inode=%ld, block=%ld",
850 inode->i_ino, nr);
851 continue;
853 ext2_free_branches(inode,
854 (u32*)bh->b_data,
855 (u32*)bh->b_data + addr_per_block,
856 depth);
857 bforget(bh);
858 /* Writer: ->i_blocks */
859 inode->i_blocks -= inode->i_sb->s_blocksize / 512;
860 /* Writer: end */
861 ext2_free_blocks(inode, nr, 1);
862 mark_inode_dirty(inode);
864 } else
865 ext2_free_data(inode, p, q);
868 void ext2_truncate (struct inode * inode)
870 u32 *i_data = inode->u.ext2_i.i_data;
871 int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
872 int offsets[4];
873 Indirect chain[4];
874 Indirect *partial;
875 int nr = 0;
876 int n;
877 long iblock;
878 unsigned blocksize;
880 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
881 S_ISLNK(inode->i_mode)))
882 return;
883 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
884 return;
886 ext2_discard_prealloc(inode);
888 blocksize = inode->i_sb->s_blocksize;
889 iblock = (inode->i_size + blocksize-1)
890 >> EXT2_BLOCK_SIZE_BITS(inode->i_sb);
892 block_truncate_page(inode->i_mapping, inode->i_size, ext2_get_block);
894 n = ext2_block_to_path(inode, iblock, offsets);
895 if (n == 0)
896 return;
898 if (n == 1) {
899 ext2_free_data(inode, i_data+offsets[0],
900 i_data + EXT2_NDIR_BLOCKS);
901 goto do_indirects;
904 partial = ext2_find_shared(inode, n, offsets, chain, &nr);
905 /* Kill the top of shared branch (already detached) */
906 if (nr) {
907 if (partial == chain)
908 mark_inode_dirty(inode);
909 else
910 mark_buffer_dirty_inode(partial->bh, inode);
911 ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
913 /* Clear the ends of indirect blocks on the shared branch */
914 while (partial > chain) {
915 ext2_free_branches(inode,
916 partial->p + 1,
917 (u32*)partial->bh->b_data + addr_per_block,
918 (chain+n-1) - partial);
919 mark_buffer_dirty_inode(partial->bh, inode);
920 if (IS_SYNC(inode)) {
921 ll_rw_block (WRITE, 1, &partial->bh);
922 wait_on_buffer (partial->bh);
924 brelse (partial->bh);
925 partial--;
927 do_indirects:
928 /* Kill the remaining (whole) subtrees */
929 switch (offsets[0]) {
930 default:
931 nr = i_data[EXT2_IND_BLOCK];
932 if (nr) {
933 i_data[EXT2_IND_BLOCK] = 0;
934 mark_inode_dirty(inode);
935 ext2_free_branches(inode, &nr, &nr+1, 1);
937 case EXT2_IND_BLOCK:
938 nr = i_data[EXT2_DIND_BLOCK];
939 if (nr) {
940 i_data[EXT2_DIND_BLOCK] = 0;
941 mark_inode_dirty(inode);
942 ext2_free_branches(inode, &nr, &nr+1, 2);
944 case EXT2_DIND_BLOCK:
945 nr = i_data[EXT2_TIND_BLOCK];
946 if (nr) {
947 i_data[EXT2_TIND_BLOCK] = 0;
948 mark_inode_dirty(inode);
949 ext2_free_branches(inode, &nr, &nr+1, 3);
951 case EXT2_TIND_BLOCK:
954 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
955 if (IS_SYNC(inode))
956 ext2_sync_inode (inode);
957 else
958 mark_inode_dirty(inode);
961 void ext2_read_inode (struct inode * inode)
963 struct buffer_head * bh;
964 struct ext2_inode * raw_inode;
965 unsigned long block_group;
966 unsigned long group_desc;
967 unsigned long desc;
968 unsigned long block;
969 unsigned long offset;
970 struct ext2_group_desc * gdp;
972 if ((inode->i_ino != EXT2_ROOT_INO && inode->i_ino != EXT2_ACL_IDX_INO &&
973 inode->i_ino != EXT2_ACL_DATA_INO &&
974 inode->i_ino < EXT2_FIRST_INO(inode->i_sb)) ||
975 inode->i_ino > le32_to_cpu(inode->i_sb->u.ext2_sb.s_es->s_inodes_count)) {
976 ext2_error (inode->i_sb, "ext2_read_inode",
977 "bad inode number: %lu", inode->i_ino);
978 goto bad_inode;
980 block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
981 if (block_group >= inode->i_sb->u.ext2_sb.s_groups_count) {
982 ext2_error (inode->i_sb, "ext2_read_inode",
983 "group >= groups count");
984 goto bad_inode;
986 group_desc = block_group >> EXT2_DESC_PER_BLOCK_BITS(inode->i_sb);
987 desc = block_group & (EXT2_DESC_PER_BLOCK(inode->i_sb) - 1);
988 bh = inode->i_sb->u.ext2_sb.s_group_desc[group_desc];
989 if (!bh) {
990 ext2_error (inode->i_sb, "ext2_read_inode",
991 "Descriptor not loaded");
992 goto bad_inode;
995 gdp = (struct ext2_group_desc *) bh->b_data;
997 * Figure out the offset within the block group inode table
999 offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) *
1000 EXT2_INODE_SIZE(inode->i_sb);
1001 block = le32_to_cpu(gdp[desc].bg_inode_table) +
1002 (offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb));
1003 if (!(bh = bread (inode->i_dev, block, inode->i_sb->s_blocksize))) {
1004 ext2_error (inode->i_sb, "ext2_read_inode",
1005 "unable to read inode block - "
1006 "inode=%lu, block=%lu", inode->i_ino, block);
1007 goto bad_inode;
1009 offset &= (EXT2_BLOCK_SIZE(inode->i_sb) - 1);
1010 raw_inode = (struct ext2_inode *) (bh->b_data + offset);
1012 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
1013 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
1014 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
1015 if(!(test_opt (inode->i_sb, NO_UID32))) {
1016 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
1017 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
1019 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
1020 inode->i_size = le32_to_cpu(raw_inode->i_size);
1021 inode->i_atime = le32_to_cpu(raw_inode->i_atime);
1022 inode->i_ctime = le32_to_cpu(raw_inode->i_ctime);
1023 inode->i_mtime = le32_to_cpu(raw_inode->i_mtime);
1024 inode->u.ext2_i.i_dtime = le32_to_cpu(raw_inode->i_dtime);
1025 /* We now have enough fields to check if the inode was active or not.
1026 * This is needed because nfsd might try to access dead inodes
1027 * the test is that same one that e2fsck uses
1028 * NeilBrown 1999oct15
1030 if (inode->i_nlink == 0 && (inode->i_mode == 0 || inode->u.ext2_i.i_dtime)) {
1031 /* this inode is deleted */
1032 brelse (bh);
1033 goto bad_inode;
1035 inode->i_blksize = PAGE_SIZE; /* This is the optimal IO size (for stat), not the fs block size */
1036 inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
1037 inode->i_version = ++event;
1038 inode->u.ext2_i.i_flags = le32_to_cpu(raw_inode->i_flags);
1039 inode->u.ext2_i.i_faddr = le32_to_cpu(raw_inode->i_faddr);
1040 inode->u.ext2_i.i_frag_no = raw_inode->i_frag;
1041 inode->u.ext2_i.i_frag_size = raw_inode->i_fsize;
1042 inode->u.ext2_i.i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
1043 if (S_ISDIR(inode->i_mode))
1044 inode->u.ext2_i.i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
1045 else {
1046 inode->u.ext2_i.i_high_size = le32_to_cpu(raw_inode->i_size_high);
1047 inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
1049 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
1050 inode->u.ext2_i.i_block_group = block_group;
1053 * NOTE! The in-memory inode i_data array is in little-endian order
1054 * even on big-endian machines: we do NOT byteswap the block numbers!
1056 for (block = 0; block < EXT2_N_BLOCKS; block++)
1057 inode->u.ext2_i.i_data[block] = raw_inode->i_block[block];
1059 if (inode->i_ino == EXT2_ACL_IDX_INO ||
1060 inode->i_ino == EXT2_ACL_DATA_INO)
1061 /* Nothing to do */ ;
1062 else if (S_ISREG(inode->i_mode)) {
1063 inode->i_op = &ext2_file_inode_operations;
1064 inode->i_fop = &ext2_file_operations;
1065 inode->i_mapping->a_ops = &ext2_aops;
1066 } else if (S_ISDIR(inode->i_mode)) {
1067 inode->i_op = &ext2_dir_inode_operations;
1068 inode->i_fop = &ext2_dir_operations;
1069 } else if (S_ISLNK(inode->i_mode)) {
1070 if (!inode->i_blocks)
1071 inode->i_op = &ext2_fast_symlink_inode_operations;
1072 else {
1073 inode->i_op = &page_symlink_inode_operations;
1074 inode->i_mapping->a_ops = &ext2_aops;
1076 } else
1077 init_special_inode(inode, inode->i_mode,
1078 le32_to_cpu(raw_inode->i_block[0]));
1079 brelse (bh);
1080 inode->i_attr_flags = 0;
1081 if (inode->u.ext2_i.i_flags & EXT2_SYNC_FL) {
1082 inode->i_attr_flags |= ATTR_FLAG_SYNCRONOUS;
1083 inode->i_flags |= S_SYNC;
1085 if (inode->u.ext2_i.i_flags & EXT2_APPEND_FL) {
1086 inode->i_attr_flags |= ATTR_FLAG_APPEND;
1087 inode->i_flags |= S_APPEND;
1089 if (inode->u.ext2_i.i_flags & EXT2_IMMUTABLE_FL) {
1090 inode->i_attr_flags |= ATTR_FLAG_IMMUTABLE;
1091 inode->i_flags |= S_IMMUTABLE;
1093 if (inode->u.ext2_i.i_flags & EXT2_NOATIME_FL) {
1094 inode->i_attr_flags |= ATTR_FLAG_NOATIME;
1095 inode->i_flags |= S_NOATIME;
1097 return;
1099 bad_inode:
1100 make_bad_inode(inode);
1101 return;
1104 static int ext2_update_inode(struct inode * inode, int do_sync)
1106 struct buffer_head * bh;
1107 struct ext2_inode * raw_inode;
1108 unsigned long block_group;
1109 unsigned long group_desc;
1110 unsigned long desc;
1111 unsigned long block;
1112 unsigned long offset;
1113 int err = 0;
1114 struct ext2_group_desc * gdp;
1116 if ((inode->i_ino != EXT2_ROOT_INO &&
1117 inode->i_ino < EXT2_FIRST_INO(inode->i_sb)) ||
1118 inode->i_ino > le32_to_cpu(inode->i_sb->u.ext2_sb.s_es->s_inodes_count)) {
1119 ext2_error (inode->i_sb, "ext2_write_inode",
1120 "bad inode number: %lu", inode->i_ino);
1121 return -EIO;
1123 block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
1124 if (block_group >= inode->i_sb->u.ext2_sb.s_groups_count) {
1125 ext2_error (inode->i_sb, "ext2_write_inode",
1126 "group >= groups count");
1127 return -EIO;
1129 group_desc = block_group >> EXT2_DESC_PER_BLOCK_BITS(inode->i_sb);
1130 desc = block_group & (EXT2_DESC_PER_BLOCK(inode->i_sb) - 1);
1131 bh = inode->i_sb->u.ext2_sb.s_group_desc[group_desc];
1132 if (!bh) {
1133 ext2_error (inode->i_sb, "ext2_write_inode",
1134 "Descriptor not loaded");
1135 return -EIO;
1137 gdp = (struct ext2_group_desc *) bh->b_data;
1139 * Figure out the offset within the block group inode table
1141 offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) *
1142 EXT2_INODE_SIZE(inode->i_sb);
1143 block = le32_to_cpu(gdp[desc].bg_inode_table) +
1144 (offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb));
1145 if (!(bh = bread (inode->i_dev, block, inode->i_sb->s_blocksize))) {
1146 ext2_error (inode->i_sb, "ext2_write_inode",
1147 "unable to read inode block - "
1148 "inode=%lu, block=%lu", inode->i_ino, block);
1149 return -EIO;
1151 offset &= EXT2_BLOCK_SIZE(inode->i_sb) - 1;
1152 raw_inode = (struct ext2_inode *) (bh->b_data + offset);
1154 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
1155 if(!(test_opt(inode->i_sb, NO_UID32))) {
1156 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
1157 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
1159 * Fix up interoperability with old kernels. Otherwise, old inodes get
1160 * re-used with the upper 16 bits of the uid/gid intact
1162 if(!inode->u.ext2_i.i_dtime) {
1163 raw_inode->i_uid_high = cpu_to_le16(high_16_bits(inode->i_uid));
1164 raw_inode->i_gid_high = cpu_to_le16(high_16_bits(inode->i_gid));
1165 } else {
1166 raw_inode->i_uid_high = 0;
1167 raw_inode->i_gid_high = 0;
1169 } else {
1170 raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(inode->i_uid));
1171 raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(inode->i_gid));
1172 raw_inode->i_uid_high = 0;
1173 raw_inode->i_gid_high = 0;
1175 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
1176 raw_inode->i_size = cpu_to_le32(inode->i_size);
1177 raw_inode->i_atime = cpu_to_le32(inode->i_atime);
1178 raw_inode->i_ctime = cpu_to_le32(inode->i_ctime);
1179 raw_inode->i_mtime = cpu_to_le32(inode->i_mtime);
1180 raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
1181 raw_inode->i_dtime = cpu_to_le32(inode->u.ext2_i.i_dtime);
1182 raw_inode->i_flags = cpu_to_le32(inode->u.ext2_i.i_flags);
1183 raw_inode->i_faddr = cpu_to_le32(inode->u.ext2_i.i_faddr);
1184 raw_inode->i_frag = inode->u.ext2_i.i_frag_no;
1185 raw_inode->i_fsize = inode->u.ext2_i.i_frag_size;
1186 raw_inode->i_file_acl = cpu_to_le32(inode->u.ext2_i.i_file_acl);
1187 if (S_ISDIR(inode->i_mode))
1188 raw_inode->i_dir_acl = cpu_to_le32(inode->u.ext2_i.i_dir_acl);
1189 else {
1190 raw_inode->i_size_high = cpu_to_le32(inode->i_size >> 32);
1191 if (raw_inode->i_size_high) {
1192 struct super_block *sb = inode->i_sb;
1193 struct ext2_super_block *es = sb->u.ext2_sb.s_es;
1194 if (!(es->s_feature_ro_compat & cpu_to_le32(EXT2_FEATURE_RO_COMPAT_LARGE_FILE))) {
1195 /* If this is the first large file
1196 * created, add a flag to the superblock.
1198 lock_kernel();
1199 es->s_feature_ro_compat |= cpu_to_le32(EXT2_FEATURE_RO_COMPAT_LARGE_FILE);
1200 unlock_kernel();
1201 ext2_write_super(sb);
1206 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
1207 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
1208 raw_inode->i_block[0] = cpu_to_le32(kdev_t_to_nr(inode->i_rdev));
1209 else for (block = 0; block < EXT2_N_BLOCKS; block++)
1210 raw_inode->i_block[block] = inode->u.ext2_i.i_data[block];
1211 mark_buffer_dirty_inode(bh, inode);
1212 if (do_sync) {
1213 ll_rw_block (WRITE, 1, &bh);
1214 wait_on_buffer (bh);
1215 if (buffer_req(bh) && !buffer_uptodate(bh)) {
1216 printk ("IO error syncing ext2 inode ["
1217 "%s:%08lx]\n",
1218 bdevname(inode->i_dev), inode->i_ino);
1219 err = -EIO;
1222 brelse (bh);
1223 return err;
1226 void ext2_write_inode (struct inode * inode, int wait)
1228 lock_kernel();
1229 ext2_update_inode (inode, wait);
1230 unlock_kernel();
1233 int ext2_sync_inode (struct inode *inode)
1235 return ext2_update_inode (inode, 1);
1238 int ext2_notify_change(struct dentry *dentry, struct iattr *iattr)
1240 struct inode *inode = dentry->d_inode;
1241 int retval;
1242 unsigned int flags;
1244 retval = -EPERM;
1245 if (iattr->ia_valid & ATTR_ATTR_FLAG &&
1246 ((!(iattr->ia_attr_flags & ATTR_FLAG_APPEND) !=
1247 !(inode->u.ext2_i.i_flags & EXT2_APPEND_FL)) ||
1248 (!(iattr->ia_attr_flags & ATTR_FLAG_IMMUTABLE) !=
1249 !(inode->u.ext2_i.i_flags & EXT2_IMMUTABLE_FL)))) {
1250 if (!capable(CAP_LINUX_IMMUTABLE))
1251 goto out;
1252 } else if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
1253 goto out;
1255 retval = inode_change_ok(inode, iattr);
1256 if (retval != 0)
1257 goto out;
1259 inode_setattr(inode, iattr);
1261 flags = iattr->ia_attr_flags;
1262 if (flags & ATTR_FLAG_SYNCRONOUS) {
1263 inode->i_flags |= S_SYNC;
1264 inode->u.ext2_i.i_flags |= EXT2_SYNC_FL;
1265 } else {
1266 inode->i_flags &= ~S_SYNC;
1267 inode->u.ext2_i.i_flags &= ~EXT2_SYNC_FL;
1269 if (flags & ATTR_FLAG_NOATIME) {
1270 inode->i_flags |= S_NOATIME;
1271 inode->u.ext2_i.i_flags |= EXT2_NOATIME_FL;
1272 } else {
1273 inode->i_flags &= ~S_NOATIME;
1274 inode->u.ext2_i.i_flags &= ~EXT2_NOATIME_FL;
1276 if (flags & ATTR_FLAG_APPEND) {
1277 inode->i_flags |= S_APPEND;
1278 inode->u.ext2_i.i_flags |= EXT2_APPEND_FL;
1279 } else {
1280 inode->i_flags &= ~S_APPEND;
1281 inode->u.ext2_i.i_flags &= ~EXT2_APPEND_FL;
1283 if (flags & ATTR_FLAG_IMMUTABLE) {
1284 inode->i_flags |= S_IMMUTABLE;
1285 inode->u.ext2_i.i_flags |= EXT2_IMMUTABLE_FL;
1286 } else {
1287 inode->i_flags &= ~S_IMMUTABLE;
1288 inode->u.ext2_i.i_flags &= ~EXT2_IMMUTABLE_FL;
1290 mark_inode_dirty(inode);
1291 out:
1292 return retval;