xref: /OK3568_Linux_fs/kernel/fs/minix/itree_common.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /* Generic part */
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun typedef struct {
5*4882a593Smuzhiyun 	block_t	*p;
6*4882a593Smuzhiyun 	block_t	key;
7*4882a593Smuzhiyun 	struct buffer_head *bh;
8*4882a593Smuzhiyun } Indirect;
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun static DEFINE_RWLOCK(pointers_lock);
11*4882a593Smuzhiyun 
add_chain(Indirect * p,struct buffer_head * bh,block_t * v)12*4882a593Smuzhiyun static inline void add_chain(Indirect *p, struct buffer_head *bh, block_t *v)
13*4882a593Smuzhiyun {
14*4882a593Smuzhiyun 	p->key = *(p->p = v);
15*4882a593Smuzhiyun 	p->bh = bh;
16*4882a593Smuzhiyun }
17*4882a593Smuzhiyun 
verify_chain(Indirect * from,Indirect * to)18*4882a593Smuzhiyun static inline int verify_chain(Indirect *from, Indirect *to)
19*4882a593Smuzhiyun {
20*4882a593Smuzhiyun 	while (from <= to && from->key == *from->p)
21*4882a593Smuzhiyun 		from++;
22*4882a593Smuzhiyun 	return (from > to);
23*4882a593Smuzhiyun }
24*4882a593Smuzhiyun 
block_end(struct buffer_head * bh)25*4882a593Smuzhiyun static inline block_t *block_end(struct buffer_head *bh)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun 	return (block_t *)((char*)bh->b_data + bh->b_size);
28*4882a593Smuzhiyun }
29*4882a593Smuzhiyun 
get_branch(struct inode * inode,int depth,int * offsets,Indirect chain[DEPTH],int * err)30*4882a593Smuzhiyun static inline Indirect *get_branch(struct inode *inode,
31*4882a593Smuzhiyun 					int depth,
32*4882a593Smuzhiyun 					int *offsets,
33*4882a593Smuzhiyun 					Indirect chain[DEPTH],
34*4882a593Smuzhiyun 					int *err)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun 	struct super_block *sb = inode->i_sb;
37*4882a593Smuzhiyun 	Indirect *p = chain;
38*4882a593Smuzhiyun 	struct buffer_head *bh;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	*err = 0;
41*4882a593Smuzhiyun 	/* i_data is not going away, no lock needed */
42*4882a593Smuzhiyun 	add_chain (chain, NULL, i_data(inode) + *offsets);
43*4882a593Smuzhiyun 	if (!p->key)
44*4882a593Smuzhiyun 		goto no_block;
45*4882a593Smuzhiyun 	while (--depth) {
46*4882a593Smuzhiyun 		bh = sb_bread(sb, block_to_cpu(p->key));
47*4882a593Smuzhiyun 		if (!bh)
48*4882a593Smuzhiyun 			goto failure;
49*4882a593Smuzhiyun 		read_lock(&pointers_lock);
50*4882a593Smuzhiyun 		if (!verify_chain(chain, p))
51*4882a593Smuzhiyun 			goto changed;
52*4882a593Smuzhiyun 		add_chain(++p, bh, (block_t *)bh->b_data + *++offsets);
53*4882a593Smuzhiyun 		read_unlock(&pointers_lock);
54*4882a593Smuzhiyun 		if (!p->key)
55*4882a593Smuzhiyun 			goto no_block;
56*4882a593Smuzhiyun 	}
57*4882a593Smuzhiyun 	return NULL;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun changed:
60*4882a593Smuzhiyun 	read_unlock(&pointers_lock);
61*4882a593Smuzhiyun 	brelse(bh);
62*4882a593Smuzhiyun 	*err = -EAGAIN;
63*4882a593Smuzhiyun 	goto no_block;
64*4882a593Smuzhiyun failure:
65*4882a593Smuzhiyun 	*err = -EIO;
66*4882a593Smuzhiyun no_block:
67*4882a593Smuzhiyun 	return p;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun 
alloc_branch(struct inode * inode,int num,int * offsets,Indirect * branch)70*4882a593Smuzhiyun static int alloc_branch(struct inode *inode,
71*4882a593Smuzhiyun 			     int num,
72*4882a593Smuzhiyun 			     int *offsets,
73*4882a593Smuzhiyun 			     Indirect *branch)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	int n = 0;
76*4882a593Smuzhiyun 	int i;
77*4882a593Smuzhiyun 	int parent = minix_new_block(inode);
78*4882a593Smuzhiyun 	int err = -ENOSPC;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	branch[0].key = cpu_to_block(parent);
81*4882a593Smuzhiyun 	if (parent) for (n = 1; n < num; n++) {
82*4882a593Smuzhiyun 		struct buffer_head *bh;
83*4882a593Smuzhiyun 		/* Allocate the next block */
84*4882a593Smuzhiyun 		int nr = minix_new_block(inode);
85*4882a593Smuzhiyun 		if (!nr)
86*4882a593Smuzhiyun 			break;
87*4882a593Smuzhiyun 		branch[n].key = cpu_to_block(nr);
88*4882a593Smuzhiyun 		bh = sb_getblk(inode->i_sb, parent);
89*4882a593Smuzhiyun 		if (!bh) {
90*4882a593Smuzhiyun 			minix_free_block(inode, nr);
91*4882a593Smuzhiyun 			err = -ENOMEM;
92*4882a593Smuzhiyun 			break;
93*4882a593Smuzhiyun 		}
94*4882a593Smuzhiyun 		lock_buffer(bh);
95*4882a593Smuzhiyun 		memset(bh->b_data, 0, bh->b_size);
96*4882a593Smuzhiyun 		branch[n].bh = bh;
97*4882a593Smuzhiyun 		branch[n].p = (block_t*) bh->b_data + offsets[n];
98*4882a593Smuzhiyun 		*branch[n].p = branch[n].key;
99*4882a593Smuzhiyun 		set_buffer_uptodate(bh);
100*4882a593Smuzhiyun 		unlock_buffer(bh);
101*4882a593Smuzhiyun 		mark_buffer_dirty_inode(bh, inode);
102*4882a593Smuzhiyun 		parent = nr;
103*4882a593Smuzhiyun 	}
104*4882a593Smuzhiyun 	if (n == num)
105*4882a593Smuzhiyun 		return 0;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	/* Allocation failed, free what we already allocated */
108*4882a593Smuzhiyun 	for (i = 1; i < n; i++)
109*4882a593Smuzhiyun 		bforget(branch[i].bh);
110*4882a593Smuzhiyun 	for (i = 0; i < n; i++)
111*4882a593Smuzhiyun 		minix_free_block(inode, block_to_cpu(branch[i].key));
112*4882a593Smuzhiyun 	return err;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
splice_branch(struct inode * inode,Indirect chain[DEPTH],Indirect * where,int num)115*4882a593Smuzhiyun static inline int splice_branch(struct inode *inode,
116*4882a593Smuzhiyun 				     Indirect chain[DEPTH],
117*4882a593Smuzhiyun 				     Indirect *where,
118*4882a593Smuzhiyun 				     int num)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun 	int i;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	write_lock(&pointers_lock);
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	/* Verify that place we are splicing to is still there and vacant */
125*4882a593Smuzhiyun 	if (!verify_chain(chain, where-1) || *where->p)
126*4882a593Smuzhiyun 		goto changed;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	*where->p = where->key;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	write_unlock(&pointers_lock);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	/* We are done with atomic stuff, now do the rest of housekeeping */
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	inode->i_ctime = current_time(inode);
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	/* had we spliced it onto indirect block? */
137*4882a593Smuzhiyun 	if (where->bh)
138*4882a593Smuzhiyun 		mark_buffer_dirty_inode(where->bh, inode);
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	mark_inode_dirty(inode);
141*4882a593Smuzhiyun 	return 0;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun changed:
144*4882a593Smuzhiyun 	write_unlock(&pointers_lock);
145*4882a593Smuzhiyun 	for (i = 1; i < num; i++)
146*4882a593Smuzhiyun 		bforget(where[i].bh);
147*4882a593Smuzhiyun 	for (i = 0; i < num; i++)
148*4882a593Smuzhiyun 		minix_free_block(inode, block_to_cpu(where[i].key));
149*4882a593Smuzhiyun 	return -EAGAIN;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun 
get_block(struct inode * inode,sector_t block,struct buffer_head * bh,int create)152*4882a593Smuzhiyun static int get_block(struct inode * inode, sector_t block,
153*4882a593Smuzhiyun 			struct buffer_head *bh, int create)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun 	int err = -EIO;
156*4882a593Smuzhiyun 	int offsets[DEPTH];
157*4882a593Smuzhiyun 	Indirect chain[DEPTH];
158*4882a593Smuzhiyun 	Indirect *partial;
159*4882a593Smuzhiyun 	int left;
160*4882a593Smuzhiyun 	int depth = block_to_path(inode, block, offsets);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	if (depth == 0)
163*4882a593Smuzhiyun 		goto out;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun reread:
166*4882a593Smuzhiyun 	partial = get_branch(inode, depth, offsets, chain, &err);
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	/* Simplest case - block found, no allocation needed */
169*4882a593Smuzhiyun 	if (!partial) {
170*4882a593Smuzhiyun got_it:
171*4882a593Smuzhiyun 		map_bh(bh, inode->i_sb, block_to_cpu(chain[depth-1].key));
172*4882a593Smuzhiyun 		/* Clean up and exit */
173*4882a593Smuzhiyun 		partial = chain+depth-1; /* the whole chain */
174*4882a593Smuzhiyun 		goto cleanup;
175*4882a593Smuzhiyun 	}
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	/* Next simple case - plain lookup or failed read of indirect block */
178*4882a593Smuzhiyun 	if (!create || err == -EIO) {
179*4882a593Smuzhiyun cleanup:
180*4882a593Smuzhiyun 		while (partial > chain) {
181*4882a593Smuzhiyun 			brelse(partial->bh);
182*4882a593Smuzhiyun 			partial--;
183*4882a593Smuzhiyun 		}
184*4882a593Smuzhiyun out:
185*4882a593Smuzhiyun 		return err;
186*4882a593Smuzhiyun 	}
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	/*
189*4882a593Smuzhiyun 	 * Indirect block might be removed by truncate while we were
190*4882a593Smuzhiyun 	 * reading it. Handling of that case (forget what we've got and
191*4882a593Smuzhiyun 	 * reread) is taken out of the main path.
192*4882a593Smuzhiyun 	 */
193*4882a593Smuzhiyun 	if (err == -EAGAIN)
194*4882a593Smuzhiyun 		goto changed;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	left = (chain + depth) - partial;
197*4882a593Smuzhiyun 	err = alloc_branch(inode, left, offsets+(partial-chain), partial);
198*4882a593Smuzhiyun 	if (err)
199*4882a593Smuzhiyun 		goto cleanup;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	if (splice_branch(inode, chain, partial, left) < 0)
202*4882a593Smuzhiyun 		goto changed;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	set_buffer_new(bh);
205*4882a593Smuzhiyun 	goto got_it;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun changed:
208*4882a593Smuzhiyun 	while (partial > chain) {
209*4882a593Smuzhiyun 		brelse(partial->bh);
210*4882a593Smuzhiyun 		partial--;
211*4882a593Smuzhiyun 	}
212*4882a593Smuzhiyun 	goto reread;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun 
all_zeroes(block_t * p,block_t * q)215*4882a593Smuzhiyun static inline int all_zeroes(block_t *p, block_t *q)
216*4882a593Smuzhiyun {
217*4882a593Smuzhiyun 	while (p < q)
218*4882a593Smuzhiyun 		if (*p++)
219*4882a593Smuzhiyun 			return 0;
220*4882a593Smuzhiyun 	return 1;
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun 
find_shared(struct inode * inode,int depth,int offsets[DEPTH],Indirect chain[DEPTH],block_t * top)223*4882a593Smuzhiyun static Indirect *find_shared(struct inode *inode,
224*4882a593Smuzhiyun 				int depth,
225*4882a593Smuzhiyun 				int offsets[DEPTH],
226*4882a593Smuzhiyun 				Indirect chain[DEPTH],
227*4882a593Smuzhiyun 				block_t *top)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun 	Indirect *partial, *p;
230*4882a593Smuzhiyun 	int k, err;
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	*top = 0;
233*4882a593Smuzhiyun 	for (k = depth; k > 1 && !offsets[k-1]; k--)
234*4882a593Smuzhiyun 		;
235*4882a593Smuzhiyun 	partial = get_branch(inode, k, offsets, chain, &err);
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	write_lock(&pointers_lock);
238*4882a593Smuzhiyun 	if (!partial)
239*4882a593Smuzhiyun 		partial = chain + k-1;
240*4882a593Smuzhiyun 	if (!partial->key && *partial->p) {
241*4882a593Smuzhiyun 		write_unlock(&pointers_lock);
242*4882a593Smuzhiyun 		goto no_top;
243*4882a593Smuzhiyun 	}
244*4882a593Smuzhiyun 	for (p=partial;p>chain && all_zeroes((block_t*)p->bh->b_data,p->p);p--)
245*4882a593Smuzhiyun 		;
246*4882a593Smuzhiyun 	if (p == chain + k - 1 && p > chain) {
247*4882a593Smuzhiyun 		p->p--;
248*4882a593Smuzhiyun 	} else {
249*4882a593Smuzhiyun 		*top = *p->p;
250*4882a593Smuzhiyun 		*p->p = 0;
251*4882a593Smuzhiyun 	}
252*4882a593Smuzhiyun 	write_unlock(&pointers_lock);
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	while(partial > p)
255*4882a593Smuzhiyun 	{
256*4882a593Smuzhiyun 		brelse(partial->bh);
257*4882a593Smuzhiyun 		partial--;
258*4882a593Smuzhiyun 	}
259*4882a593Smuzhiyun no_top:
260*4882a593Smuzhiyun 	return partial;
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun 
free_data(struct inode * inode,block_t * p,block_t * q)263*4882a593Smuzhiyun static inline void free_data(struct inode *inode, block_t *p, block_t *q)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun 	unsigned long nr;
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	for ( ; p < q ; p++) {
268*4882a593Smuzhiyun 		nr = block_to_cpu(*p);
269*4882a593Smuzhiyun 		if (nr) {
270*4882a593Smuzhiyun 			*p = 0;
271*4882a593Smuzhiyun 			minix_free_block(inode, nr);
272*4882a593Smuzhiyun 		}
273*4882a593Smuzhiyun 	}
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun 
free_branches(struct inode * inode,block_t * p,block_t * q,int depth)276*4882a593Smuzhiyun static void free_branches(struct inode *inode, block_t *p, block_t *q, int depth)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun 	struct buffer_head * bh;
279*4882a593Smuzhiyun 	unsigned long nr;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	if (depth--) {
282*4882a593Smuzhiyun 		for ( ; p < q ; p++) {
283*4882a593Smuzhiyun 			nr = block_to_cpu(*p);
284*4882a593Smuzhiyun 			if (!nr)
285*4882a593Smuzhiyun 				continue;
286*4882a593Smuzhiyun 			*p = 0;
287*4882a593Smuzhiyun 			bh = sb_bread(inode->i_sb, nr);
288*4882a593Smuzhiyun 			if (!bh)
289*4882a593Smuzhiyun 				continue;
290*4882a593Smuzhiyun 			free_branches(inode, (block_t*)bh->b_data,
291*4882a593Smuzhiyun 				      block_end(bh), depth);
292*4882a593Smuzhiyun 			bforget(bh);
293*4882a593Smuzhiyun 			minix_free_block(inode, nr);
294*4882a593Smuzhiyun 			mark_inode_dirty(inode);
295*4882a593Smuzhiyun 		}
296*4882a593Smuzhiyun 	} else
297*4882a593Smuzhiyun 		free_data(inode, p, q);
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun 
truncate(struct inode * inode)300*4882a593Smuzhiyun static inline void truncate (struct inode * inode)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun 	struct super_block *sb = inode->i_sb;
303*4882a593Smuzhiyun 	block_t *idata = i_data(inode);
304*4882a593Smuzhiyun 	int offsets[DEPTH];
305*4882a593Smuzhiyun 	Indirect chain[DEPTH];
306*4882a593Smuzhiyun 	Indirect *partial;
307*4882a593Smuzhiyun 	block_t nr = 0;
308*4882a593Smuzhiyun 	int n;
309*4882a593Smuzhiyun 	int first_whole;
310*4882a593Smuzhiyun 	long iblock;
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	iblock = (inode->i_size + sb->s_blocksize -1) >> sb->s_blocksize_bits;
313*4882a593Smuzhiyun 	block_truncate_page(inode->i_mapping, inode->i_size, get_block);
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	n = block_to_path(inode, iblock, offsets);
316*4882a593Smuzhiyun 	if (!n)
317*4882a593Smuzhiyun 		return;
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	if (n == 1) {
320*4882a593Smuzhiyun 		free_data(inode, idata+offsets[0], idata + DIRECT);
321*4882a593Smuzhiyun 		first_whole = 0;
322*4882a593Smuzhiyun 		goto do_indirects;
323*4882a593Smuzhiyun 	}
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	first_whole = offsets[0] + 1 - DIRECT;
326*4882a593Smuzhiyun 	partial = find_shared(inode, n, offsets, chain, &nr);
327*4882a593Smuzhiyun 	if (nr) {
328*4882a593Smuzhiyun 		if (partial == chain)
329*4882a593Smuzhiyun 			mark_inode_dirty(inode);
330*4882a593Smuzhiyun 		else
331*4882a593Smuzhiyun 			mark_buffer_dirty_inode(partial->bh, inode);
332*4882a593Smuzhiyun 		free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
333*4882a593Smuzhiyun 	}
334*4882a593Smuzhiyun 	/* Clear the ends of indirect blocks on the shared branch */
335*4882a593Smuzhiyun 	while (partial > chain) {
336*4882a593Smuzhiyun 		free_branches(inode, partial->p + 1, block_end(partial->bh),
337*4882a593Smuzhiyun 				(chain+n-1) - partial);
338*4882a593Smuzhiyun 		mark_buffer_dirty_inode(partial->bh, inode);
339*4882a593Smuzhiyun 		brelse (partial->bh);
340*4882a593Smuzhiyun 		partial--;
341*4882a593Smuzhiyun 	}
342*4882a593Smuzhiyun do_indirects:
343*4882a593Smuzhiyun 	/* Kill the remaining (whole) subtrees */
344*4882a593Smuzhiyun 	while (first_whole < DEPTH-1) {
345*4882a593Smuzhiyun 		nr = idata[DIRECT+first_whole];
346*4882a593Smuzhiyun 		if (nr) {
347*4882a593Smuzhiyun 			idata[DIRECT+first_whole] = 0;
348*4882a593Smuzhiyun 			mark_inode_dirty(inode);
349*4882a593Smuzhiyun 			free_branches(inode, &nr, &nr+1, first_whole+1);
350*4882a593Smuzhiyun 		}
351*4882a593Smuzhiyun 		first_whole++;
352*4882a593Smuzhiyun 	}
353*4882a593Smuzhiyun 	inode->i_mtime = inode->i_ctime = current_time(inode);
354*4882a593Smuzhiyun 	mark_inode_dirty(inode);
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun 
nblocks(loff_t size,struct super_block * sb)357*4882a593Smuzhiyun static inline unsigned nblocks(loff_t size, struct super_block *sb)
358*4882a593Smuzhiyun {
359*4882a593Smuzhiyun 	int k = sb->s_blocksize_bits - 10;
360*4882a593Smuzhiyun 	unsigned blocks, res, direct = DIRECT, i = DEPTH;
361*4882a593Smuzhiyun 	blocks = (size + sb->s_blocksize - 1) >> (BLOCK_SIZE_BITS + k);
362*4882a593Smuzhiyun 	res = blocks;
363*4882a593Smuzhiyun 	while (--i && blocks > direct) {
364*4882a593Smuzhiyun 		blocks -= direct;
365*4882a593Smuzhiyun 		blocks += sb->s_blocksize/sizeof(block_t) - 1;
366*4882a593Smuzhiyun 		blocks /= sb->s_blocksize/sizeof(block_t);
367*4882a593Smuzhiyun 		res += blocks;
368*4882a593Smuzhiyun 		direct = 1;
369*4882a593Smuzhiyun 	}
370*4882a593Smuzhiyun 	return res;
371*4882a593Smuzhiyun }
372