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