File: /usr/src/linux/fs/ext2/inode.c

1     /*
2      *  linux/fs/ext2/inode.c
3      *
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)
8      *
9      *  from
10      *
11      *  linux/fs/minix/inode.c
12      *
13      *  Copyright (C) 1991, 1992  Linus Torvalds
14      *
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)
21      *
22      *  Assorted race fixes, rewrite of ext2_get_block() by Al Viro, 2000
23      */
24     
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>
31     
32     static int ext2_update_inode(struct inode * inode, int do_sync);
33     
34     /*
35      * Called at each iput()
36      */
37     void ext2_put_inode (struct inode * inode)
38     {
39     	ext2_discard_prealloc (inode);
40     }
41     
42     /*
43      * Called at the last iput() if i_nlink is zero.
44      */
45     void ext2_delete_inode (struct inode * inode)
46     {
47     	lock_kernel();
48     
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);
60     
61     	unlock_kernel();
62     	return;
63     no_delete:
64     	unlock_kernel();
65     	clear_inode(inode);	/* We must guarantee clearing of inode... */
66     }
67     
68     void ext2_discard_prealloc (struct inode * inode)
69     {
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);
80     	}
81     	unlock_kernel();
82     #endif
83     }
84     
85     static int ext2_alloc_block (struct inode * inode, unsigned long goal, int *err)
86     {
87     #ifdef EXT2FS_DEBUG
88     	static unsigned long alloc_hits = 0, alloc_attempts = 0;
89     #endif
90     	unsigned long result;
91     
92     
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))
98     	{		
99     		result = inode->u.ext2_i.i_prealloc_block++;
100     		inode->u.ext2_i.i_prealloc_count--;
101     		/* Writer: end */
102     		ext2_debug ("preallocation hit (%lu/%lu).\n",
103     			    ++alloc_hits, ++alloc_attempts);
104     	} else {
105     		ext2_discard_prealloc (inode);
106     		ext2_debug ("preallocation miss (%lu/%lu).\n",
107     			    alloc_hits, ++alloc_attempts);
108     		if (S_ISREG(inode->i_mode))
109     			result = ext2_new_block (inode, goal, 
110     				 &inode->u.ext2_i.i_prealloc_count,
111     				 &inode->u.ext2_i.i_prealloc_block, err);
112     		else
113     			result = ext2_new_block (inode, goal, 0, 0, err);
114     	}
115     #else
116     	result = ext2_new_block (inode, goal, 0, 0, err);
117     #endif
118     	return result;
119     }
120     
121     typedef struct {
122     	u32	*p;
123     	u32	key;
124     	struct buffer_head *bh;
125     } Indirect;
126     
127     static inline void add_chain(Indirect *p, struct buffer_head *bh, u32 *v)
128     {
129     	p->key = *(p->p = v);
130     	p->bh = bh;
131     }
132     
133     static inline int verify_chain(Indirect *from, Indirect *to)
134     {
135     	while (from <= to && from->key == *from->p)
136     		from++;
137     	return (from > to);
138     }
139     
140     /**
141      *	ext2_block_to_path - parse the block number into array of offsets
142      *	@inode: inode in question (we are only interested in its superblock)
143      *	@i_block: block number to be parsed
144      *	@offsets: array to store the offsets in
145      *
146      *	To store the locations of file's data ext2 uses a data structure common
147      *	for UNIX filesystems - tree of pointers anchored in the inode, with
148      *	data blocks at leaves and indirect blocks in intermediate nodes.
149      *	This function translates the block number into path in that tree -
150      *	return value is the path length and @offsets[n] is the offset of
151      *	pointer to (n+1)th node in the nth one. If @block is out of range
152      *	(negative or too large) warning is printed and zero returned.
153      *
154      *	Note: function doesn't find node addresses, so no IO is needed. All
155      *	we need to know is the capacity of indirect blocks (taken from the
156      *	inode->i_sb).
157      */
158     
159     /*
160      * Portability note: the last comparison (check that we fit into triple
161      * indirect block) is spelled differently, because otherwise on an
162      * architecture with 32-bit longs and 8Kb pages we might get into trouble
163      * if our filesystem had 8Kb blocks. We might use long long, but that would
164      * kill us on x86. Oh, well, at least the sign propagation does not matter -
165      * i_block would have to be negative in the very beginning, so we would not
166      * get there at all.
167      */
168     
169     static int ext2_block_to_path(struct inode *inode, long i_block, int offsets[4])
170     {
171     	int ptrs = EXT2_ADDR_PER_BLOCK(inode->i_sb);
172     	int ptrs_bits = EXT2_ADDR_PER_BLOCK_BITS(inode->i_sb);
173     	const long direct_blocks = EXT2_NDIR_BLOCKS,
174     		indirect_blocks = ptrs,
175     		double_blocks = (1 << (ptrs_bits * 2));
176     	int n = 0;
177     
178     	if (i_block < 0) {
179     		ext2_warning (inode->i_sb, "ext2_block_to_path", "block < 0");
180     	} else if (i_block < direct_blocks) {
181     		offsets[n++] = i_block;
182     	} else if ( (i_block -= direct_blocks) < indirect_blocks) {
183     		offsets[n++] = EXT2_IND_BLOCK;
184     		offsets[n++] = i_block;
185     	} else if ((i_block -= indirect_blocks) < double_blocks) {
186     		offsets[n++] = EXT2_DIND_BLOCK;
187     		offsets[n++] = i_block >> ptrs_bits;
188     		offsets[n++] = i_block & (ptrs - 1);
189     	} else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
190     		offsets[n++] = EXT2_TIND_BLOCK;
191     		offsets[n++] = i_block >> (ptrs_bits * 2);
192     		offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
193     		offsets[n++] = i_block & (ptrs - 1);
194     	} else {
195     		ext2_warning (inode->i_sb, "ext2_block_to_path", "block > big");
196     	}
197     	return n;
198     }
199     
200     /**
201      *	ext2_get_branch - read the chain of indirect blocks leading to data
202      *	@inode: inode in question
203      *	@depth: depth of the chain (1 - direct pointer, etc.)
204      *	@offsets: offsets of pointers in inode/indirect blocks
205      *	@chain: place to store the result
206      *	@err: here we store the error value
207      *
208      *	Function fills the array of triples <key, p, bh> and returns %NULL
209      *	if everything went OK or the pointer to the last filled triple
210      *	(incomplete one) otherwise. Upon the return chain[i].key contains
211      *	the number of (i+1)-th block in the chain (as it is stored in memory,
212      *	i.e. little-endian 32-bit), chain[i].p contains the address of that
213      *	number (it points into struct inode for i==0 and into the bh->b_data
214      *	for i>0) and chain[i].bh points to the buffer_head of i-th indirect
215      *	block for i>0 and NULL for i==0. In other words, it holds the block
216      *	numbers of the chain, addresses they were taken from (and where we can
217      *	verify that chain did not change) and buffer_heads hosting these
218      *	numbers.
219      *
220      *	Function stops when it stumbles upon zero pointer (absent block)
221      *		(pointer to last triple returned, *@err == 0)
222      *	or when it gets an IO error reading an indirect block
223      *		(ditto, *@err == -EIO)
224      *	or when it notices that chain had been changed while it was reading
225      *		(ditto, *@err == -EAGAIN)
226      *	or when it reads all @depth-1 indirect blocks successfully and finds
227      *	the whole chain, all way to the data (returns %NULL, *err == 0).
228      */
229     static Indirect *ext2_get_branch(struct inode *inode,
230     				 int depth,
231     				 int *offsets,
232     				 Indirect chain[4],
233     				 int *err)
234     {
235     	kdev_t dev = inode->i_dev;
236     	int size = inode->i_sb->s_blocksize;
237     	Indirect *p = chain;
238     	struct buffer_head *bh;
239     
240     	*err = 0;
241     	/* i_data is not going away, no lock needed */
242     	add_chain (chain, NULL, inode->u.ext2_i.i_data + *offsets);
243     	if (!p->key)
244     		goto no_block;
245     	while (--depth) {
246     		bh = bread(dev, le32_to_cpu(p->key), size);
247     		if (!bh)
248     			goto failure;
249     		/* Reader: pointers */
250     		if (!verify_chain(chain, p))
251     			goto changed;
252     		add_chain(++p, bh, (u32*)bh->b_data + *++offsets);
253     		/* Reader: end */
254     		if (!p->key)
255     			goto no_block;
256     	}
257     	return NULL;
258     
259     changed:
260     	*err = -EAGAIN;
261     	goto no_block;
262     failure:
263     	*err = -EIO;
264     no_block:
265     	return p;
266     }
267     
268     /**
269      *	ext2_find_near - find a place for allocation with sufficient locality
270      *	@inode: owner
271      *	@ind: descriptor of indirect block.
272      *
273      *	This function returns the prefered place for block allocation.
274      *	It is used when heuristic for sequential allocation fails.
275      *	Rules are:
276      *	  + if there is a block to the left of our position - allocate near it.
277      *	  + if pointer will live in indirect block - allocate near that block.
278      *	  + if pointer will live in inode - allocate in the same cylinder group.
279      *	Caller must make sure that @ind is valid and will stay that way.
280      */
281     
282     static inline unsigned long ext2_find_near(struct inode *inode, Indirect *ind)
283     {
284     	u32 *start = ind->bh ? (u32*) ind->bh->b_data : inode->u.ext2_i.i_data;
285     	u32 *p;
286     
287     	/* Try to find previous block */
288     	for (p = ind->p - 1; p >= start; p--)
289     		if (*p)
290     			return le32_to_cpu(*p);
291     
292     	/* No such thing, so let's try location of indirect block */
293     	if (ind->bh)
294     		return ind->bh->b_blocknr;
295     
296     	/*
297     	 * It is going to be refered from inode itself? OK, just put it into
298     	 * the same cylinder group then.
299     	 */
300     	return (inode->u.ext2_i.i_block_group * 
301     		EXT2_BLOCKS_PER_GROUP(inode->i_sb)) +
302     	       le32_to_cpu(inode->i_sb->u.ext2_sb.s_es->s_first_data_block);
303     }
304     
305     /**
306      *	ext2_find_goal - find a prefered place for allocation.
307      *	@inode: owner
308      *	@block:  block we want
309      *	@chain:  chain of indirect blocks
310      *	@partial: pointer to the last triple within a chain
311      *	@goal:	place to store the result.
312      *
313      *	Normally this function find the prefered place for block allocation,
314      *	stores it in *@goal and returns zero. If the branch had been changed
315      *	under us we return -EAGAIN.
316      */
317     
318     static inline int ext2_find_goal(struct inode *inode,
319     				 long block,
320     				 Indirect chain[4],
321     				 Indirect *partial,
322     				 unsigned long *goal)
323     {
324     	/* Writer: ->i_next_alloc* */
325     	if (block == inode->u.ext2_i.i_next_alloc_block + 1) {
326     		inode->u.ext2_i.i_next_alloc_block++;
327     		inode->u.ext2_i.i_next_alloc_goal++;
328     	} 
329     	/* Writer: end */
330     	/* Reader: pointers, ->i_next_alloc* */
331     	if (verify_chain(chain, partial)) {
332     		/*
333     		 * try the heuristic for sequential allocation,
334     		 * failing that at least try to get decent locality.
335     		 */
336     		if (block == inode->u.ext2_i.i_next_alloc_block)
337     			*goal = inode->u.ext2_i.i_next_alloc_goal;
338     		if (!*goal)
339     			*goal = ext2_find_near(inode, partial);
340     		return 0;
341     	}
342     	/* Reader: end */
343     	return -EAGAIN;
344     }
345     
346     /**
347      *	ext2_alloc_branch - allocate and set up a chain of blocks.
348      *	@inode: owner
349      *	@num: depth of the chain (number of blocks to allocate)
350      *	@offsets: offsets (in the blocks) to store the pointers to next.
351      *	@branch: place to store the chain in.
352      *
353      *	This function allocates @num blocks, zeroes out all but the last one,
354      *	links them into chain and (if we are synchronous) writes them to disk.
355      *	In other words, it prepares a branch that can be spliced onto the
356      *	inode. It stores the information about that chain in the branch[], in
357      *	the same format as ext2_get_branch() would do. We are calling it after
358      *	we had read the existing part of chain and partial points to the last
359      *	triple of that (one with zero ->key). Upon the exit we have the same
360      *	picture as after the successful ext2_get_block(), excpet that in one
361      *	place chain is disconnected - *branch->p is still zero (we did not
362      *	set the last link), but branch->key contains the number that should
363      *	be placed into *branch->p to fill that gap.
364      *
365      *	If allocation fails we free all blocks we've allocated (and forget
366      *	their buffer_heads) and return the error value the from failed
367      *	ext2_alloc_block() (normally -ENOSPC). Otherwise we set the chain
368      *	as described above and return 0.
369      */
370     
371     static int ext2_alloc_branch(struct inode *inode,
372     			     int num,
373     			     unsigned long goal,
374     			     int *offsets,
375     			     Indirect *branch)
376     {
377     	int blocksize = inode->i_sb->s_blocksize;
378     	int n = 0;
379     	int err;
380     	int i;
381     	int parent = ext2_alloc_block(inode, goal, &err);
382     
383     	branch[0].key = cpu_to_le32(parent);
384     	if (parent) for (n = 1; n < num; n++) {
385     		struct buffer_head *bh;
386     		/* Allocate the next block */
387     		int nr = ext2_alloc_block(inode, parent, &err);
388     		if (!nr)
389     			break;
390     		branch[n].key = cpu_to_le32(nr);
391     		/*
392     		 * Get buffer_head for parent block, zero it out and set 
393     		 * the pointer to new one, then send parent to disk.
394     		 */
395     		bh = getblk(inode->i_dev, parent, blocksize);
396     		lock_buffer(bh);
397     		memset(bh->b_data, 0, blocksize);
398     		branch[n].bh = bh;
399     		branch[n].p = (u32*) bh->b_data + offsets[n];
400     		*branch[n].p = branch[n].key;
401     		mark_buffer_uptodate(bh, 1);
402     		unlock_buffer(bh);
403     		mark_buffer_dirty_inode(bh, inode);
404     		if (IS_SYNC(inode) || inode->u.ext2_i.i_osync) {
405     			ll_rw_block (WRITE, 1, &bh);
406     			wait_on_buffer (bh);
407     		}
408     		parent = nr;
409     	}
410     	if (n == num)
411     		return 0;
412     
413     	/* Allocation failed, free what we already allocated */
414     	for (i = 1; i < n; i++)
415     		bforget(branch[i].bh);
416     	for (i = 0; i < n; i++)
417     		ext2_free_blocks(inode, le32_to_cpu(branch[i].key), 1);
418     	return err;
419     }
420     
421     /**
422      *	ext2_splice_branch - splice the allocated branch onto inode.
423      *	@inode: owner
424      *	@block: (logical) number of block we are adding
425      *	@chain: chain of indirect blocks (with a missing link - see
426      *		ext2_alloc_branch)
427      *	@where: location of missing link
428      *	@num:   number of blocks we are adding
429      *
430      *	This function verifies that chain (up to the missing link) had not
431      *	changed, fills the missing link and does all housekeeping needed in
432      *	inode (->i_blocks, etc.). In case of success we end up with the full
433      *	chain to new block and return 0. Otherwise (== chain had been changed)
434      *	we free the new blocks (forgetting their buffer_heads, indeed) and
435      *	return -EAGAIN.
436      */
437     
438     static inline int ext2_splice_branch(struct inode *inode,
439     				     long block,
440     				     Indirect chain[4],
441     				     Indirect *where,
442     				     int num)
443     {
444     	int i;
445     
446     	/* Verify that place we are splicing to is still there and vacant */
447     
448     	/* Writer: pointers, ->i_next_alloc*, ->i_blocks */
449     	if (!verify_chain(chain, where-1) || *where->p)
450     		/* Writer: end */
451     		goto changed;
452     
453     	/* That's it */
454     
455     	*where->p = where->key;
456     	inode->u.ext2_i.i_next_alloc_block = block;
457     	inode->u.ext2_i.i_next_alloc_goal = le32_to_cpu(where[num-1].key);
458     	inode->i_blocks += num * inode->i_sb->s_blocksize/512;
459     
460     	/* Writer: end */
461     
462     	/* We are done with atomic stuff, now do the rest of housekeeping */
463     
464     	inode->i_ctime = CURRENT_TIME;
465     
466     	/* had we spliced it onto indirect block? */
467     	if (where->bh) {
468     		mark_buffer_dirty_inode(where->bh, inode);
469     		if (IS_SYNC(inode) || inode->u.ext2_i.i_osync) {
470     			ll_rw_block (WRITE, 1, &where->bh);
471     			wait_on_buffer(where->bh);
472     		}
473     	}
474     
475     	if (IS_SYNC(inode) || inode->u.ext2_i.i_osync)
476     		ext2_sync_inode (inode);
477     	else
478     		mark_inode_dirty(inode);
479     	return 0;
480     
481     changed:
482     	for (i = 1; i < num; i++)
483     		bforget(where[i].bh);
484     	for (i = 0; i < num; i++)
485     		ext2_free_blocks(inode, le32_to_cpu(where[i].key), 1);
486     	return -EAGAIN;
487     }
488     
489     /*
490      * Allocation strategy is simple: if we have to allocate something, we will
491      * have to go the whole way to leaf. So let's do it before attaching anything
492      * to tree, set linkage between the newborn blocks, write them if sync is
493      * required, recheck the path, free and repeat if check fails, otherwise
494      * set the last missing link (that will protect us from any truncate-generated
495      * removals - all blocks on the path are immune now) and possibly force the
496      * write on the parent block.
497      * That has a nice additional property: no special recovery from the failed
498      * allocations is needed - we simply release blocks and do not touch anything
499      * reachable from inode.
500      */
501     
502     static int ext2_get_block(struct inode *inode, long iblock, struct buffer_head *bh_result, int create)
503     {
504     	int err = -EIO;
505     	int offsets[4];
506     	Indirect chain[4];
507     	Indirect *partial;
508     	unsigned long goal;
509     	int left;
510     	int depth = ext2_block_to_path(inode, iblock, offsets);
511     
512     	if (depth == 0)
513     		goto out;
514     
515     	lock_kernel();
516     reread:
517     	partial = ext2_get_branch(inode, depth, offsets, chain, &err);
518     
519     	/* Simplest case - block found, no allocation needed */
520     	if (!partial) {
521     got_it:
522     		bh_result->b_dev = inode->i_dev;
523     		bh_result->b_blocknr = le32_to_cpu(chain[depth-1].key);
524     		bh_result->b_state |= (1UL << BH_Mapped);
525     		/* Clean up and exit */
526     		partial = chain+depth-1; /* the whole chain */
527     		goto cleanup;
528     	}
529     
530     	/* Next simple case - plain lookup or failed read of indirect block */
531     	if (!create || err == -EIO) {
532     cleanup:
533     		while (partial > chain) {
534     			brelse(partial->bh);
535     			partial--;
536     		}
537     		unlock_kernel();
538     out:
539     		return err;
540     	}
541     
542     	/*
543     	 * Indirect block might be removed by truncate while we were
544     	 * reading it. Handling of that case (forget what we've got and
545     	 * reread) is taken out of the main path.
546     	 */
547     	if (err == -EAGAIN)
548     		goto changed;
549     
550     	if (ext2_find_goal(inode, iblock, chain, partial, &goal) < 0)
551     		goto changed;
552     
553     	left = (chain + depth) - partial;
554     	err = ext2_alloc_branch(inode, left, goal,
555     					offsets+(partial-chain), partial);
556     	if (err)
557     		goto cleanup;
558     
559     	if (ext2_splice_branch(inode, iblock, chain, partial, left) < 0)
560     		goto changed;
561     
562     	bh_result->b_state |= (1UL << BH_New);
563     	goto got_it;
564     
565     changed:
566     	while (partial > chain) {
567     		brelse(partial->bh);
568     		partial--;
569     	}
570     	goto reread;
571     }
572     
573     static int ext2_writepage(struct page *page)
574     {
575     	return block_write_full_page(page,ext2_get_block);
576     }
577     static int ext2_readpage(struct file *file, struct page *page)
578     {
579     	return block_read_full_page(page,ext2_get_block);
580     }
581     static int ext2_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
582     {
583     	return block_prepare_write(page,from,to,ext2_get_block);
584     }
585     static int ext2_bmap(struct address_space *mapping, long block)
586     {
587     	return generic_block_bmap(mapping,block,ext2_get_block);
588     }
589     static int ext2_direct_IO(int rw, struct inode * inode, struct kiobuf * iobuf, unsigned long blocknr, int blocksize)
590     {
591     	return generic_direct_IO(rw, inode, iobuf, blocknr, blocksize, ext2_get_block);
592     }
593     struct address_space_operations ext2_aops = {
594     	readpage: ext2_readpage,
595     	writepage: ext2_writepage,
596     	sync_page: block_sync_page,
597     	prepare_write: ext2_prepare_write,
598     	commit_write: generic_commit_write,
599     	bmap: ext2_bmap,
600     	direct_IO: ext2_direct_IO,
601     };
602     
603     /*
604      * Probably it should be a library function... search for first non-zero word
605      * or memcmp with zero_page, whatever is better for particular architecture.
606      * Linus?
607      */
608     static inline int all_zeroes(u32 *p, u32 *q)
609     {
610     	while (p < q)
611     		if (*p++)
612     			return 0;
613     	return 1;
614     }
615     
616     /**
617      *	ext2_find_shared - find the indirect blocks for partial truncation.
618      *	@inode:	  inode in question
619      *	@depth:	  depth of the affected branch
620      *	@offsets: offsets of pointers in that branch (see ext2_block_to_path)
621      *	@chain:	  place to store the pointers to partial indirect blocks
622      *	@top:	  place to the (detached) top of branch
623      *
624      *	This is a helper function used by ext2_truncate().
625      *
626      *	When we do truncate() we may have to clean the ends of several indirect
627      *	blocks but leave the blocks themselves alive. Block is partially
628      *	truncated if some data below the new i_size is refered from it (and
629      *	it is on the path to the first completely truncated data block, indeed).
630      *	We have to free the top of that path along with everything to the right
631      *	of the path. Since no allocation past the truncation point is possible
632      *	until ext2_truncate() finishes, we may safely do the latter, but top
633      *	of branch may require special attention - pageout below the truncation
634      *	point might try to populate it.
635      *
636      *	We atomically detach the top of branch from the tree, store the block
637      *	number of its root in *@top, pointers to buffer_heads of partially
638      *	truncated blocks - in @chain[].bh and pointers to their last elements
639      *	that should not be removed - in @chain[].p. Return value is the pointer
640      *	to last filled element of @chain.
641      *
642      *	The work left to caller to do the actual freeing of subtrees:
643      *		a) free the subtree starting from *@top
644      *		b) free the subtrees whose roots are stored in
645      *			(@chain[i].p+1 .. end of @chain[i].bh->b_data)
646      *		c) free the subtrees growing from the inode past the @chain[0].p
647      *			(no partially truncated stuff there).
648      */
649     
650     static Indirect *ext2_find_shared(struct inode *inode,
651     				int depth,
652     				int offsets[4],
653     				Indirect chain[4],
654     				u32 *top)
655     {
656     	Indirect *partial, *p;
657     	int k, err;
658     
659     	*top = 0;
660     	for (k = depth; k > 1 && !offsets[k-1]; k--)
661     		;
662     	partial = ext2_get_branch(inode, k, offsets, chain, &err);
663     	/* Writer: pointers */
664     	if (!partial)
665     		partial = chain + k-1;
666     	/*
667     	 * If the branch acquired continuation since we've looked at it -
668     	 * fine, it should all survive and (new) top doesn't belong to us.
669     	 */
670     	if (!partial->key && *partial->p)
671     		/* Writer: end */
672     		goto no_top;
673     	for (p=partial; p>chain && all_zeroes((u32*)p->bh->b_data,p->p); p--)
674     		;
675     	/*
676     	 * OK, we've found the last block that must survive. The rest of our
677     	 * branch should be detached before unlocking. However, if that rest
678     	 * of branch is all ours and does not grow immediately from the inode
679     	 * it's easier to cheat and just decrement partial->p.
680     	 */
681     	if (p == chain + k - 1 && p > chain) {
682     		p->p--;
683     	} else {
684     		*top = *p->p;
685     		*p->p = 0;
686     	}
687     	/* Writer: end */
688     
689     	while(partial > p)
690     	{
691     		brelse(partial->bh);
692     		partial--;
693     	}
694     no_top:
695     	return partial;
696     }
697     
698     /**
699      *	ext2_free_data - free a list of data blocks
700      *	@inode:	inode we are dealing with
701      *	@p:	array of block numbers
702      *	@q:	points immediately past the end of array
703      *
704      *	We are freeing all blocks refered from that array (numbers are
705      *	stored as little-endian 32-bit) and updating @inode->i_blocks
706      *	appropriately.
707      */
708     static inline void ext2_free_data(struct inode *inode, u32 *p, u32 *q)
709     {
710     	int blocks = inode->i_sb->s_blocksize / 512;
711     	unsigned long block_to_free = 0, count = 0;
712     	unsigned long nr;
713     
714     	for ( ; p < q ; p++) {
715     		nr = le32_to_cpu(*p);
716     		if (nr) {
717     			*p = 0;
718     			/* accumulate blocks to free if they're contiguous */
719     			if (count == 0)
720     				goto free_this;
721     			else if (block_to_free == nr - count)
722     				count++;
723     			else {
724     				/* Writer: ->i_blocks */
725     				inode->i_blocks -= blocks * count;
726     				/* Writer: end */
727     				mark_inode_dirty(inode);
728     				ext2_free_blocks (inode, block_to_free, count);
729     			free_this:
730     				block_to_free = nr;
731     				count = 1;
732     			}
733     		}
734     	}
735     	if (count > 0) {
736     		/* Writer: ->i_blocks */
737     		inode->i_blocks -= blocks * count;
738     		/* Writer: end */
739     		mark_inode_dirty(inode);
740     		ext2_free_blocks (inode, block_to_free, count);
741     	}
742     }
743     
744     /**
745      *	ext2_free_branches - free an array of branches
746      *	@inode:	inode we are dealing with
747      *	@p:	array of block numbers
748      *	@q:	pointer immediately past the end of array
749      *	@depth:	depth of the branches to free
750      *
751      *	We are freeing all blocks refered from these branches (numbers are
752      *	stored as little-endian 32-bit) and updating @inode->i_blocks
753      *	appropriately.
754      */
755     static void ext2_free_branches(struct inode *inode, u32 *p, u32 *q, int depth)
756     {
757     	struct buffer_head * bh;
758     	unsigned long nr;
759     
760     	if (depth--) {
761     		int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
762     		for ( ; p < q ; p++) {
763     			nr = le32_to_cpu(*p);
764     			if (!nr)
765     				continue;
766     			*p = 0;
767     			bh = bread (inode->i_dev, nr, inode->i_sb->s_blocksize);
768     			/*
769     			 * A read failure? Report error and clear slot
770     			 * (should be rare).
771     			 */ 
772     			if (!bh) {
773     				ext2_error(inode->i_sb, "ext2_free_branches",
774     					"Read failure, inode=%ld, block=%ld",
775     					inode->i_ino, nr);
776     				continue;
777     			}
778     			ext2_free_branches(inode,
779     					   (u32*)bh->b_data,
780     					   (u32*)bh->b_data + addr_per_block,
781     					   depth);
782     			bforget(bh);
783     			/* Writer: ->i_blocks */
784     			inode->i_blocks -= inode->i_sb->s_blocksize / 512;
785     			/* Writer: end */
786     			ext2_free_blocks(inode, nr, 1);
787     			mark_inode_dirty(inode);
788     		}
789     	} else
790     		ext2_free_data(inode, p, q);
791     }
792     
793     void ext2_truncate (struct inode * inode)
794     {
795     	u32 *i_data = inode->u.ext2_i.i_data;
796     	int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
797     	int offsets[4];
798     	Indirect chain[4];
799     	Indirect *partial;
800     	int nr = 0;
801     	int n;
802     	long iblock;
803     	unsigned blocksize;
804     
805     	if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
806     	    S_ISLNK(inode->i_mode)))
807     		return;
808     	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
809     		return;
810     
811     	ext2_discard_prealloc(inode);
812     
813     	blocksize = inode->i_sb->s_blocksize;
814     	iblock = (inode->i_size + blocksize-1)
815     					>> EXT2_BLOCK_SIZE_BITS(inode->i_sb);
816     
817     	block_truncate_page(inode->i_mapping, inode->i_size, ext2_get_block);
818     
819     	n = ext2_block_to_path(inode, iblock, offsets);
820     	if (n == 0)
821     		return;
822     
823     	if (n == 1) {
824     		ext2_free_data(inode, i_data+offsets[0],
825     					i_data + EXT2_NDIR_BLOCKS);
826     		goto do_indirects;
827     	}
828     
829     	partial = ext2_find_shared(inode, n, offsets, chain, &nr);
830     	/* Kill the top of shared branch (already detached) */
831     	if (nr) {
832     		if (partial == chain)
833     			mark_inode_dirty(inode);
834     		else
835     			mark_buffer_dirty_inode(partial->bh, inode);
836     		ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
837     	}
838     	/* Clear the ends of indirect blocks on the shared branch */
839     	while (partial > chain) {
840     		ext2_free_branches(inode,
841     				   partial->p + 1,
842     				   (u32*)partial->bh->b_data + addr_per_block,
843     				   (chain+n-1) - partial);
844     		mark_buffer_dirty_inode(partial->bh, inode);
845     		if (IS_SYNC(inode)) {
846     			ll_rw_block (WRITE, 1, &partial->bh);
847     			wait_on_buffer (partial->bh);
848     		}
849     		brelse (partial->bh);
850     		partial--;
851     	}
852     do_indirects:
853     	/* Kill the remaining (whole) subtrees */
854     	switch (offsets[0]) {
855     		default:
856     			nr = i_data[EXT2_IND_BLOCK];
857     			if (nr) {
858     				i_data[EXT2_IND_BLOCK] = 0;
859     				mark_inode_dirty(inode);
860     				ext2_free_branches(inode, &nr, &nr+1, 1);
861     			}
862     		case EXT2_IND_BLOCK:
863     			nr = i_data[EXT2_DIND_BLOCK];
864     			if (nr) {
865     				i_data[EXT2_DIND_BLOCK] = 0;
866     				mark_inode_dirty(inode);
867     				ext2_free_branches(inode, &nr, &nr+1, 2);
868     			}
869     		case EXT2_DIND_BLOCK:
870     			nr = i_data[EXT2_TIND_BLOCK];
871     			if (nr) {
872     				i_data[EXT2_TIND_BLOCK] = 0;
873     				mark_inode_dirty(inode);
874     				ext2_free_branches(inode, &nr, &nr+1, 3);
875     			}
876     		case EXT2_TIND_BLOCK:
877     			;
878     	}
879     	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
880     	if (IS_SYNC(inode))
881     		ext2_sync_inode (inode);
882     	else
883     		mark_inode_dirty(inode);
884     }
885     
886     void ext2_read_inode (struct inode * inode)
887     {
888     	struct buffer_head * bh;
889     	struct ext2_inode * raw_inode;
890     	unsigned long block_group;
891     	unsigned long group_desc;
892     	unsigned long desc;
893     	unsigned long block;
894     	unsigned long offset;
895     	struct ext2_group_desc * gdp;
896     
897     	if ((inode->i_ino != EXT2_ROOT_INO && inode->i_ino != EXT2_ACL_IDX_INO &&
898     	     inode->i_ino != EXT2_ACL_DATA_INO &&
899     	     inode->i_ino < EXT2_FIRST_INO(inode->i_sb)) ||
900     	    inode->i_ino > le32_to_cpu(inode->i_sb->u.ext2_sb.s_es->s_inodes_count)) {
901     		ext2_error (inode->i_sb, "ext2_read_inode",
902     			    "bad inode number: %lu", inode->i_ino);
903     		goto bad_inode;
904     	}
905     	block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
906     	if (block_group >= inode->i_sb->u.ext2_sb.s_groups_count) {
907     		ext2_error (inode->i_sb, "ext2_read_inode",
908     			    "group >= groups count");
909     		goto bad_inode;
910     	}
911     	group_desc = block_group >> EXT2_DESC_PER_BLOCK_BITS(inode->i_sb);
912     	desc = block_group & (EXT2_DESC_PER_BLOCK(inode->i_sb) - 1);
913     	bh = inode->i_sb->u.ext2_sb.s_group_desc[group_desc];
914     	if (!bh) {
915     		ext2_error (inode->i_sb, "ext2_read_inode",
916     			    "Descriptor not loaded");
917     		goto bad_inode;
918     	}
919     
920     	gdp = (struct ext2_group_desc *) bh->b_data;
921     	/*
922     	 * Figure out the offset within the block group inode table
923     	 */
924     	offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) *
925     		EXT2_INODE_SIZE(inode->i_sb);
926     	block = le32_to_cpu(gdp[desc].bg_inode_table) +
927     		(offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb));
928     	if (!(bh = bread (inode->i_dev, block, inode->i_sb->s_blocksize))) {
929     		ext2_error (inode->i_sb, "ext2_read_inode",
930     			    "unable to read inode block - "
931     			    "inode=%lu, block=%lu", inode->i_ino, block);
932     		goto bad_inode;
933     	}
934     	offset &= (EXT2_BLOCK_SIZE(inode->i_sb) - 1);
935     	raw_inode = (struct ext2_inode *) (bh->b_data + offset);
936     
937     	inode->i_mode = le16_to_cpu(raw_inode->i_mode);
938     	inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
939     	inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
940     	if(!(test_opt (inode->i_sb, NO_UID32))) {
941     		inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
942     		inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
943     	}
944     	inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
945     	inode->i_size = le32_to_cpu(raw_inode->i_size);
946     	inode->i_atime = le32_to_cpu(raw_inode->i_atime);
947     	inode->i_ctime = le32_to_cpu(raw_inode->i_ctime);
948     	inode->i_mtime = le32_to_cpu(raw_inode->i_mtime);
949     	inode->u.ext2_i.i_dtime = le32_to_cpu(raw_inode->i_dtime);
950     	/* We now have enough fields to check if the inode was active or not.
951     	 * This is needed because nfsd might try to access dead inodes
952     	 * the test is that same one that e2fsck uses
953     	 * NeilBrown 1999oct15
954     	 */
955     	if (inode->i_nlink == 0 && (inode->i_mode == 0 || inode->u.ext2_i.i_dtime)) {
956     		/* this inode is deleted */
957     		brelse (bh);
958     		goto bad_inode;
959     	}
960     	inode->i_blksize = PAGE_SIZE;	/* This is the optimal IO size (for stat), not the fs block size */
961     	inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
962     	inode->i_version = ++event;
963     	inode->u.ext2_i.i_flags = le32_to_cpu(raw_inode->i_flags);
964     	inode->u.ext2_i.i_faddr = le32_to_cpu(raw_inode->i_faddr);
965     	inode->u.ext2_i.i_frag_no = raw_inode->i_frag;
966     	inode->u.ext2_i.i_frag_size = raw_inode->i_fsize;
967     	inode->u.ext2_i.i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
968     	if (S_ISREG(inode->i_mode))
969     		inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
970     	else
971     		inode->u.ext2_i.i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
972     	inode->i_generation = le32_to_cpu(raw_inode->i_generation);
973     	inode->u.ext2_i.i_prealloc_count = 0;
974     	inode->u.ext2_i.i_block_group = block_group;
975     
976     	/*
977     	 * NOTE! The in-memory inode i_data array is in little-endian order
978     	 * even on big-endian machines: we do NOT byteswap the block numbers!
979     	 */
980     	for (block = 0; block < EXT2_N_BLOCKS; block++)
981     		inode->u.ext2_i.i_data[block] = raw_inode->i_block[block];
982     
983     	if (inode->i_ino == EXT2_ACL_IDX_INO ||
984     	    inode->i_ino == EXT2_ACL_DATA_INO)
985     		/* Nothing to do */ ;
986     	else if (S_ISREG(inode->i_mode)) {
987     		inode->i_op = &ext2_file_inode_operations;
988     		inode->i_fop = &ext2_file_operations;
989     		inode->i_mapping->a_ops = &ext2_aops;
990     	} else if (S_ISDIR(inode->i_mode)) {
991     		inode->i_op = &ext2_dir_inode_operations;
992     		inode->i_fop = &ext2_dir_operations;
993     		inode->i_mapping->a_ops = &ext2_aops;
994     	} else if (S_ISLNK(inode->i_mode)) {
995     		if (!inode->i_blocks)
996     			inode->i_op = &ext2_fast_symlink_inode_operations;
997     		else {
998     			inode->i_op = &page_symlink_inode_operations;
999     			inode->i_mapping->a_ops = &ext2_aops;
1000     		}
1001     	} else 
1002     		init_special_inode(inode, inode->i_mode,
1003     				   le32_to_cpu(raw_inode->i_block[0]));
1004     	brelse (bh);
1005     	inode->i_attr_flags = 0;
1006     	if (inode->u.ext2_i.i_flags & EXT2_SYNC_FL) {
1007     		inode->i_attr_flags |= ATTR_FLAG_SYNCRONOUS;
1008     		inode->i_flags |= S_SYNC;
1009     	}
1010     	if (inode->u.ext2_i.i_flags & EXT2_APPEND_FL) {
1011     		inode->i_attr_flags |= ATTR_FLAG_APPEND;
1012     		inode->i_flags |= S_APPEND;
1013     	}
1014     	if (inode->u.ext2_i.i_flags & EXT2_IMMUTABLE_FL) {
1015     		inode->i_attr_flags |= ATTR_FLAG_IMMUTABLE;
1016     		inode->i_flags |= S_IMMUTABLE;
1017     	}
1018     	if (inode->u.ext2_i.i_flags & EXT2_NOATIME_FL) {
1019     		inode->i_attr_flags |= ATTR_FLAG_NOATIME;
1020     		inode->i_flags |= S_NOATIME;
1021     	}
1022     	return;
1023     	
1024     bad_inode:
1025     	make_bad_inode(inode);
1026     	return;
1027     }
1028     
1029     static int ext2_update_inode(struct inode * inode, int do_sync)
1030     {
1031     	struct buffer_head * bh;
1032     	struct ext2_inode * raw_inode;
1033     	unsigned long block_group;
1034     	unsigned long group_desc;
1035     	unsigned long desc;
1036     	unsigned long block;
1037     	unsigned long offset;
1038     	int err = 0;
1039     	struct ext2_group_desc * gdp;
1040     
1041     	if ((inode->i_ino != EXT2_ROOT_INO &&
1042     	     inode->i_ino < EXT2_FIRST_INO(inode->i_sb)) ||
1043     	    inode->i_ino > le32_to_cpu(inode->i_sb->u.ext2_sb.s_es->s_inodes_count)) {
1044     		ext2_error (inode->i_sb, "ext2_write_inode",
1045     			    "bad inode number: %lu", inode->i_ino);
1046     		return -EIO;
1047     	}
1048     	block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
1049     	if (block_group >= inode->i_sb->u.ext2_sb.s_groups_count) {
1050     		ext2_error (inode->i_sb, "ext2_write_inode",
1051     			    "group >= groups count");
1052     		return -EIO;
1053     	}
1054     	group_desc = block_group >> EXT2_DESC_PER_BLOCK_BITS(inode->i_sb);
1055     	desc = block_group & (EXT2_DESC_PER_BLOCK(inode->i_sb) - 1);
1056     	bh = inode->i_sb->u.ext2_sb.s_group_desc[group_desc];
1057     	if (!bh) {
1058     		ext2_error (inode->i_sb, "ext2_write_inode",
1059     			    "Descriptor not loaded");
1060     		return -EIO;
1061     	}
1062     	gdp = (struct ext2_group_desc *) bh->b_data;
1063     	/*
1064     	 * Figure out the offset within the block group inode table
1065     	 */
1066     	offset = ((inode->i_ino - 1) % EXT2_INODES_PER_GROUP(inode->i_sb)) *
1067     		EXT2_INODE_SIZE(inode->i_sb);
1068     	block = le32_to_cpu(gdp[desc].bg_inode_table) +
1069     		(offset >> EXT2_BLOCK_SIZE_BITS(inode->i_sb));
1070     	if (!(bh = bread (inode->i_dev, block, inode->i_sb->s_blocksize))) {
1071     		ext2_error (inode->i_sb, "ext2_write_inode",
1072     			    "unable to read inode block - "
1073     			    "inode=%lu, block=%lu", inode->i_ino, block);
1074     		return -EIO;
1075     	}
1076     	offset &= EXT2_BLOCK_SIZE(inode->i_sb) - 1;
1077     	raw_inode = (struct ext2_inode *) (bh->b_data + offset);
1078     
1079     	raw_inode->i_mode = cpu_to_le16(inode->i_mode);
1080     	if(!(test_opt(inode->i_sb, NO_UID32))) {
1081     		raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
1082     		raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
1083     /*
1084      * Fix up interoperability with old kernels. Otherwise, old inodes get
1085      * re-used with the upper 16 bits of the uid/gid intact
1086      */
1087     		if(!inode->u.ext2_i.i_dtime) {
1088     			raw_inode->i_uid_high = cpu_to_le16(high_16_bits(inode->i_uid));
1089     			raw_inode->i_gid_high = cpu_to_le16(high_16_bits(inode->i_gid));
1090     		} else {
1091     			raw_inode->i_uid_high = 0;
1092     			raw_inode->i_gid_high = 0;
1093     		}
1094     	} else {
1095     		raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(inode->i_uid));
1096     		raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(inode->i_gid));
1097     		raw_inode->i_uid_high = 0;
1098     		raw_inode->i_gid_high = 0;
1099     	}
1100     	raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
1101     	raw_inode->i_size = cpu_to_le32(inode->i_size);
1102     	raw_inode->i_atime = cpu_to_le32(inode->i_atime);
1103     	raw_inode->i_ctime = cpu_to_le32(inode->i_ctime);
1104     	raw_inode->i_mtime = cpu_to_le32(inode->i_mtime);
1105     	raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
1106     	raw_inode->i_dtime = cpu_to_le32(inode->u.ext2_i.i_dtime);
1107     	raw_inode->i_flags = cpu_to_le32(inode->u.ext2_i.i_flags);
1108     	raw_inode->i_faddr = cpu_to_le32(inode->u.ext2_i.i_faddr);
1109     	raw_inode->i_frag = inode->u.ext2_i.i_frag_no;
1110     	raw_inode->i_fsize = inode->u.ext2_i.i_frag_size;
1111     	raw_inode->i_file_acl = cpu_to_le32(inode->u.ext2_i.i_file_acl);
1112     	if (S_ISDIR(inode->i_mode))
1113     		raw_inode->i_dir_acl = cpu_to_le32(inode->u.ext2_i.i_dir_acl);
1114     	else {
1115     		raw_inode->i_size_high = cpu_to_le32(inode->i_size >> 32);
1116     		if (inode->i_size > 0x7fffffffULL) {
1117     			struct super_block *sb = inode->i_sb;
1118     			if (!EXT2_HAS_RO_COMPAT_FEATURE(sb,
1119     					EXT2_FEATURE_RO_COMPAT_LARGE_FILE) ||
1120     			    EXT2_SB(sb)->s_es->s_rev_level ==
1121     					cpu_to_le32(EXT2_GOOD_OLD_REV)) {
1122     			       /* If this is the first large file
1123     				* created, add a flag to the superblock.
1124     				*/
1125     				lock_kernel();
1126     				ext2_update_dynamic_rev(sb);
1127     				EXT2_SET_RO_COMPAT_FEATURE(sb,
1128     					EXT2_FEATURE_RO_COMPAT_LARGE_FILE);
1129     				unlock_kernel();
1130     				ext2_write_super(sb);
1131     			}
1132     		}
1133     	}
1134     	
1135     	raw_inode->i_generation = cpu_to_le32(inode->i_generation);
1136     	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
1137     		raw_inode->i_block[0] = cpu_to_le32(kdev_t_to_nr(inode->i_rdev));
1138     	else for (block = 0; block < EXT2_N_BLOCKS; block++)
1139     		raw_inode->i_block[block] = inode->u.ext2_i.i_data[block];
1140     	mark_buffer_dirty(bh);
1141     	if (do_sync) {
1142     		ll_rw_block (WRITE, 1, &bh);
1143     		wait_on_buffer (bh);
1144     		if (buffer_req(bh) && !buffer_uptodate(bh)) {
1145     			printk ("IO error syncing ext2 inode ["
1146     				"%s:%08lx]\n",
1147     				bdevname(inode->i_dev), inode->i_ino);
1148     			err = -EIO;
1149     		}
1150     	}
1151     	brelse (bh);
1152     	return err;
1153     }
1154     
1155     void ext2_write_inode (struct inode * inode, int wait)
1156     {
1157     	lock_kernel();
1158     	ext2_update_inode (inode, wait);
1159     	unlock_kernel();
1160     }
1161     
1162     int ext2_sync_inode (struct inode *inode)
1163     {
1164     	return ext2_update_inode (inode, 1);
1165     }
1166     
1167     int ext2_notify_change(struct dentry *dentry, struct iattr *iattr)
1168     {
1169     	struct inode *inode = dentry->d_inode;
1170     	int		retval;
1171     	unsigned int	flags;
1172     	
1173     	retval = -EPERM;
1174     	if (iattr->ia_valid & ATTR_ATTR_FLAG &&
1175     	    ((!(iattr->ia_attr_flags & ATTR_FLAG_APPEND) !=
1176     	      !(inode->u.ext2_i.i_flags & EXT2_APPEND_FL)) ||
1177     	     (!(iattr->ia_attr_flags & ATTR_FLAG_IMMUTABLE) !=
1178     	      !(inode->u.ext2_i.i_flags & EXT2_IMMUTABLE_FL)))) {
1179     		if (!capable(CAP_LINUX_IMMUTABLE))
1180     			goto out;
1181     	} else if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
1182     		goto out;
1183     
1184     	retval = inode_change_ok(inode, iattr);
1185     	if (retval != 0)
1186     		goto out;
1187     
1188     	inode_setattr(inode, iattr);
1189     	
1190     	flags = iattr->ia_attr_flags;
1191     	if (flags & ATTR_FLAG_SYNCRONOUS) {
1192     		inode->i_flags |= S_SYNC;
1193     		inode->u.ext2_i.i_flags |= EXT2_SYNC_FL;
1194     	} else {
1195     		inode->i_flags &= ~S_SYNC;
1196     		inode->u.ext2_i.i_flags &= ~EXT2_SYNC_FL;
1197     	}
1198     	if (flags & ATTR_FLAG_NOATIME) {
1199     		inode->i_flags |= S_NOATIME;
1200     		inode->u.ext2_i.i_flags |= EXT2_NOATIME_FL;
1201     	} else {
1202     		inode->i_flags &= ~S_NOATIME;
1203     		inode->u.ext2_i.i_flags &= ~EXT2_NOATIME_FL;
1204     	}
1205     	if (flags & ATTR_FLAG_APPEND) {
1206     		inode->i_flags |= S_APPEND;
1207     		inode->u.ext2_i.i_flags |= EXT2_APPEND_FL;
1208     	} else {
1209     		inode->i_flags &= ~S_APPEND;
1210     		inode->u.ext2_i.i_flags &= ~EXT2_APPEND_FL;
1211     	}
1212     	if (flags & ATTR_FLAG_IMMUTABLE) {
1213     		inode->i_flags |= S_IMMUTABLE;
1214     		inode->u.ext2_i.i_flags |= EXT2_IMMUTABLE_FL;
1215     	} else {
1216     		inode->i_flags &= ~S_IMMUTABLE;
1217     		inode->u.ext2_i.i_flags &= ~EXT2_IMMUTABLE_FL;
1218     	}
1219     	mark_inode_dirty(inode);
1220     out:
1221     	return retval;
1222     }
1223     
1224