- pre5:
[davej-history.git] / fs / ext2 / inode.c
blob720597b36a3d273577ba81f8e46838406407457c
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/locks.h>
27 #include <linux/smp_lock.h>
28 #include <linux/sched.h>
29 #include <linux/highuid.h>
31 static int ext2_update_inode(struct inode * inode, int do_sync);
34 * Called at each iput()
36 void ext2_put_inode (struct inode * inode)
38 ext2_discard_prealloc (inode);
42 * Called at the last iput() if i_nlink is zero.
44 void ext2_delete_inode (struct inode * inode)
46 lock_kernel();
48 if (is_bad_inode(inode) ||
49 inode->i_ino == EXT2_ACL_IDX_INO ||
50 inode->i_ino == EXT2_ACL_DATA_INO)
51 goto no_delete;
52 inode->u.ext2_i.i_dtime = CURRENT_TIME;
53 mark_inode_dirty(inode);
54 ext2_update_inode(inode, IS_SYNC(inode));
55 inode->i_size = 0;
56 if (inode->i_blocks)
57 ext2_truncate (inode);
58 ext2_free_inode (inode);
60 unlock_kernel();
61 return;
62 no_delete:
63 unlock_kernel();
64 clear_inode(inode); /* We must guarantee clearing of inode... */
67 void ext2_discard_prealloc (struct inode * inode)
69 #ifdef EXT2_PREALLOCATE
70 lock_kernel();
71 /* Writer: ->i_prealloc* */
72 if (inode->u.ext2_i.i_prealloc_count) {
73 unsigned short total = inode->u.ext2_i.i_prealloc_count;
74 unsigned long block = inode->u.ext2_i.i_prealloc_block;
75 inode->u.ext2_i.i_prealloc_count = 0;
76 inode->u.ext2_i.i_prealloc_block = 0;
77 /* Writer: end */
78 ext2_free_blocks (inode, block, total);
80 unlock_kernel();
81 #endif
84 static int ext2_alloc_block (struct inode * inode, unsigned long goal, int *err)
86 #ifdef EXT2FS_DEBUG
87 static unsigned long alloc_hits = 0, alloc_attempts = 0;
88 #endif
89 unsigned long result;
92 #ifdef EXT2_PREALLOCATE
93 /* Writer: ->i_prealloc* */
94 if (inode->u.ext2_i.i_prealloc_count &&
95 (goal == inode->u.ext2_i.i_prealloc_block ||
96 goal + 1 == inode->u.ext2_i.i_prealloc_block))
98 result = inode->u.ext2_i.i_prealloc_block++;
99 inode->u.ext2_i.i_prealloc_count--;
100 /* Writer: end */
101 #ifdef EXT2FS_DEBUG
102 ext2_debug ("preallocation hit (%lu/%lu).\n",
103 ++alloc_hits, ++alloc_attempts);
104 #endif
105 } else {
106 ext2_discard_prealloc (inode);
107 #ifdef EXT2FS_DEBUG
108 ext2_debug ("preallocation miss (%lu/%lu).\n",
109 alloc_hits, ++alloc_attempts);
110 #endif
111 if (S_ISREG(inode->i_mode))
112 result = ext2_new_block (inode, goal,
113 &inode->u.ext2_i.i_prealloc_count,
114 &inode->u.ext2_i.i_prealloc_block, err);
115 else
116 result = ext2_new_block (inode, goal, 0, 0, err);
118 #else
119 result = ext2_new_block (inode, goal, 0, 0, err);
120 #endif
121 return result;
124 typedef struct {
125 u32 *p;
126 u32 key;
127 struct buffer_head *bh;
128 } Indirect;
130 static inline void add_chain(Indirect *p, struct buffer_head *bh, u32 *v)
132 p->key = *(p->p = v);
133 p->bh = bh;
136 static inline int verify_chain(Indirect *from, Indirect *to)
138 while (from <= to && from->key == *from->p)
139 from++;
140 return (from > to);
144 * ext2_block_to_path - parse the block number into array of offsets
145 * @inode: inode in question (we are only interested in its superblock)
146 * @i_block: block number to be parsed
147 * @offsets: array to store the offsets in
149 * To store the locations of file's data ext2 uses a data structure common
150 * for UNIX filesystems - tree of pointers anchored in the inode, with
151 * data blocks at leaves and indirect blocks in intermediate nodes.
152 * This function translates the block number into path in that tree -
153 * return value is the path length and @offsets[n] is the offset of
154 * pointer to (n+1)th node in the nth one. If @block is out of range
155 * (negative or too large) warning is printed and zero returned.
157 * Note: function doesn't find node addresses, so no IO is needed. All
158 * we need to know is the capacity of indirect blocks (taken from the
159 * inode->i_sb).
163 * Portability note: the last comparison (check that we fit into triple
164 * indirect block) is spelled differently, because otherwise on an
165 * architecture with 32-bit longs and 8Kb pages we might get into trouble
166 * if our filesystem had 8Kb blocks. We might use long long, but that would
167 * kill us on x86. Oh, well, at least the sign propagation does not matter -
168 * i_block would have to be negative in the very beginning, so we would not
169 * get there at all.
172 static int ext2_block_to_path(struct inode *inode, long i_block, int offsets[4])
174 int ptrs = EXT2_ADDR_PER_BLOCK(inode->i_sb);
175 int ptrs_bits = EXT2_ADDR_PER_BLOCK_BITS(inode->i_sb);
176 const long direct_blocks = EXT2_NDIR_BLOCKS,
177 indirect_blocks = ptrs,
178 double_blocks = (1 << (ptrs_bits * 2));
179 int n = 0;
181 if (i_block < 0) {
182 ext2_warning (inode->i_sb, "ext2_block_to_path", "block < 0");
183 } else if (i_block < direct_blocks) {
184 offsets[n++] = i_block;
185 } else if ( (i_block -= direct_blocks) < indirect_blocks) {
186 offsets[n++] = EXT2_IND_BLOCK;
187 offsets[n++] = i_block;
188 } else if ((i_block -= indirect_blocks) < double_blocks) {
189 offsets[n++] = EXT2_DIND_BLOCK;
190 offsets[n++] = i_block >> ptrs_bits;
191 offsets[n++] = i_block & (ptrs - 1);
192 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
193 offsets[n++] = EXT2_TIND_BLOCK;
194 offsets[n++] = i_block >> (ptrs_bits * 2);
195 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
196 offsets[n++] = i_block & (ptrs - 1);
197 } else {
198 ext2_warning (inode->i_sb, "ext2_block_to_path", "block > big");
200 return n;
204 * ext2_get_branch - read the chain of indirect blocks leading to data
205 * @inode: inode in question
206 * @depth: depth of the chain (1 - direct pointer, etc.)
207 * @offsets: offsets of pointers in inode/indirect blocks
208 * @chain: place to store the result
209 * @err: here we store the error value
211 * Function fills the array of triples <key, p, bh> and returns %NULL
212 * if everything went OK or the pointer to the last filled triple
213 * (incomplete one) otherwise. Upon the return chain[i].key contains
214 * the number of (i+1)-th block in the chain (as it is stored in memory,
215 * i.e. little-endian 32-bit), chain[i].p contains the address of that
216 * number (it points into struct inode for i==0 and into the bh->b_data
217 * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
218 * block for i>0 and NULL for i==0. In other words, it holds the block
219 * numbers of the chain, addresses they were taken from (and where we can
220 * verify that chain did not change) and buffer_heads hosting these
221 * numbers.
223 * Function stops when it stumbles upon zero pointer (absent block)
224 * (pointer to last triple returned, *@err == 0)
225 * or when it gets an IO error reading an indirect block
226 * (ditto, *@err == -EIO)
227 * or when it notices that chain had been changed while it was reading
228 * (ditto, *@err == -EAGAIN)
229 * or when it reads all @depth-1 indirect blocks successfully and finds
230 * the whole chain, all way to the data (returns %NULL, *err == 0).
232 static inline Indirect *ext2_get_branch(struct inode *inode,
233 int depth,
234 int *offsets,
235 Indirect chain[4],
236 int *err)
238 kdev_t dev = inode->i_dev;
239 int size = inode->i_sb->s_blocksize;
240 Indirect *p = chain;
241 struct buffer_head *bh;
243 *err = 0;
244 /* i_data is not going away, no lock needed */
245 add_chain (chain, NULL, inode->u.ext2_i.i_data + *offsets);
246 if (!p->key)
247 goto no_block;
248 while (--depth) {
249 bh = bread(dev, le32_to_cpu(p->key), size);
250 if (!bh)
251 goto failure;
252 /* Reader: pointers */
253 if (!verify_chain(chain, p))
254 goto changed;
255 add_chain(++p, bh, (u32*)bh->b_data + *++offsets);
256 /* Reader: end */
257 if (!p->key)
258 goto no_block;
260 return NULL;
262 changed:
263 *err = -EAGAIN;
264 goto no_block;
265 failure:
266 *err = -EIO;
267 no_block:
268 return p;
272 * ext2_find_near - find a place for allocation with sufficient locality
273 * @inode: owner
274 * @ind: descriptor of indirect block.
276 * This function returns the prefered place for block allocation.
277 * It is used when heuristic for sequential allocation fails.
278 * Rules are:
279 * + if there is a block to the left of our position - allocate near it.
280 * + if pointer will live in indirect block - allocate near that block.
281 * + if pointer will live in inode - allocate in the same cylinder group.
282 * Caller must make sure that @ind is valid and will stay that way.
285 static inline unsigned long ext2_find_near(struct inode *inode, Indirect *ind)
287 u32 *start = ind->bh ? (u32*) ind->bh->b_data : inode->u.ext2_i.i_data;
288 u32 *p;
290 /* Try to find previous block */
291 for (p = ind->p - 1; p >= start; p--)
292 if (*p)
293 return le32_to_cpu(*p);
295 /* No such thing, so let's try location of indirect block */
296 if (ind->bh)
297 return ind->bh->b_blocknr;
300 * It is going to be refered from inode itself? OK, just put it into
301 * the same cylinder group then.
303 return (inode->u.ext2_i.i_block_group *
304 EXT2_BLOCKS_PER_GROUP(inode->i_sb)) +
305 le32_to_cpu(inode->i_sb->u.ext2_sb.s_es->s_first_data_block);
309 * ext2_find_goal - find a prefered place for allocation.
310 * @inode: owner
311 * @block: block we want
312 * @chain: chain of indirect blocks
313 * @partial: pointer to the last triple within a chain
314 * @goal: place to store the result.
316 * Normally this function find the prefered place for block allocation,
317 * stores it in *@goal and returns zero. If the branch had been changed
318 * under us we return -EAGAIN.
321 static inline int ext2_find_goal(struct inode *inode,
322 long block,
323 Indirect chain[4],
324 Indirect *partial,
325 unsigned long *goal)
327 /* Writer: ->i_next_alloc* */
328 if (block == inode->u.ext2_i.i_next_alloc_block + 1) {
329 inode->u.ext2_i.i_next_alloc_block++;
330 inode->u.ext2_i.i_next_alloc_goal++;
332 /* Writer: end */
333 /* Reader: pointers, ->i_next_alloc* */
334 if (verify_chain(chain, partial)) {
336 * try the heuristic for sequential allocation,
337 * failing that at least try to get decent locality.
339 if (block == inode->u.ext2_i.i_next_alloc_block)
340 *goal = inode->u.ext2_i.i_next_alloc_goal;
341 if (!*goal)
342 *goal = ext2_find_near(inode, partial);
343 return 0;
345 /* Reader: end */
346 return -EAGAIN;
350 * ext2_alloc_branch - allocate and set up a chain of blocks.
351 * @inode: owner
352 * @num: depth of the chain (number of blocks to allocate)
353 * @offsets: offsets (in the blocks) to store the pointers to next.
354 * @branch: place to store the chain in.
356 * This function allocates @num blocks, zeroes out all but the last one,
357 * links them into chain and (if we are synchronous) writes them to disk.
358 * In other words, it prepares a branch that can be spliced onto the
359 * inode. It stores the information about that chain in the branch[], in
360 * the same format as ext2_get_branch() would do. We are calling it after
361 * we had read the existing part of chain and partial points to the last
362 * triple of that (one with zero ->key). Upon the exit we have the same
363 * picture as after the successful ext2_get_block(), excpet that in one
364 * place chain is disconnected - *branch->p is still zero (we did not
365 * set the last link), but branch->key contains the number that should
366 * be placed into *branch->p to fill that gap.
368 * If allocation fails we free all blocks we've allocated (and forget
369 * ther buffer_heads) and return the error value the from failed
370 * ext2_alloc_block() (normally -ENOSPC). Otherwise we set the chain
371 * as described above and return 0.
374 static int ext2_alloc_branch(struct inode *inode,
375 int num,
376 unsigned long goal,
377 int *offsets,
378 Indirect *branch)
380 int blocksize = inode->i_sb->s_blocksize;
381 int n = 0;
382 int err;
383 int i;
384 int parent = ext2_alloc_block(inode, goal, &err);
386 branch[0].key = cpu_to_le32(parent);
387 if (parent) for (n = 1; n < num; n++) {
388 struct buffer_head *bh;
389 /* Allocate the next block */
390 int nr = ext2_alloc_block(inode, parent, &err);
391 if (!nr)
392 break;
393 branch[n].key = cpu_to_le32(nr);
395 * Get buffer_head for parent block, zero it out and set
396 * the pointer to new one, then send parent to disk.
398 bh = getblk(inode->i_dev, parent, blocksize);
399 if (!buffer_uptodate(bh))
400 wait_on_buffer(bh);
401 memset(bh->b_data, 0, blocksize);
402 branch[n].bh = bh;
403 branch[n].p = (u32*) bh->b_data + offsets[n];
404 *branch[n].p = branch[n].key;
405 mark_buffer_uptodate(bh, 1);
406 mark_buffer_dirty(bh);
407 if (IS_SYNC(inode) || inode->u.ext2_i.i_osync) {
408 ll_rw_block (WRITE, 1, &bh);
409 wait_on_buffer (bh);
411 parent = nr;
413 if (n == num)
414 return 0;
416 /* Allocation failed, free what we already allocated */
417 for (i = 1; i < n; i++)
418 bforget(branch[i].bh);
419 for (i = 0; i < n; i++)
420 ext2_free_blocks(inode, le32_to_cpu(branch[i].key), 1);
421 return err;
425 * ext2_splice_branch - splice the allocated branch onto inode.
426 * @inode: owner
427 * @block: (logical) number of block we are adding
428 * @chain: chain of indirect blocks (with a missing link - see
429 * ext2_alloc_branch)
430 * @where: location of missing link
431 * @num: number of blocks we are adding
433 * This function verifies that chain (up to the missing link) had not
434 * changed, fills the missing link and does all housekeeping needed in
435 * inode (->i_blocks, etc.). In case of success we end up with the full
436 * chain to new block and return 0. Otherwise (== chain had been changed)
437 * we free the new blocks (forgetting their buffer_heads, indeed) and
438 * return -EAGAIN.
441 static inline int ext2_splice_branch(struct inode *inode,
442 long block,
443 Indirect chain[4],
444 Indirect *where,
445 int num)
447 int i;
449 /* Verify that place we are splicing to is still there and vacant */
451 /* Writer: pointers, ->i_next_alloc*, ->i_blocks */
452 if (!verify_chain(chain, where-1) || *where->p)
453 /* Writer: end */
454 goto changed;
456 /* That's it */
458 *where->p = where->key;
459 inode->u.ext2_i.i_next_alloc_block = block;
460 inode->u.ext2_i.i_next_alloc_goal = le32_to_cpu(where[num-1].key);
461 inode->i_blocks += num * inode->i_sb->s_blocksize/512;
463 /* Writer: end */
465 /* We are done with atomic stuff, now do the rest of housekeeping */
467 inode->i_ctime = CURRENT_TIME;
469 /* had we spliced it onto indirect block? */
470 if (where->bh) {
471 mark_buffer_dirty(where->bh);
472 if (IS_SYNC(inode) || inode->u.ext2_i.i_osync) {
473 ll_rw_block (WRITE, 1, &where->bh);
474 wait_on_buffer(where->bh);
478 if (IS_SYNC(inode) || inode->u.ext2_i.i_osync)
479 ext2_sync_inode (inode);
480 else
481 mark_inode_dirty(inode);
482 return 0;
484 changed:
485 for (i = 1; i < num; i++)
486 bforget(where[i].bh);
487 for (i = 0; i < num; i++)
488 ext2_free_blocks(inode, le32_to_cpu(where[i].key), 1);
489 return -EAGAIN;
493 * Allocation strategy is simple: if we have to allocate something, we will
494 * have to go the whole way to leaf. So let's do it before attaching anything
495 * to tree, set linkage between the newborn blocks, write them if sync is
496 * required, recheck the path, free and repeat if check fails, otherwise
497 * set the last missing link (that will protect us from any truncate-generated
498 * removals - all blocks on the path are immune now) and possibly force the
499 * write on the parent block.
500 * That has a nice additional property: no special recovery from the failed
501 * allocations is needed - we simply release blocks and do not touch anything
502 * reachable from inode.
505 static int ext2_get_block(struct inode *inode, long iblock, struct buffer_head *bh_result, int create)
507 int err = -EIO;
508 int offsets[4];
509 Indirect chain[4];
510 Indirect *partial;
511 unsigned long goal;
512 int left;
513 int depth = ext2_block_to_path(inode, iblock, offsets);
515 if (depth == 0)
516 goto out;
518 lock_kernel();
519 reread:
520 partial = ext2_get_branch(inode, depth, offsets, chain, &err);
522 /* Simplest case - block found, no allocation needed */
523 if (!partial) {
524 got_it:
525 bh_result->b_dev = inode->i_dev;
526 bh_result->b_blocknr = le32_to_cpu(chain[depth-1].key);
527 bh_result->b_state |= (1UL << BH_Mapped);
528 /* Clean up and exit */
529 partial = chain+depth-1; /* the whole chain */
530 goto cleanup;
533 /* Next simple case - plain lookup or failed read of indirect block */
534 if (!create || err == -EIO) {
535 cleanup:
536 while (partial > chain) {
537 brelse(partial->bh);
538 partial--;
540 unlock_kernel();
541 out:
542 return err;
546 * Indirect block might be removed by truncate while we were
547 * reading it. Handling of that case (forget what we've got and
548 * reread) is taken out of the main path.
550 if (err == -EAGAIN)
551 goto changed;
553 if (ext2_find_goal(inode, iblock, chain, partial, &goal) < 0)
554 goto changed;
556 left = (chain + depth) - partial;
557 err = ext2_alloc_branch(inode, left, goal,
558 offsets+(partial-chain), partial);
559 if (err)
560 goto cleanup;
562 if (ext2_splice_branch(inode, iblock, chain, partial, left) < 0)
563 goto changed;
565 bh_result->b_state |= (1UL << BH_New);
566 goto got_it;
568 changed:
569 while (partial > chain) {
570 bforget(partial->bh);
571 partial--;
573 goto reread;
576 struct buffer_head * ext2_getblk(struct inode * inode, long block, int create, int * err)
578 struct buffer_head dummy;
579 int error;
581 dummy.b_state = 0;
582 dummy.b_blocknr = -1000;
583 error = ext2_get_block(inode, block, &dummy, create);
584 *err = error;
585 if (!error && buffer_mapped(&dummy)) {
586 struct buffer_head *bh;
587 bh = getblk(dummy.b_dev, dummy.b_blocknr, inode->i_sb->s_blocksize);
588 if (buffer_new(&dummy)) {
589 if (!buffer_uptodate(bh))
590 wait_on_buffer(bh);
591 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
592 mark_buffer_uptodate(bh, 1);
593 mark_buffer_dirty(bh);
595 return bh;
597 return NULL;
600 struct buffer_head * ext2_bread (struct inode * inode, int block,
601 int create, int *err)
603 struct buffer_head * bh;
604 int prev_blocks;
606 prev_blocks = inode->i_blocks;
608 bh = ext2_getblk (inode, block, create, err);
609 if (!bh)
610 return bh;
613 * If the inode has grown, and this is a directory, then perform
614 * preallocation of a few more blocks to try to keep directory
615 * fragmentation down.
617 if (create &&
618 S_ISDIR(inode->i_mode) &&
619 inode->i_blocks > prev_blocks &&
620 EXT2_HAS_COMPAT_FEATURE(inode->i_sb,
621 EXT2_FEATURE_COMPAT_DIR_PREALLOC)) {
622 int i;
623 struct buffer_head *tmp_bh;
625 for (i = 1;
626 i < EXT2_SB(inode->i_sb)->s_es->s_prealloc_dir_blocks;
627 i++) {
629 * ext2_getblk will zero out the contents of the
630 * directory for us
632 tmp_bh = ext2_getblk(inode, block+i, create, err);
633 if (!tmp_bh) {
634 brelse (bh);
635 return 0;
637 brelse (tmp_bh);
641 if (buffer_uptodate(bh))
642 return bh;
643 ll_rw_block (READ, 1, &bh);
644 wait_on_buffer (bh);
645 if (buffer_uptodate(bh))
646 return bh;
647 brelse (bh);
648 *err = -EIO;
649 return NULL;
652 static int ext2_writepage(struct file *file, struct page *page)
654 return block_write_full_page(page,ext2_get_block);
656 static int ext2_readpage(struct file *file, struct page *page)
658 return block_read_full_page(page,ext2_get_block);
660 static int ext2_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
662 return block_prepare_write(page,from,to,ext2_get_block);
664 static int ext2_bmap(struct address_space *mapping, long block)
666 return generic_block_bmap(mapping,block,ext2_get_block);
668 struct address_space_operations ext2_aops = {
669 readpage: ext2_readpage,
670 writepage: ext2_writepage,
671 sync_page: block_sync_page,
672 prepare_write: ext2_prepare_write,
673 commit_write: generic_commit_write,
674 bmap: ext2_bmap
678 * Probably it should be a library function... search for first non-zero word
679 * or memcmp with zero_page, whatever is better for particular architecture.
680 * Linus?
682 static inline int all_zeroes(u32 *p, u32 *q)
684 while (p < q)
685 if (*p++)
686 return 0;
687 return 1;
691 * ext2_find_shared - find the indirect blocks for partial truncation.
692 * @inode: inode in question
693 * @depth: depth of the affected branch
694 * @offsets: offsets of pointers in that branch (see ext2_block_to_path)
695 * @chain: place to store the pointers to partial indirect blocks
696 * @top: place to the (detached) top of branch
698 * This is a helper function used by ext2_truncate().
700 * When we do truncate() we may have to clean the ends of several indirect
701 * blocks but leave the blocks themselves alive. Block is partially
702 * truncated if some data below the new i_size is refered from it (and
703 * it is on the path to the first completely truncated data block, indeed).
704 * We have to free the top of that path along with everything to the right
705 * of the path. Since no allocation past the truncation point is possible
706 * until ext2_truncate() finishes, we may safely do the latter, but top
707 * of branch may require special attention - pageout below the truncation
708 * point might try to populate it.
710 * We atomically detach the top of branch from the tree, store the block
711 * number of its root in *@top, pointers to buffer_heads of partially
712 * truncated blocks - in @chain[].bh and pointers to their last elements
713 * that should not be removed - in @chain[].p. Return value is the pointer
714 * to last filled element of @chain.
716 * The work left to caller to do the actual freeing of subtrees:
717 * a) free the subtree starting from *@top
718 * b) free the subtrees whose roots are stored in
719 * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
720 * c) free the subtrees growing from the inode past the @chain[0].p
721 * (no partially truncated stuff there).
724 static Indirect *ext2_find_shared(struct inode *inode,
725 int depth,
726 int offsets[4],
727 Indirect chain[4],
728 u32 *top)
730 Indirect *partial, *p;
731 int k, err;
733 *top = 0;
734 for (k = depth; k > 1 && !offsets[k-1]; k--)
736 partial = ext2_get_branch(inode, k, offsets, chain, &err);
737 /* Writer: pointers */
738 if (!partial)
739 partial = chain + k-1;
741 * If the branch acquired continuation since we've looked at it -
742 * fine, it should all survive and (new) top doesn't belong to us.
744 if (!partial->key && *partial->p)
745 /* Writer: end */
746 goto no_top;
747 for (p=partial; p>chain && all_zeroes((u32*)p->bh->b_data,p->p); p--)
750 * OK, we've found the last block that must survive. The rest of our
751 * branch should be detached before unlocking. However, if that rest
752 * of branch is all ours and does not grow immediately from the inode
753 * it's easier to cheat and just decrement partial->p.
755 if (p == chain + k - 1 && p > chain) {
756 p->p--;
757 } else {
758 *top = *p->p;
759 *p->p = 0;
761 /* Writer: end */
763 while(partial > p)
765 brelse(partial->bh);
766 partial--;
768 no_top:
769 return partial;
773 * ext2_free_data - free a list of data blocks
774 * @inode: inode we are dealing with
775 * @p: array of block numbers
776 * @q: points immediately past the end of array
778 * We are freeing all blocks refered from that array (numbers are
779 * stored as little-endian 32-bit) and updating @inode->i_blocks
780 * appropriately.
782 static inline void ext2_free_data(struct inode *inode, u32 *p, u32 *q)
784 int blocks = inode->i_sb->s_blocksize / 512;
785 unsigned long block_to_free = 0, count = 0;
786 unsigned long nr;
788 for ( ; p < q ; p++) {
789 nr = le32_to_cpu(*p);
790 if (nr) {
791 *p = 0;
792 /* accumulate blocks to free if they're contiguous */
793 if (count == 0)
794 goto free_this;
795 else if (block_to_free == nr - count)
796 count++;
797 else {
798 /* Writer: ->i_blocks */
799 inode->i_blocks -= blocks * count;
800 /* Writer: end */
801 ext2_free_blocks (inode, block_to_free, count);
802 mark_inode_dirty(inode);
803 free_this:
804 block_to_free = nr;
805 count = 1;
809 if (count > 0) {
810 /* Writer: ->i_blocks */
811 inode->i_blocks -= blocks * count;
812 /* Writer: end */
813 ext2_free_blocks (inode, block_to_free, count);
814 mark_inode_dirty(inode);
819 * ext2_free_branches - free an array of branches
820 * @inode: inode we are dealing with
821 * @p: array of block numbers
822 * @q: pointer immediately past the end of array
823 * @depth: depth of the branches to free
825 * We are freeing all blocks refered from these branches (numbers are
826 * stored as little-endian 32-bit) and updating @inode->i_blocks
827 * appropriately.
829 static void ext2_free_branches(struct inode *inode, u32 *p, u32 *q, int depth)
831 struct buffer_head * bh;
832 unsigned long nr;
834 if (depth--) {
835 int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
836 for ( ; p < q ; p++) {
837 nr = le32_to_cpu(*p);
838 if (!nr)
839 continue;
840 *p = 0;
841 bh = bread (inode->i_dev, nr, inode->i_sb->s_blocksize);
843 * A read failure? Report error and clear slot
844 * (should be rare).
846 if (!bh) {
847 ext2_error(inode->i_sb, "ext2_free_branches",
848 "Read failure, inode=%ld, block=%ld",
849 inode->i_ino, nr);
850 continue;
852 ext2_free_branches(inode,
853 (u32*)bh->b_data,
854 (u32*)bh->b_data + addr_per_block,
855 depth);
856 bforget(bh);
857 /* Writer: ->i_blocks */
858 inode->i_blocks -= inode->i_sb->s_blocksize / 512;
859 /* Writer: end */
860 ext2_free_blocks(inode, nr, 1);
861 mark_inode_dirty(inode);
863 } else
864 ext2_free_data(inode, p, q);
867 void ext2_truncate (struct inode * inode)
869 u32 *i_data = inode->u.ext2_i.i_data;
870 int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
871 int offsets[4];
872 Indirect chain[4];
873 Indirect *partial;
874 int nr = 0;
875 int n;
876 long iblock;
877 unsigned blocksize, tail;
879 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
880 S_ISLNK(inode->i_mode)))
881 return;
882 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
883 return;
885 ext2_discard_prealloc(inode);
887 blocksize = inode->i_sb->s_blocksize;
888 iblock = (inode->i_size + blocksize-1)
889 >> EXT2_BLOCK_SIZE_BITS(inode->i_sb);
890 tail = (iblock << EXT2_BLOCK_SIZE_BITS(inode->i_sb)) - inode->i_size;
892 block_zero_page(inode->i_mapping, inode->i_size, tail);
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(partial->bh);
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(partial->bh);
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(bh);
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;