xref: /OK3568_Linux_fs/kernel/fs/ubifs/find.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * This file is part of UBIFS.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2006-2008 Nokia Corporation.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Authors: Artem Bityutskiy (Битюцкий Артём)
8*4882a593Smuzhiyun  *          Adrian Hunter
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun /*
12*4882a593Smuzhiyun  * This file contains functions for finding LEBs for various purposes e.g.
13*4882a593Smuzhiyun  * garbage collection. In general, lprops category heaps and lists are used
14*4882a593Smuzhiyun  * for fast access, falling back on scanning the LPT as a last resort.
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <linux/sort.h>
18*4882a593Smuzhiyun #include "ubifs.h"
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun /**
21*4882a593Smuzhiyun  * struct scan_data - data provided to scan callback functions
22*4882a593Smuzhiyun  * @min_space: minimum number of bytes for which to scan
23*4882a593Smuzhiyun  * @pick_free: whether it is OK to scan for empty LEBs
24*4882a593Smuzhiyun  * @lnum: LEB number found is returned here
25*4882a593Smuzhiyun  * @exclude_index: whether to exclude index LEBs
26*4882a593Smuzhiyun  */
27*4882a593Smuzhiyun struct scan_data {
28*4882a593Smuzhiyun 	int min_space;
29*4882a593Smuzhiyun 	int pick_free;
30*4882a593Smuzhiyun 	int lnum;
31*4882a593Smuzhiyun 	int exclude_index;
32*4882a593Smuzhiyun };
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /**
35*4882a593Smuzhiyun  * valuable - determine whether LEB properties are valuable.
36*4882a593Smuzhiyun  * @c: the UBIFS file-system description object
37*4882a593Smuzhiyun  * @lprops: LEB properties
38*4882a593Smuzhiyun  *
39*4882a593Smuzhiyun  * This function return %1 if the LEB properties should be added to the LEB
40*4882a593Smuzhiyun  * properties tree in memory. Otherwise %0 is returned.
41*4882a593Smuzhiyun  */
valuable(struct ubifs_info * c,const struct ubifs_lprops * lprops)42*4882a593Smuzhiyun static int valuable(struct ubifs_info *c, const struct ubifs_lprops *lprops)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	int n, cat = lprops->flags & LPROPS_CAT_MASK;
45*4882a593Smuzhiyun 	struct ubifs_lpt_heap *heap;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	switch (cat) {
48*4882a593Smuzhiyun 	case LPROPS_DIRTY:
49*4882a593Smuzhiyun 	case LPROPS_DIRTY_IDX:
50*4882a593Smuzhiyun 	case LPROPS_FREE:
51*4882a593Smuzhiyun 		heap = &c->lpt_heap[cat - 1];
52*4882a593Smuzhiyun 		if (heap->cnt < heap->max_cnt)
53*4882a593Smuzhiyun 			return 1;
54*4882a593Smuzhiyun 		if (lprops->free + lprops->dirty >= c->dark_wm)
55*4882a593Smuzhiyun 			return 1;
56*4882a593Smuzhiyun 		return 0;
57*4882a593Smuzhiyun 	case LPROPS_EMPTY:
58*4882a593Smuzhiyun 		n = c->lst.empty_lebs + c->freeable_cnt -
59*4882a593Smuzhiyun 		    c->lst.taken_empty_lebs;
60*4882a593Smuzhiyun 		if (n < c->lsave_cnt)
61*4882a593Smuzhiyun 			return 1;
62*4882a593Smuzhiyun 		return 0;
63*4882a593Smuzhiyun 	case LPROPS_FREEABLE:
64*4882a593Smuzhiyun 		return 1;
65*4882a593Smuzhiyun 	case LPROPS_FRDI_IDX:
66*4882a593Smuzhiyun 		return 1;
67*4882a593Smuzhiyun 	}
68*4882a593Smuzhiyun 	return 0;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun /**
72*4882a593Smuzhiyun  * scan_for_dirty_cb - dirty space scan callback.
73*4882a593Smuzhiyun  * @c: the UBIFS file-system description object
74*4882a593Smuzhiyun  * @lprops: LEB properties to scan
75*4882a593Smuzhiyun  * @in_tree: whether the LEB properties are in main memory
76*4882a593Smuzhiyun  * @data: information passed to and from the caller of the scan
77*4882a593Smuzhiyun  *
78*4882a593Smuzhiyun  * This function returns a code that indicates whether the scan should continue
79*4882a593Smuzhiyun  * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
80*4882a593Smuzhiyun  * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
81*4882a593Smuzhiyun  * (%LPT_SCAN_STOP).
82*4882a593Smuzhiyun  */
scan_for_dirty_cb(struct ubifs_info * c,const struct ubifs_lprops * lprops,int in_tree,struct scan_data * data)83*4882a593Smuzhiyun static int scan_for_dirty_cb(struct ubifs_info *c,
84*4882a593Smuzhiyun 			     const struct ubifs_lprops *lprops, int in_tree,
85*4882a593Smuzhiyun 			     struct scan_data *data)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	int ret = LPT_SCAN_CONTINUE;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	/* Exclude LEBs that are currently in use */
90*4882a593Smuzhiyun 	if (lprops->flags & LPROPS_TAKEN)
91*4882a593Smuzhiyun 		return LPT_SCAN_CONTINUE;
92*4882a593Smuzhiyun 	/* Determine whether to add these LEB properties to the tree */
93*4882a593Smuzhiyun 	if (!in_tree && valuable(c, lprops))
94*4882a593Smuzhiyun 		ret |= LPT_SCAN_ADD;
95*4882a593Smuzhiyun 	/* Exclude LEBs with too little space */
96*4882a593Smuzhiyun 	if (lprops->free + lprops->dirty < data->min_space)
97*4882a593Smuzhiyun 		return ret;
98*4882a593Smuzhiyun 	/* If specified, exclude index LEBs */
99*4882a593Smuzhiyun 	if (data->exclude_index && lprops->flags & LPROPS_INDEX)
100*4882a593Smuzhiyun 		return ret;
101*4882a593Smuzhiyun 	/* If specified, exclude empty or freeable LEBs */
102*4882a593Smuzhiyun 	if (lprops->free + lprops->dirty == c->leb_size) {
103*4882a593Smuzhiyun 		if (!data->pick_free)
104*4882a593Smuzhiyun 			return ret;
105*4882a593Smuzhiyun 	/* Exclude LEBs with too little dirty space (unless it is empty) */
106*4882a593Smuzhiyun 	} else if (lprops->dirty < c->dead_wm)
107*4882a593Smuzhiyun 		return ret;
108*4882a593Smuzhiyun 	/* Finally we found space */
109*4882a593Smuzhiyun 	data->lnum = lprops->lnum;
110*4882a593Smuzhiyun 	return LPT_SCAN_ADD | LPT_SCAN_STOP;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun /**
114*4882a593Smuzhiyun  * scan_for_dirty - find a data LEB with free space.
115*4882a593Smuzhiyun  * @c: the UBIFS file-system description object
116*4882a593Smuzhiyun  * @min_space: minimum amount free plus dirty space the returned LEB has to
117*4882a593Smuzhiyun  *             have
118*4882a593Smuzhiyun  * @pick_free: if it is OK to return a free or freeable LEB
119*4882a593Smuzhiyun  * @exclude_index: whether to exclude index LEBs
120*4882a593Smuzhiyun  *
121*4882a593Smuzhiyun  * This function returns a pointer to the LEB properties found or a negative
122*4882a593Smuzhiyun  * error code.
123*4882a593Smuzhiyun  */
scan_for_dirty(struct ubifs_info * c,int min_space,int pick_free,int exclude_index)124*4882a593Smuzhiyun static const struct ubifs_lprops *scan_for_dirty(struct ubifs_info *c,
125*4882a593Smuzhiyun 						 int min_space, int pick_free,
126*4882a593Smuzhiyun 						 int exclude_index)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	const struct ubifs_lprops *lprops;
129*4882a593Smuzhiyun 	struct ubifs_lpt_heap *heap;
130*4882a593Smuzhiyun 	struct scan_data data;
131*4882a593Smuzhiyun 	int err, i;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	/* There may be an LEB with enough dirty space on the free heap */
134*4882a593Smuzhiyun 	heap = &c->lpt_heap[LPROPS_FREE - 1];
135*4882a593Smuzhiyun 	for (i = 0; i < heap->cnt; i++) {
136*4882a593Smuzhiyun 		lprops = heap->arr[i];
137*4882a593Smuzhiyun 		if (lprops->free + lprops->dirty < min_space)
138*4882a593Smuzhiyun 			continue;
139*4882a593Smuzhiyun 		if (lprops->dirty < c->dead_wm)
140*4882a593Smuzhiyun 			continue;
141*4882a593Smuzhiyun 		return lprops;
142*4882a593Smuzhiyun 	}
143*4882a593Smuzhiyun 	/*
144*4882a593Smuzhiyun 	 * A LEB may have fallen off of the bottom of the dirty heap, and ended
145*4882a593Smuzhiyun 	 * up as uncategorized even though it has enough dirty space for us now,
146*4882a593Smuzhiyun 	 * so check the uncategorized list. N.B. neither empty nor freeable LEBs
147*4882a593Smuzhiyun 	 * can end up as uncategorized because they are kept on lists not
148*4882a593Smuzhiyun 	 * finite-sized heaps.
149*4882a593Smuzhiyun 	 */
150*4882a593Smuzhiyun 	list_for_each_entry(lprops, &c->uncat_list, list) {
151*4882a593Smuzhiyun 		if (lprops->flags & LPROPS_TAKEN)
152*4882a593Smuzhiyun 			continue;
153*4882a593Smuzhiyun 		if (lprops->free + lprops->dirty < min_space)
154*4882a593Smuzhiyun 			continue;
155*4882a593Smuzhiyun 		if (exclude_index && (lprops->flags & LPROPS_INDEX))
156*4882a593Smuzhiyun 			continue;
157*4882a593Smuzhiyun 		if (lprops->dirty < c->dead_wm)
158*4882a593Smuzhiyun 			continue;
159*4882a593Smuzhiyun 		return lprops;
160*4882a593Smuzhiyun 	}
161*4882a593Smuzhiyun 	/* We have looked everywhere in main memory, now scan the flash */
162*4882a593Smuzhiyun 	if (c->pnodes_have >= c->pnode_cnt)
163*4882a593Smuzhiyun 		/* All pnodes are in memory, so skip scan */
164*4882a593Smuzhiyun 		return ERR_PTR(-ENOSPC);
165*4882a593Smuzhiyun 	data.min_space = min_space;
166*4882a593Smuzhiyun 	data.pick_free = pick_free;
167*4882a593Smuzhiyun 	data.lnum = -1;
168*4882a593Smuzhiyun 	data.exclude_index = exclude_index;
169*4882a593Smuzhiyun 	err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
170*4882a593Smuzhiyun 				    (ubifs_lpt_scan_callback)scan_for_dirty_cb,
171*4882a593Smuzhiyun 				    &data);
172*4882a593Smuzhiyun 	if (err)
173*4882a593Smuzhiyun 		return ERR_PTR(err);
174*4882a593Smuzhiyun 	ubifs_assert(c, data.lnum >= c->main_first && data.lnum < c->leb_cnt);
175*4882a593Smuzhiyun 	c->lscan_lnum = data.lnum;
176*4882a593Smuzhiyun 	lprops = ubifs_lpt_lookup_dirty(c, data.lnum);
177*4882a593Smuzhiyun 	if (IS_ERR(lprops))
178*4882a593Smuzhiyun 		return lprops;
179*4882a593Smuzhiyun 	ubifs_assert(c, lprops->lnum == data.lnum);
180*4882a593Smuzhiyun 	ubifs_assert(c, lprops->free + lprops->dirty >= min_space);
181*4882a593Smuzhiyun 	ubifs_assert(c, lprops->dirty >= c->dead_wm ||
182*4882a593Smuzhiyun 		     (pick_free &&
183*4882a593Smuzhiyun 		      lprops->free + lprops->dirty == c->leb_size));
184*4882a593Smuzhiyun 	ubifs_assert(c, !(lprops->flags & LPROPS_TAKEN));
185*4882a593Smuzhiyun 	ubifs_assert(c, !exclude_index || !(lprops->flags & LPROPS_INDEX));
186*4882a593Smuzhiyun 	return lprops;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun /**
190*4882a593Smuzhiyun  * ubifs_find_dirty_leb - find a dirty LEB for the Garbage Collector.
191*4882a593Smuzhiyun  * @c: the UBIFS file-system description object
192*4882a593Smuzhiyun  * @ret_lp: LEB properties are returned here on exit
193*4882a593Smuzhiyun  * @min_space: minimum amount free plus dirty space the returned LEB has to
194*4882a593Smuzhiyun  *             have
195*4882a593Smuzhiyun  * @pick_free: controls whether it is OK to pick empty or index LEBs
196*4882a593Smuzhiyun  *
197*4882a593Smuzhiyun  * This function tries to find a dirty logical eraseblock which has at least
198*4882a593Smuzhiyun  * @min_space free and dirty space. It prefers to take an LEB from the dirty or
199*4882a593Smuzhiyun  * dirty index heap, and it falls-back to LPT scanning if the heaps are empty
200*4882a593Smuzhiyun  * or do not have an LEB which satisfies the @min_space criteria.
201*4882a593Smuzhiyun  *
202*4882a593Smuzhiyun  * Note, LEBs which have less than dead watermark of free + dirty space are
203*4882a593Smuzhiyun  * never picked by this function.
204*4882a593Smuzhiyun  *
205*4882a593Smuzhiyun  * The additional @pick_free argument controls if this function has to return a
206*4882a593Smuzhiyun  * free or freeable LEB if one is present. For example, GC must to set it to %1,
207*4882a593Smuzhiyun  * when called from the journal space reservation function, because the
208*4882a593Smuzhiyun  * appearance of free space may coincide with the loss of enough dirty space
209*4882a593Smuzhiyun  * for GC to succeed anyway.
210*4882a593Smuzhiyun  *
211*4882a593Smuzhiyun  * In contrast, if the Garbage Collector is called from budgeting, it should
212*4882a593Smuzhiyun  * just make free space, not return LEBs which are already free or freeable.
213*4882a593Smuzhiyun  *
214*4882a593Smuzhiyun  * In addition @pick_free is set to %2 by the recovery process in order to
215*4882a593Smuzhiyun  * recover gc_lnum in which case an index LEB must not be returned.
216*4882a593Smuzhiyun  *
217*4882a593Smuzhiyun  * This function returns zero and the LEB properties of found dirty LEB in case
218*4882a593Smuzhiyun  * of success, %-ENOSPC if no dirty LEB was found and a negative error code in
219*4882a593Smuzhiyun  * case of other failures. The returned LEB is marked as "taken".
220*4882a593Smuzhiyun  */
ubifs_find_dirty_leb(struct ubifs_info * c,struct ubifs_lprops * ret_lp,int min_space,int pick_free)221*4882a593Smuzhiyun int ubifs_find_dirty_leb(struct ubifs_info *c, struct ubifs_lprops *ret_lp,
222*4882a593Smuzhiyun 			 int min_space, int pick_free)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun 	int err = 0, sum, exclude_index = pick_free == 2 ? 1 : 0;
225*4882a593Smuzhiyun 	const struct ubifs_lprops *lp = NULL, *idx_lp = NULL;
226*4882a593Smuzhiyun 	struct ubifs_lpt_heap *heap, *idx_heap;
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	ubifs_get_lprops(c);
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	if (pick_free) {
231*4882a593Smuzhiyun 		int lebs, rsvd_idx_lebs = 0;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 		spin_lock(&c->space_lock);
234*4882a593Smuzhiyun 		lebs = c->lst.empty_lebs + c->idx_gc_cnt;
235*4882a593Smuzhiyun 		lebs += c->freeable_cnt - c->lst.taken_empty_lebs;
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 		/*
238*4882a593Smuzhiyun 		 * Note, the index may consume more LEBs than have been reserved
239*4882a593Smuzhiyun 		 * for it. It is OK because it might be consolidated by GC.
240*4882a593Smuzhiyun 		 * But if the index takes fewer LEBs than it is reserved for it,
241*4882a593Smuzhiyun 		 * this function must avoid picking those reserved LEBs.
242*4882a593Smuzhiyun 		 */
243*4882a593Smuzhiyun 		if (c->bi.min_idx_lebs >= c->lst.idx_lebs) {
244*4882a593Smuzhiyun 			rsvd_idx_lebs = c->bi.min_idx_lebs -  c->lst.idx_lebs;
245*4882a593Smuzhiyun 			exclude_index = 1;
246*4882a593Smuzhiyun 		}
247*4882a593Smuzhiyun 		spin_unlock(&c->space_lock);
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 		/* Check if there are enough free LEBs for the index */
250*4882a593Smuzhiyun 		if (rsvd_idx_lebs < lebs) {
251*4882a593Smuzhiyun 			/* OK, try to find an empty LEB */
252*4882a593Smuzhiyun 			lp = ubifs_fast_find_empty(c);
253*4882a593Smuzhiyun 			if (lp)
254*4882a593Smuzhiyun 				goto found;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 			/* Or a freeable LEB */
257*4882a593Smuzhiyun 			lp = ubifs_fast_find_freeable(c);
258*4882a593Smuzhiyun 			if (lp)
259*4882a593Smuzhiyun 				goto found;
260*4882a593Smuzhiyun 		} else
261*4882a593Smuzhiyun 			/*
262*4882a593Smuzhiyun 			 * We cannot pick free/freeable LEBs in the below code.
263*4882a593Smuzhiyun 			 */
264*4882a593Smuzhiyun 			pick_free = 0;
265*4882a593Smuzhiyun 	} else {
266*4882a593Smuzhiyun 		spin_lock(&c->space_lock);
267*4882a593Smuzhiyun 		exclude_index = (c->bi.min_idx_lebs >= c->lst.idx_lebs);
268*4882a593Smuzhiyun 		spin_unlock(&c->space_lock);
269*4882a593Smuzhiyun 	}
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	/* Look on the dirty and dirty index heaps */
272*4882a593Smuzhiyun 	heap = &c->lpt_heap[LPROPS_DIRTY - 1];
273*4882a593Smuzhiyun 	idx_heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	if (idx_heap->cnt && !exclude_index) {
276*4882a593Smuzhiyun 		idx_lp = idx_heap->arr[0];
277*4882a593Smuzhiyun 		sum = idx_lp->free + idx_lp->dirty;
278*4882a593Smuzhiyun 		/*
279*4882a593Smuzhiyun 		 * Since we reserve thrice as much space for the index than it
280*4882a593Smuzhiyun 		 * actually takes, it does not make sense to pick indexing LEBs
281*4882a593Smuzhiyun 		 * with less than, say, half LEB of dirty space. May be half is
282*4882a593Smuzhiyun 		 * not the optimal boundary - this should be tested and
283*4882a593Smuzhiyun 		 * checked. This boundary should determine how much we use
284*4882a593Smuzhiyun 		 * in-the-gaps to consolidate the index comparing to how much
285*4882a593Smuzhiyun 		 * we use garbage collector to consolidate it. The "half"
286*4882a593Smuzhiyun 		 * criteria just feels to be fine.
287*4882a593Smuzhiyun 		 */
288*4882a593Smuzhiyun 		if (sum < min_space || sum < c->half_leb_size)
289*4882a593Smuzhiyun 			idx_lp = NULL;
290*4882a593Smuzhiyun 	}
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	if (heap->cnt) {
293*4882a593Smuzhiyun 		lp = heap->arr[0];
294*4882a593Smuzhiyun 		if (lp->dirty + lp->free < min_space)
295*4882a593Smuzhiyun 			lp = NULL;
296*4882a593Smuzhiyun 	}
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	/* Pick the LEB with most space */
299*4882a593Smuzhiyun 	if (idx_lp && lp) {
300*4882a593Smuzhiyun 		if (idx_lp->free + idx_lp->dirty >= lp->free + lp->dirty)
301*4882a593Smuzhiyun 			lp = idx_lp;
302*4882a593Smuzhiyun 	} else if (idx_lp && !lp)
303*4882a593Smuzhiyun 		lp = idx_lp;
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 	if (lp) {
306*4882a593Smuzhiyun 		ubifs_assert(c, lp->free + lp->dirty >= c->dead_wm);
307*4882a593Smuzhiyun 		goto found;
308*4882a593Smuzhiyun 	}
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	/* Did not find a dirty LEB on the dirty heaps, have to scan */
311*4882a593Smuzhiyun 	dbg_find("scanning LPT for a dirty LEB");
312*4882a593Smuzhiyun 	lp = scan_for_dirty(c, min_space, pick_free, exclude_index);
313*4882a593Smuzhiyun 	if (IS_ERR(lp)) {
314*4882a593Smuzhiyun 		err = PTR_ERR(lp);
315*4882a593Smuzhiyun 		goto out;
316*4882a593Smuzhiyun 	}
317*4882a593Smuzhiyun 	ubifs_assert(c, lp->dirty >= c->dead_wm ||
318*4882a593Smuzhiyun 		     (pick_free && lp->free + lp->dirty == c->leb_size));
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun found:
321*4882a593Smuzhiyun 	dbg_find("found LEB %d, free %d, dirty %d, flags %#x",
322*4882a593Smuzhiyun 		 lp->lnum, lp->free, lp->dirty, lp->flags);
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
325*4882a593Smuzhiyun 			     lp->flags | LPROPS_TAKEN, 0);
326*4882a593Smuzhiyun 	if (IS_ERR(lp)) {
327*4882a593Smuzhiyun 		err = PTR_ERR(lp);
328*4882a593Smuzhiyun 		goto out;
329*4882a593Smuzhiyun 	}
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	memcpy(ret_lp, lp, sizeof(struct ubifs_lprops));
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun out:
334*4882a593Smuzhiyun 	ubifs_release_lprops(c);
335*4882a593Smuzhiyun 	return err;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun /**
339*4882a593Smuzhiyun  * scan_for_free_cb - free space scan callback.
340*4882a593Smuzhiyun  * @c: the UBIFS file-system description object
341*4882a593Smuzhiyun  * @lprops: LEB properties to scan
342*4882a593Smuzhiyun  * @in_tree: whether the LEB properties are in main memory
343*4882a593Smuzhiyun  * @data: information passed to and from the caller of the scan
344*4882a593Smuzhiyun  *
345*4882a593Smuzhiyun  * This function returns a code that indicates whether the scan should continue
346*4882a593Smuzhiyun  * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
347*4882a593Smuzhiyun  * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
348*4882a593Smuzhiyun  * (%LPT_SCAN_STOP).
349*4882a593Smuzhiyun  */
scan_for_free_cb(struct ubifs_info * c,const struct ubifs_lprops * lprops,int in_tree,struct scan_data * data)350*4882a593Smuzhiyun static int scan_for_free_cb(struct ubifs_info *c,
351*4882a593Smuzhiyun 			    const struct ubifs_lprops *lprops, int in_tree,
352*4882a593Smuzhiyun 			    struct scan_data *data)
353*4882a593Smuzhiyun {
354*4882a593Smuzhiyun 	int ret = LPT_SCAN_CONTINUE;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	/* Exclude LEBs that are currently in use */
357*4882a593Smuzhiyun 	if (lprops->flags & LPROPS_TAKEN)
358*4882a593Smuzhiyun 		return LPT_SCAN_CONTINUE;
359*4882a593Smuzhiyun 	/* Determine whether to add these LEB properties to the tree */
360*4882a593Smuzhiyun 	if (!in_tree && valuable(c, lprops))
361*4882a593Smuzhiyun 		ret |= LPT_SCAN_ADD;
362*4882a593Smuzhiyun 	/* Exclude index LEBs */
363*4882a593Smuzhiyun 	if (lprops->flags & LPROPS_INDEX)
364*4882a593Smuzhiyun 		return ret;
365*4882a593Smuzhiyun 	/* Exclude LEBs with too little space */
366*4882a593Smuzhiyun 	if (lprops->free < data->min_space)
367*4882a593Smuzhiyun 		return ret;
368*4882a593Smuzhiyun 	/* If specified, exclude empty LEBs */
369*4882a593Smuzhiyun 	if (!data->pick_free && lprops->free == c->leb_size)
370*4882a593Smuzhiyun 		return ret;
371*4882a593Smuzhiyun 	/*
372*4882a593Smuzhiyun 	 * LEBs that have only free and dirty space must not be allocated
373*4882a593Smuzhiyun 	 * because they may have been unmapped already or they may have data
374*4882a593Smuzhiyun 	 * that is obsolete only because of nodes that are still sitting in a
375*4882a593Smuzhiyun 	 * wbuf.
376*4882a593Smuzhiyun 	 */
377*4882a593Smuzhiyun 	if (lprops->free + lprops->dirty == c->leb_size && lprops->dirty > 0)
378*4882a593Smuzhiyun 		return ret;
379*4882a593Smuzhiyun 	/* Finally we found space */
380*4882a593Smuzhiyun 	data->lnum = lprops->lnum;
381*4882a593Smuzhiyun 	return LPT_SCAN_ADD | LPT_SCAN_STOP;
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun /**
385*4882a593Smuzhiyun  * do_find_free_space - find a data LEB with free space.
386*4882a593Smuzhiyun  * @c: the UBIFS file-system description object
387*4882a593Smuzhiyun  * @min_space: minimum amount of free space required
388*4882a593Smuzhiyun  * @pick_free: whether it is OK to scan for empty LEBs
389*4882a593Smuzhiyun  * @squeeze: whether to try to find space in a non-empty LEB first
390*4882a593Smuzhiyun  *
391*4882a593Smuzhiyun  * This function returns a pointer to the LEB properties found or a negative
392*4882a593Smuzhiyun  * error code.
393*4882a593Smuzhiyun  */
394*4882a593Smuzhiyun static
do_find_free_space(struct ubifs_info * c,int min_space,int pick_free,int squeeze)395*4882a593Smuzhiyun const struct ubifs_lprops *do_find_free_space(struct ubifs_info *c,
396*4882a593Smuzhiyun 					      int min_space, int pick_free,
397*4882a593Smuzhiyun 					      int squeeze)
398*4882a593Smuzhiyun {
399*4882a593Smuzhiyun 	const struct ubifs_lprops *lprops;
400*4882a593Smuzhiyun 	struct ubifs_lpt_heap *heap;
401*4882a593Smuzhiyun 	struct scan_data data;
402*4882a593Smuzhiyun 	int err, i;
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	if (squeeze) {
405*4882a593Smuzhiyun 		lprops = ubifs_fast_find_free(c);
406*4882a593Smuzhiyun 		if (lprops && lprops->free >= min_space)
407*4882a593Smuzhiyun 			return lprops;
408*4882a593Smuzhiyun 	}
409*4882a593Smuzhiyun 	if (pick_free) {
410*4882a593Smuzhiyun 		lprops = ubifs_fast_find_empty(c);
411*4882a593Smuzhiyun 		if (lprops)
412*4882a593Smuzhiyun 			return lprops;
413*4882a593Smuzhiyun 	}
414*4882a593Smuzhiyun 	if (!squeeze) {
415*4882a593Smuzhiyun 		lprops = ubifs_fast_find_free(c);
416*4882a593Smuzhiyun 		if (lprops && lprops->free >= min_space)
417*4882a593Smuzhiyun 			return lprops;
418*4882a593Smuzhiyun 	}
419*4882a593Smuzhiyun 	/* There may be an LEB with enough free space on the dirty heap */
420*4882a593Smuzhiyun 	heap = &c->lpt_heap[LPROPS_DIRTY - 1];
421*4882a593Smuzhiyun 	for (i = 0; i < heap->cnt; i++) {
422*4882a593Smuzhiyun 		lprops = heap->arr[i];
423*4882a593Smuzhiyun 		if (lprops->free >= min_space)
424*4882a593Smuzhiyun 			return lprops;
425*4882a593Smuzhiyun 	}
426*4882a593Smuzhiyun 	/*
427*4882a593Smuzhiyun 	 * A LEB may have fallen off of the bottom of the free heap, and ended
428*4882a593Smuzhiyun 	 * up as uncategorized even though it has enough free space for us now,
429*4882a593Smuzhiyun 	 * so check the uncategorized list. N.B. neither empty nor freeable LEBs
430*4882a593Smuzhiyun 	 * can end up as uncategorized because they are kept on lists not
431*4882a593Smuzhiyun 	 * finite-sized heaps.
432*4882a593Smuzhiyun 	 */
433*4882a593Smuzhiyun 	list_for_each_entry(lprops, &c->uncat_list, list) {
434*4882a593Smuzhiyun 		if (lprops->flags & LPROPS_TAKEN)
435*4882a593Smuzhiyun 			continue;
436*4882a593Smuzhiyun 		if (lprops->flags & LPROPS_INDEX)
437*4882a593Smuzhiyun 			continue;
438*4882a593Smuzhiyun 		if (lprops->free >= min_space)
439*4882a593Smuzhiyun 			return lprops;
440*4882a593Smuzhiyun 	}
441*4882a593Smuzhiyun 	/* We have looked everywhere in main memory, now scan the flash */
442*4882a593Smuzhiyun 	if (c->pnodes_have >= c->pnode_cnt)
443*4882a593Smuzhiyun 		/* All pnodes are in memory, so skip scan */
444*4882a593Smuzhiyun 		return ERR_PTR(-ENOSPC);
445*4882a593Smuzhiyun 	data.min_space = min_space;
446*4882a593Smuzhiyun 	data.pick_free = pick_free;
447*4882a593Smuzhiyun 	data.lnum = -1;
448*4882a593Smuzhiyun 	err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
449*4882a593Smuzhiyun 				    (ubifs_lpt_scan_callback)scan_for_free_cb,
450*4882a593Smuzhiyun 				    &data);
451*4882a593Smuzhiyun 	if (err)
452*4882a593Smuzhiyun 		return ERR_PTR(err);
453*4882a593Smuzhiyun 	ubifs_assert(c, data.lnum >= c->main_first && data.lnum < c->leb_cnt);
454*4882a593Smuzhiyun 	c->lscan_lnum = data.lnum;
455*4882a593Smuzhiyun 	lprops = ubifs_lpt_lookup_dirty(c, data.lnum);
456*4882a593Smuzhiyun 	if (IS_ERR(lprops))
457*4882a593Smuzhiyun 		return lprops;
458*4882a593Smuzhiyun 	ubifs_assert(c, lprops->lnum == data.lnum);
459*4882a593Smuzhiyun 	ubifs_assert(c, lprops->free >= min_space);
460*4882a593Smuzhiyun 	ubifs_assert(c, !(lprops->flags & LPROPS_TAKEN));
461*4882a593Smuzhiyun 	ubifs_assert(c, !(lprops->flags & LPROPS_INDEX));
462*4882a593Smuzhiyun 	return lprops;
463*4882a593Smuzhiyun }
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun /**
466*4882a593Smuzhiyun  * ubifs_find_free_space - find a data LEB with free space.
467*4882a593Smuzhiyun  * @c: the UBIFS file-system description object
468*4882a593Smuzhiyun  * @min_space: minimum amount of required free space
469*4882a593Smuzhiyun  * @offs: contains offset of where free space starts on exit
470*4882a593Smuzhiyun  * @squeeze: whether to try to find space in a non-empty LEB first
471*4882a593Smuzhiyun  *
472*4882a593Smuzhiyun  * This function looks for an LEB with at least @min_space bytes of free space.
473*4882a593Smuzhiyun  * It tries to find an empty LEB if possible. If no empty LEBs are available,
474*4882a593Smuzhiyun  * this function searches for a non-empty data LEB. The returned LEB is marked
475*4882a593Smuzhiyun  * as "taken".
476*4882a593Smuzhiyun  *
477*4882a593Smuzhiyun  * This function returns found LEB number in case of success, %-ENOSPC if it
478*4882a593Smuzhiyun  * failed to find a LEB with @min_space bytes of free space and other a negative
479*4882a593Smuzhiyun  * error codes in case of failure.
480*4882a593Smuzhiyun  */
ubifs_find_free_space(struct ubifs_info * c,int min_space,int * offs,int squeeze)481*4882a593Smuzhiyun int ubifs_find_free_space(struct ubifs_info *c, int min_space, int *offs,
482*4882a593Smuzhiyun 			  int squeeze)
483*4882a593Smuzhiyun {
484*4882a593Smuzhiyun 	const struct ubifs_lprops *lprops;
485*4882a593Smuzhiyun 	int lebs, rsvd_idx_lebs, pick_free = 0, err, lnum, flags;
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	dbg_find("min_space %d", min_space);
488*4882a593Smuzhiyun 	ubifs_get_lprops(c);
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	/* Check if there are enough empty LEBs for commit */
491*4882a593Smuzhiyun 	spin_lock(&c->space_lock);
492*4882a593Smuzhiyun 	if (c->bi.min_idx_lebs > c->lst.idx_lebs)
493*4882a593Smuzhiyun 		rsvd_idx_lebs = c->bi.min_idx_lebs -  c->lst.idx_lebs;
494*4882a593Smuzhiyun 	else
495*4882a593Smuzhiyun 		rsvd_idx_lebs = 0;
496*4882a593Smuzhiyun 	lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt -
497*4882a593Smuzhiyun 	       c->lst.taken_empty_lebs;
498*4882a593Smuzhiyun 	if (rsvd_idx_lebs < lebs)
499*4882a593Smuzhiyun 		/*
500*4882a593Smuzhiyun 		 * OK to allocate an empty LEB, but we still don't want to go
501*4882a593Smuzhiyun 		 * looking for one if there aren't any.
502*4882a593Smuzhiyun 		 */
503*4882a593Smuzhiyun 		if (c->lst.empty_lebs - c->lst.taken_empty_lebs > 0) {
504*4882a593Smuzhiyun 			pick_free = 1;
505*4882a593Smuzhiyun 			/*
506*4882a593Smuzhiyun 			 * Because we release the space lock, we must account
507*4882a593Smuzhiyun 			 * for this allocation here. After the LEB properties
508*4882a593Smuzhiyun 			 * flags have been updated, we subtract one. Note, the
509*4882a593Smuzhiyun 			 * result of this is that lprops also decreases
510*4882a593Smuzhiyun 			 * @taken_empty_lebs in 'ubifs_change_lp()', so it is
511*4882a593Smuzhiyun 			 * off by one for a short period of time which may
512*4882a593Smuzhiyun 			 * introduce a small disturbance to budgeting
513*4882a593Smuzhiyun 			 * calculations, but this is harmless because at the
514*4882a593Smuzhiyun 			 * worst case this would make the budgeting subsystem
515*4882a593Smuzhiyun 			 * be more pessimistic than needed.
516*4882a593Smuzhiyun 			 *
517*4882a593Smuzhiyun 			 * Fundamentally, this is about serialization of the
518*4882a593Smuzhiyun 			 * budgeting and lprops subsystems. We could make the
519*4882a593Smuzhiyun 			 * @space_lock a mutex and avoid dropping it before
520*4882a593Smuzhiyun 			 * calling 'ubifs_change_lp()', but mutex is more
521*4882a593Smuzhiyun 			 * heavy-weight, and we want budgeting to be as fast as
522*4882a593Smuzhiyun 			 * possible.
523*4882a593Smuzhiyun 			 */
524*4882a593Smuzhiyun 			c->lst.taken_empty_lebs += 1;
525*4882a593Smuzhiyun 		}
526*4882a593Smuzhiyun 	spin_unlock(&c->space_lock);
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	lprops = do_find_free_space(c, min_space, pick_free, squeeze);
529*4882a593Smuzhiyun 	if (IS_ERR(lprops)) {
530*4882a593Smuzhiyun 		err = PTR_ERR(lprops);
531*4882a593Smuzhiyun 		goto out;
532*4882a593Smuzhiyun 	}
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	lnum = lprops->lnum;
535*4882a593Smuzhiyun 	flags = lprops->flags | LPROPS_TAKEN;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	lprops = ubifs_change_lp(c, lprops, LPROPS_NC, LPROPS_NC, flags, 0);
538*4882a593Smuzhiyun 	if (IS_ERR(lprops)) {
539*4882a593Smuzhiyun 		err = PTR_ERR(lprops);
540*4882a593Smuzhiyun 		goto out;
541*4882a593Smuzhiyun 	}
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	if (pick_free) {
544*4882a593Smuzhiyun 		spin_lock(&c->space_lock);
545*4882a593Smuzhiyun 		c->lst.taken_empty_lebs -= 1;
546*4882a593Smuzhiyun 		spin_unlock(&c->space_lock);
547*4882a593Smuzhiyun 	}
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	*offs = c->leb_size - lprops->free;
550*4882a593Smuzhiyun 	ubifs_release_lprops(c);
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	if (*offs == 0) {
553*4882a593Smuzhiyun 		/*
554*4882a593Smuzhiyun 		 * Ensure that empty LEBs have been unmapped. They may not have
555*4882a593Smuzhiyun 		 * been, for example, because of an unclean unmount.  Also
556*4882a593Smuzhiyun 		 * LEBs that were freeable LEBs (free + dirty == leb_size) will
557*4882a593Smuzhiyun 		 * not have been unmapped.
558*4882a593Smuzhiyun 		 */
559*4882a593Smuzhiyun 		err = ubifs_leb_unmap(c, lnum);
560*4882a593Smuzhiyun 		if (err)
561*4882a593Smuzhiyun 			return err;
562*4882a593Smuzhiyun 	}
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 	dbg_find("found LEB %d, free %d", lnum, c->leb_size - *offs);
565*4882a593Smuzhiyun 	ubifs_assert(c, *offs <= c->leb_size - min_space);
566*4882a593Smuzhiyun 	return lnum;
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun out:
569*4882a593Smuzhiyun 	if (pick_free) {
570*4882a593Smuzhiyun 		spin_lock(&c->space_lock);
571*4882a593Smuzhiyun 		c->lst.taken_empty_lebs -= 1;
572*4882a593Smuzhiyun 		spin_unlock(&c->space_lock);
573*4882a593Smuzhiyun 	}
574*4882a593Smuzhiyun 	ubifs_release_lprops(c);
575*4882a593Smuzhiyun 	return err;
576*4882a593Smuzhiyun }
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun /**
579*4882a593Smuzhiyun  * scan_for_idx_cb - callback used by the scan for a free LEB for the index.
580*4882a593Smuzhiyun  * @c: the UBIFS file-system description object
581*4882a593Smuzhiyun  * @lprops: LEB properties to scan
582*4882a593Smuzhiyun  * @in_tree: whether the LEB properties are in main memory
583*4882a593Smuzhiyun  * @data: information passed to and from the caller of the scan
584*4882a593Smuzhiyun  *
585*4882a593Smuzhiyun  * This function returns a code that indicates whether the scan should continue
586*4882a593Smuzhiyun  * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
587*4882a593Smuzhiyun  * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
588*4882a593Smuzhiyun  * (%LPT_SCAN_STOP).
589*4882a593Smuzhiyun  */
scan_for_idx_cb(struct ubifs_info * c,const struct ubifs_lprops * lprops,int in_tree,struct scan_data * data)590*4882a593Smuzhiyun static int scan_for_idx_cb(struct ubifs_info *c,
591*4882a593Smuzhiyun 			   const struct ubifs_lprops *lprops, int in_tree,
592*4882a593Smuzhiyun 			   struct scan_data *data)
593*4882a593Smuzhiyun {
594*4882a593Smuzhiyun 	int ret = LPT_SCAN_CONTINUE;
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	/* Exclude LEBs that are currently in use */
597*4882a593Smuzhiyun 	if (lprops->flags & LPROPS_TAKEN)
598*4882a593Smuzhiyun 		return LPT_SCAN_CONTINUE;
599*4882a593Smuzhiyun 	/* Determine whether to add these LEB properties to the tree */
600*4882a593Smuzhiyun 	if (!in_tree && valuable(c, lprops))
601*4882a593Smuzhiyun 		ret |= LPT_SCAN_ADD;
602*4882a593Smuzhiyun 	/* Exclude index LEBS */
603*4882a593Smuzhiyun 	if (lprops->flags & LPROPS_INDEX)
604*4882a593Smuzhiyun 		return ret;
605*4882a593Smuzhiyun 	/* Exclude LEBs that cannot be made empty */
606*4882a593Smuzhiyun 	if (lprops->free + lprops->dirty != c->leb_size)
607*4882a593Smuzhiyun 		return ret;
608*4882a593Smuzhiyun 	/*
609*4882a593Smuzhiyun 	 * We are allocating for the index so it is safe to allocate LEBs with
610*4882a593Smuzhiyun 	 * only free and dirty space, because write buffers are sync'd at commit
611*4882a593Smuzhiyun 	 * start.
612*4882a593Smuzhiyun 	 */
613*4882a593Smuzhiyun 	data->lnum = lprops->lnum;
614*4882a593Smuzhiyun 	return LPT_SCAN_ADD | LPT_SCAN_STOP;
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun /**
618*4882a593Smuzhiyun  * scan_for_leb_for_idx - scan for a free LEB for the index.
619*4882a593Smuzhiyun  * @c: the UBIFS file-system description object
620*4882a593Smuzhiyun  */
scan_for_leb_for_idx(struct ubifs_info * c)621*4882a593Smuzhiyun static const struct ubifs_lprops *scan_for_leb_for_idx(struct ubifs_info *c)
622*4882a593Smuzhiyun {
623*4882a593Smuzhiyun 	const struct ubifs_lprops *lprops;
624*4882a593Smuzhiyun 	struct scan_data data;
625*4882a593Smuzhiyun 	int err;
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	data.lnum = -1;
628*4882a593Smuzhiyun 	err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
629*4882a593Smuzhiyun 				    (ubifs_lpt_scan_callback)scan_for_idx_cb,
630*4882a593Smuzhiyun 				    &data);
631*4882a593Smuzhiyun 	if (err)
632*4882a593Smuzhiyun 		return ERR_PTR(err);
633*4882a593Smuzhiyun 	ubifs_assert(c, data.lnum >= c->main_first && data.lnum < c->leb_cnt);
634*4882a593Smuzhiyun 	c->lscan_lnum = data.lnum;
635*4882a593Smuzhiyun 	lprops = ubifs_lpt_lookup_dirty(c, data.lnum);
636*4882a593Smuzhiyun 	if (IS_ERR(lprops))
637*4882a593Smuzhiyun 		return lprops;
638*4882a593Smuzhiyun 	ubifs_assert(c, lprops->lnum == data.lnum);
639*4882a593Smuzhiyun 	ubifs_assert(c, lprops->free + lprops->dirty == c->leb_size);
640*4882a593Smuzhiyun 	ubifs_assert(c, !(lprops->flags & LPROPS_TAKEN));
641*4882a593Smuzhiyun 	ubifs_assert(c, !(lprops->flags & LPROPS_INDEX));
642*4882a593Smuzhiyun 	return lprops;
643*4882a593Smuzhiyun }
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun /**
646*4882a593Smuzhiyun  * ubifs_find_free_leb_for_idx - find a free LEB for the index.
647*4882a593Smuzhiyun  * @c: the UBIFS file-system description object
648*4882a593Smuzhiyun  *
649*4882a593Smuzhiyun  * This function looks for a free LEB and returns that LEB number. The returned
650*4882a593Smuzhiyun  * LEB is marked as "taken", "index".
651*4882a593Smuzhiyun  *
652*4882a593Smuzhiyun  * Only empty LEBs are allocated. This is for two reasons. First, the commit
653*4882a593Smuzhiyun  * calculates the number of LEBs to allocate based on the assumption that they
654*4882a593Smuzhiyun  * will be empty. Secondly, free space at the end of an index LEB is not
655*4882a593Smuzhiyun  * guaranteed to be empty because it may have been used by the in-the-gaps
656*4882a593Smuzhiyun  * method prior to an unclean unmount.
657*4882a593Smuzhiyun  *
658*4882a593Smuzhiyun  * If no LEB is found %-ENOSPC is returned. For other failures another negative
659*4882a593Smuzhiyun  * error code is returned.
660*4882a593Smuzhiyun  */
ubifs_find_free_leb_for_idx(struct ubifs_info * c)661*4882a593Smuzhiyun int ubifs_find_free_leb_for_idx(struct ubifs_info *c)
662*4882a593Smuzhiyun {
663*4882a593Smuzhiyun 	const struct ubifs_lprops *lprops;
664*4882a593Smuzhiyun 	int lnum = -1, err, flags;
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 	ubifs_get_lprops(c);
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	lprops = ubifs_fast_find_empty(c);
669*4882a593Smuzhiyun 	if (!lprops) {
670*4882a593Smuzhiyun 		lprops = ubifs_fast_find_freeable(c);
671*4882a593Smuzhiyun 		if (!lprops) {
672*4882a593Smuzhiyun 			/*
673*4882a593Smuzhiyun 			 * The first condition means the following: go scan the
674*4882a593Smuzhiyun 			 * LPT if there are uncategorized lprops, which means
675*4882a593Smuzhiyun 			 * there may be freeable LEBs there (UBIFS does not
676*4882a593Smuzhiyun 			 * store the information about freeable LEBs in the
677*4882a593Smuzhiyun 			 * master node).
678*4882a593Smuzhiyun 			 */
679*4882a593Smuzhiyun 			if (c->in_a_category_cnt != c->main_lebs ||
680*4882a593Smuzhiyun 			    c->lst.empty_lebs - c->lst.taken_empty_lebs > 0) {
681*4882a593Smuzhiyun 				ubifs_assert(c, c->freeable_cnt == 0);
682*4882a593Smuzhiyun 				lprops = scan_for_leb_for_idx(c);
683*4882a593Smuzhiyun 				if (IS_ERR(lprops)) {
684*4882a593Smuzhiyun 					err = PTR_ERR(lprops);
685*4882a593Smuzhiyun 					goto out;
686*4882a593Smuzhiyun 				}
687*4882a593Smuzhiyun 			}
688*4882a593Smuzhiyun 		}
689*4882a593Smuzhiyun 	}
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	if (!lprops) {
692*4882a593Smuzhiyun 		err = -ENOSPC;
693*4882a593Smuzhiyun 		goto out;
694*4882a593Smuzhiyun 	}
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	lnum = lprops->lnum;
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	dbg_find("found LEB %d, free %d, dirty %d, flags %#x",
699*4882a593Smuzhiyun 		 lnum, lprops->free, lprops->dirty, lprops->flags);
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 	flags = lprops->flags | LPROPS_TAKEN | LPROPS_INDEX;
702*4882a593Smuzhiyun 	lprops = ubifs_change_lp(c, lprops, c->leb_size, 0, flags, 0);
703*4882a593Smuzhiyun 	if (IS_ERR(lprops)) {
704*4882a593Smuzhiyun 		err = PTR_ERR(lprops);
705*4882a593Smuzhiyun 		goto out;
706*4882a593Smuzhiyun 	}
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun 	ubifs_release_lprops(c);
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 	/*
711*4882a593Smuzhiyun 	 * Ensure that empty LEBs have been unmapped. They may not have been,
712*4882a593Smuzhiyun 	 * for example, because of an unclean unmount. Also LEBs that were
713*4882a593Smuzhiyun 	 * freeable LEBs (free + dirty == leb_size) will not have been unmapped.
714*4882a593Smuzhiyun 	 */
715*4882a593Smuzhiyun 	err = ubifs_leb_unmap(c, lnum);
716*4882a593Smuzhiyun 	if (err) {
717*4882a593Smuzhiyun 		ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
718*4882a593Smuzhiyun 				    LPROPS_TAKEN | LPROPS_INDEX, 0);
719*4882a593Smuzhiyun 		return err;
720*4882a593Smuzhiyun 	}
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	return lnum;
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun out:
725*4882a593Smuzhiyun 	ubifs_release_lprops(c);
726*4882a593Smuzhiyun 	return err;
727*4882a593Smuzhiyun }
728*4882a593Smuzhiyun 
cmp_dirty_idx(const struct ubifs_lprops ** a,const struct ubifs_lprops ** b)729*4882a593Smuzhiyun static int cmp_dirty_idx(const struct ubifs_lprops **a,
730*4882a593Smuzhiyun 			 const struct ubifs_lprops **b)
731*4882a593Smuzhiyun {
732*4882a593Smuzhiyun 	const struct ubifs_lprops *lpa = *a;
733*4882a593Smuzhiyun 	const struct ubifs_lprops *lpb = *b;
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	return lpa->dirty + lpa->free - lpb->dirty - lpb->free;
736*4882a593Smuzhiyun }
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun /**
739*4882a593Smuzhiyun  * ubifs_save_dirty_idx_lnums - save an array of the most dirty index LEB nos.
740*4882a593Smuzhiyun  * @c: the UBIFS file-system description object
741*4882a593Smuzhiyun  *
742*4882a593Smuzhiyun  * This function is called each commit to create an array of LEB numbers of
743*4882a593Smuzhiyun  * dirty index LEBs sorted in order of dirty and free space.  This is used by
744*4882a593Smuzhiyun  * the in-the-gaps method of TNC commit.
745*4882a593Smuzhiyun  */
ubifs_save_dirty_idx_lnums(struct ubifs_info * c)746*4882a593Smuzhiyun int ubifs_save_dirty_idx_lnums(struct ubifs_info *c)
747*4882a593Smuzhiyun {
748*4882a593Smuzhiyun 	int i;
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	ubifs_get_lprops(c);
751*4882a593Smuzhiyun 	/* Copy the LPROPS_DIRTY_IDX heap */
752*4882a593Smuzhiyun 	c->dirty_idx.cnt = c->lpt_heap[LPROPS_DIRTY_IDX - 1].cnt;
753*4882a593Smuzhiyun 	memcpy(c->dirty_idx.arr, c->lpt_heap[LPROPS_DIRTY_IDX - 1].arr,
754*4882a593Smuzhiyun 	       sizeof(void *) * c->dirty_idx.cnt);
755*4882a593Smuzhiyun 	/* Sort it so that the dirtiest is now at the end */
756*4882a593Smuzhiyun 	sort(c->dirty_idx.arr, c->dirty_idx.cnt, sizeof(void *),
757*4882a593Smuzhiyun 	     (int (*)(const void *, const void *))cmp_dirty_idx, NULL);
758*4882a593Smuzhiyun 	dbg_find("found %d dirty index LEBs", c->dirty_idx.cnt);
759*4882a593Smuzhiyun 	if (c->dirty_idx.cnt)
760*4882a593Smuzhiyun 		dbg_find("dirtiest index LEB is %d with dirty %d and free %d",
761*4882a593Smuzhiyun 			 c->dirty_idx.arr[c->dirty_idx.cnt - 1]->lnum,
762*4882a593Smuzhiyun 			 c->dirty_idx.arr[c->dirty_idx.cnt - 1]->dirty,
763*4882a593Smuzhiyun 			 c->dirty_idx.arr[c->dirty_idx.cnt - 1]->free);
764*4882a593Smuzhiyun 	/* Replace the lprops pointers with LEB numbers */
765*4882a593Smuzhiyun 	for (i = 0; i < c->dirty_idx.cnt; i++)
766*4882a593Smuzhiyun 		c->dirty_idx.arr[i] = (void *)(size_t)c->dirty_idx.arr[i]->lnum;
767*4882a593Smuzhiyun 	ubifs_release_lprops(c);
768*4882a593Smuzhiyun 	return 0;
769*4882a593Smuzhiyun }
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun /**
772*4882a593Smuzhiyun  * scan_dirty_idx_cb - callback used by the scan for a dirty index LEB.
773*4882a593Smuzhiyun  * @c: the UBIFS file-system description object
774*4882a593Smuzhiyun  * @lprops: LEB properties to scan
775*4882a593Smuzhiyun  * @in_tree: whether the LEB properties are in main memory
776*4882a593Smuzhiyun  * @data: information passed to and from the caller of the scan
777*4882a593Smuzhiyun  *
778*4882a593Smuzhiyun  * This function returns a code that indicates whether the scan should continue
779*4882a593Smuzhiyun  * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
780*4882a593Smuzhiyun  * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
781*4882a593Smuzhiyun  * (%LPT_SCAN_STOP).
782*4882a593Smuzhiyun  */
scan_dirty_idx_cb(struct ubifs_info * c,const struct ubifs_lprops * lprops,int in_tree,struct scan_data * data)783*4882a593Smuzhiyun static int scan_dirty_idx_cb(struct ubifs_info *c,
784*4882a593Smuzhiyun 			   const struct ubifs_lprops *lprops, int in_tree,
785*4882a593Smuzhiyun 			   struct scan_data *data)
786*4882a593Smuzhiyun {
787*4882a593Smuzhiyun 	int ret = LPT_SCAN_CONTINUE;
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	/* Exclude LEBs that are currently in use */
790*4882a593Smuzhiyun 	if (lprops->flags & LPROPS_TAKEN)
791*4882a593Smuzhiyun 		return LPT_SCAN_CONTINUE;
792*4882a593Smuzhiyun 	/* Determine whether to add these LEB properties to the tree */
793*4882a593Smuzhiyun 	if (!in_tree && valuable(c, lprops))
794*4882a593Smuzhiyun 		ret |= LPT_SCAN_ADD;
795*4882a593Smuzhiyun 	/* Exclude non-index LEBs */
796*4882a593Smuzhiyun 	if (!(lprops->flags & LPROPS_INDEX))
797*4882a593Smuzhiyun 		return ret;
798*4882a593Smuzhiyun 	/* Exclude LEBs with too little space */
799*4882a593Smuzhiyun 	if (lprops->free + lprops->dirty < c->min_idx_node_sz)
800*4882a593Smuzhiyun 		return ret;
801*4882a593Smuzhiyun 	/* Finally we found space */
802*4882a593Smuzhiyun 	data->lnum = lprops->lnum;
803*4882a593Smuzhiyun 	return LPT_SCAN_ADD | LPT_SCAN_STOP;
804*4882a593Smuzhiyun }
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun /**
807*4882a593Smuzhiyun  * find_dirty_idx_leb - find a dirty index LEB.
808*4882a593Smuzhiyun  * @c: the UBIFS file-system description object
809*4882a593Smuzhiyun  *
810*4882a593Smuzhiyun  * This function returns LEB number upon success and a negative error code upon
811*4882a593Smuzhiyun  * failure.  In particular, -ENOSPC is returned if a dirty index LEB is not
812*4882a593Smuzhiyun  * found.
813*4882a593Smuzhiyun  *
814*4882a593Smuzhiyun  * Note that this function scans the entire LPT but it is called very rarely.
815*4882a593Smuzhiyun  */
find_dirty_idx_leb(struct ubifs_info * c)816*4882a593Smuzhiyun static int find_dirty_idx_leb(struct ubifs_info *c)
817*4882a593Smuzhiyun {
818*4882a593Smuzhiyun 	const struct ubifs_lprops *lprops;
819*4882a593Smuzhiyun 	struct ubifs_lpt_heap *heap;
820*4882a593Smuzhiyun 	struct scan_data data;
821*4882a593Smuzhiyun 	int err, i, ret;
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 	/* Check all structures in memory first */
824*4882a593Smuzhiyun 	data.lnum = -1;
825*4882a593Smuzhiyun 	heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
826*4882a593Smuzhiyun 	for (i = 0; i < heap->cnt; i++) {
827*4882a593Smuzhiyun 		lprops = heap->arr[i];
828*4882a593Smuzhiyun 		ret = scan_dirty_idx_cb(c, lprops, 1, &data);
829*4882a593Smuzhiyun 		if (ret & LPT_SCAN_STOP)
830*4882a593Smuzhiyun 			goto found;
831*4882a593Smuzhiyun 	}
832*4882a593Smuzhiyun 	list_for_each_entry(lprops, &c->frdi_idx_list, list) {
833*4882a593Smuzhiyun 		ret = scan_dirty_idx_cb(c, lprops, 1, &data);
834*4882a593Smuzhiyun 		if (ret & LPT_SCAN_STOP)
835*4882a593Smuzhiyun 			goto found;
836*4882a593Smuzhiyun 	}
837*4882a593Smuzhiyun 	list_for_each_entry(lprops, &c->uncat_list, list) {
838*4882a593Smuzhiyun 		ret = scan_dirty_idx_cb(c, lprops, 1, &data);
839*4882a593Smuzhiyun 		if (ret & LPT_SCAN_STOP)
840*4882a593Smuzhiyun 			goto found;
841*4882a593Smuzhiyun 	}
842*4882a593Smuzhiyun 	if (c->pnodes_have >= c->pnode_cnt)
843*4882a593Smuzhiyun 		/* All pnodes are in memory, so skip scan */
844*4882a593Smuzhiyun 		return -ENOSPC;
845*4882a593Smuzhiyun 	err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
846*4882a593Smuzhiyun 				    (ubifs_lpt_scan_callback)scan_dirty_idx_cb,
847*4882a593Smuzhiyun 				    &data);
848*4882a593Smuzhiyun 	if (err)
849*4882a593Smuzhiyun 		return err;
850*4882a593Smuzhiyun found:
851*4882a593Smuzhiyun 	ubifs_assert(c, data.lnum >= c->main_first && data.lnum < c->leb_cnt);
852*4882a593Smuzhiyun 	c->lscan_lnum = data.lnum;
853*4882a593Smuzhiyun 	lprops = ubifs_lpt_lookup_dirty(c, data.lnum);
854*4882a593Smuzhiyun 	if (IS_ERR(lprops))
855*4882a593Smuzhiyun 		return PTR_ERR(lprops);
856*4882a593Smuzhiyun 	ubifs_assert(c, lprops->lnum == data.lnum);
857*4882a593Smuzhiyun 	ubifs_assert(c, lprops->free + lprops->dirty >= c->min_idx_node_sz);
858*4882a593Smuzhiyun 	ubifs_assert(c, !(lprops->flags & LPROPS_TAKEN));
859*4882a593Smuzhiyun 	ubifs_assert(c, (lprops->flags & LPROPS_INDEX));
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 	dbg_find("found dirty LEB %d, free %d, dirty %d, flags %#x",
862*4882a593Smuzhiyun 		 lprops->lnum, lprops->free, lprops->dirty, lprops->flags);
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 	lprops = ubifs_change_lp(c, lprops, LPROPS_NC, LPROPS_NC,
865*4882a593Smuzhiyun 				 lprops->flags | LPROPS_TAKEN, 0);
866*4882a593Smuzhiyun 	if (IS_ERR(lprops))
867*4882a593Smuzhiyun 		return PTR_ERR(lprops);
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 	return lprops->lnum;
870*4882a593Smuzhiyun }
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun /**
873*4882a593Smuzhiyun  * get_idx_gc_leb - try to get a LEB number from trivial GC.
874*4882a593Smuzhiyun  * @c: the UBIFS file-system description object
875*4882a593Smuzhiyun  */
get_idx_gc_leb(struct ubifs_info * c)876*4882a593Smuzhiyun static int get_idx_gc_leb(struct ubifs_info *c)
877*4882a593Smuzhiyun {
878*4882a593Smuzhiyun 	const struct ubifs_lprops *lp;
879*4882a593Smuzhiyun 	int err, lnum;
880*4882a593Smuzhiyun 
881*4882a593Smuzhiyun 	err = ubifs_get_idx_gc_leb(c);
882*4882a593Smuzhiyun 	if (err < 0)
883*4882a593Smuzhiyun 		return err;
884*4882a593Smuzhiyun 	lnum = err;
885*4882a593Smuzhiyun 	/*
886*4882a593Smuzhiyun 	 * The LEB was due to be unmapped after the commit but
887*4882a593Smuzhiyun 	 * it is needed now for this commit.
888*4882a593Smuzhiyun 	 */
889*4882a593Smuzhiyun 	lp = ubifs_lpt_lookup_dirty(c, lnum);
890*4882a593Smuzhiyun 	if (IS_ERR(lp))
891*4882a593Smuzhiyun 		return PTR_ERR(lp);
892*4882a593Smuzhiyun 	lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
893*4882a593Smuzhiyun 			     lp->flags | LPROPS_INDEX, -1);
894*4882a593Smuzhiyun 	if (IS_ERR(lp))
895*4882a593Smuzhiyun 		return PTR_ERR(lp);
896*4882a593Smuzhiyun 	dbg_find("LEB %d, dirty %d and free %d flags %#x",
897*4882a593Smuzhiyun 		 lp->lnum, lp->dirty, lp->free, lp->flags);
898*4882a593Smuzhiyun 	return lnum;
899*4882a593Smuzhiyun }
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun /**
902*4882a593Smuzhiyun  * find_dirtiest_idx_leb - find dirtiest index LEB from dirtiest array.
903*4882a593Smuzhiyun  * @c: the UBIFS file-system description object
904*4882a593Smuzhiyun  */
find_dirtiest_idx_leb(struct ubifs_info * c)905*4882a593Smuzhiyun static int find_dirtiest_idx_leb(struct ubifs_info *c)
906*4882a593Smuzhiyun {
907*4882a593Smuzhiyun 	const struct ubifs_lprops *lp;
908*4882a593Smuzhiyun 	int lnum;
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun 	while (1) {
911*4882a593Smuzhiyun 		if (!c->dirty_idx.cnt)
912*4882a593Smuzhiyun 			return -ENOSPC;
913*4882a593Smuzhiyun 		/* The lprops pointers were replaced by LEB numbers */
914*4882a593Smuzhiyun 		lnum = (size_t)c->dirty_idx.arr[--c->dirty_idx.cnt];
915*4882a593Smuzhiyun 		lp = ubifs_lpt_lookup(c, lnum);
916*4882a593Smuzhiyun 		if (IS_ERR(lp))
917*4882a593Smuzhiyun 			return PTR_ERR(lp);
918*4882a593Smuzhiyun 		if ((lp->flags & LPROPS_TAKEN) || !(lp->flags & LPROPS_INDEX))
919*4882a593Smuzhiyun 			continue;
920*4882a593Smuzhiyun 		lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
921*4882a593Smuzhiyun 				     lp->flags | LPROPS_TAKEN, 0);
922*4882a593Smuzhiyun 		if (IS_ERR(lp))
923*4882a593Smuzhiyun 			return PTR_ERR(lp);
924*4882a593Smuzhiyun 		break;
925*4882a593Smuzhiyun 	}
926*4882a593Smuzhiyun 	dbg_find("LEB %d, dirty %d and free %d flags %#x", lp->lnum, lp->dirty,
927*4882a593Smuzhiyun 		 lp->free, lp->flags);
928*4882a593Smuzhiyun 	ubifs_assert(c, lp->flags & LPROPS_TAKEN);
929*4882a593Smuzhiyun 	ubifs_assert(c, lp->flags & LPROPS_INDEX);
930*4882a593Smuzhiyun 	return lnum;
931*4882a593Smuzhiyun }
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun /**
934*4882a593Smuzhiyun  * ubifs_find_dirty_idx_leb - try to find dirtiest index LEB as at last commit.
935*4882a593Smuzhiyun  * @c: the UBIFS file-system description object
936*4882a593Smuzhiyun  *
937*4882a593Smuzhiyun  * This function attempts to find an untaken index LEB with the most free and
938*4882a593Smuzhiyun  * dirty space that can be used without overwriting index nodes that were in the
939*4882a593Smuzhiyun  * last index committed.
940*4882a593Smuzhiyun  */
ubifs_find_dirty_idx_leb(struct ubifs_info * c)941*4882a593Smuzhiyun int ubifs_find_dirty_idx_leb(struct ubifs_info *c)
942*4882a593Smuzhiyun {
943*4882a593Smuzhiyun 	int err;
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun 	ubifs_get_lprops(c);
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun 	/*
948*4882a593Smuzhiyun 	 * We made an array of the dirtiest index LEB numbers as at the start of
949*4882a593Smuzhiyun 	 * last commit.  Try that array first.
950*4882a593Smuzhiyun 	 */
951*4882a593Smuzhiyun 	err = find_dirtiest_idx_leb(c);
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun 	/* Next try scanning the entire LPT */
954*4882a593Smuzhiyun 	if (err == -ENOSPC)
955*4882a593Smuzhiyun 		err = find_dirty_idx_leb(c);
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun 	/* Finally take any index LEBs awaiting trivial GC */
958*4882a593Smuzhiyun 	if (err == -ENOSPC)
959*4882a593Smuzhiyun 		err = get_idx_gc_leb(c);
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun 	ubifs_release_lprops(c);
962*4882a593Smuzhiyun 	return err;
963*4882a593Smuzhiyun }
964