xref: /kernel/linux/linux-5.10/fs/ubifs/find.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * This file is part of UBIFS.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2006-2008 Nokia Corporation.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Authors: Artem Bityutskiy (Битюцкий Артём)
88c2ecf20Sopenharmony_ci *          Adrian Hunter
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/*
128c2ecf20Sopenharmony_ci * This file contains functions for finding LEBs for various purposes e.g.
138c2ecf20Sopenharmony_ci * garbage collection. In general, lprops category heaps and lists are used
148c2ecf20Sopenharmony_ci * for fast access, falling back on scanning the LPT as a last resort.
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <linux/sort.h>
188c2ecf20Sopenharmony_ci#include "ubifs.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/**
218c2ecf20Sopenharmony_ci * struct scan_data - data provided to scan callback functions
228c2ecf20Sopenharmony_ci * @min_space: minimum number of bytes for which to scan
238c2ecf20Sopenharmony_ci * @pick_free: whether it is OK to scan for empty LEBs
248c2ecf20Sopenharmony_ci * @lnum: LEB number found is returned here
258c2ecf20Sopenharmony_ci * @exclude_index: whether to exclude index LEBs
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_cistruct scan_data {
288c2ecf20Sopenharmony_ci	int min_space;
298c2ecf20Sopenharmony_ci	int pick_free;
308c2ecf20Sopenharmony_ci	int lnum;
318c2ecf20Sopenharmony_ci	int exclude_index;
328c2ecf20Sopenharmony_ci};
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/**
358c2ecf20Sopenharmony_ci * valuable - determine whether LEB properties are valuable.
368c2ecf20Sopenharmony_ci * @c: the UBIFS file-system description object
378c2ecf20Sopenharmony_ci * @lprops: LEB properties
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci * This function return %1 if the LEB properties should be added to the LEB
408c2ecf20Sopenharmony_ci * properties tree in memory. Otherwise %0 is returned.
418c2ecf20Sopenharmony_ci */
428c2ecf20Sopenharmony_cistatic int valuable(struct ubifs_info *c, const struct ubifs_lprops *lprops)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	int n, cat = lprops->flags & LPROPS_CAT_MASK;
458c2ecf20Sopenharmony_ci	struct ubifs_lpt_heap *heap;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	switch (cat) {
488c2ecf20Sopenharmony_ci	case LPROPS_DIRTY:
498c2ecf20Sopenharmony_ci	case LPROPS_DIRTY_IDX:
508c2ecf20Sopenharmony_ci	case LPROPS_FREE:
518c2ecf20Sopenharmony_ci		heap = &c->lpt_heap[cat - 1];
528c2ecf20Sopenharmony_ci		if (heap->cnt < heap->max_cnt)
538c2ecf20Sopenharmony_ci			return 1;
548c2ecf20Sopenharmony_ci		if (lprops->free + lprops->dirty >= c->dark_wm)
558c2ecf20Sopenharmony_ci			return 1;
568c2ecf20Sopenharmony_ci		return 0;
578c2ecf20Sopenharmony_ci	case LPROPS_EMPTY:
588c2ecf20Sopenharmony_ci		n = c->lst.empty_lebs + c->freeable_cnt -
598c2ecf20Sopenharmony_ci		    c->lst.taken_empty_lebs;
608c2ecf20Sopenharmony_ci		if (n < c->lsave_cnt)
618c2ecf20Sopenharmony_ci			return 1;
628c2ecf20Sopenharmony_ci		return 0;
638c2ecf20Sopenharmony_ci	case LPROPS_FREEABLE:
648c2ecf20Sopenharmony_ci		return 1;
658c2ecf20Sopenharmony_ci	case LPROPS_FRDI_IDX:
668c2ecf20Sopenharmony_ci		return 1;
678c2ecf20Sopenharmony_ci	}
688c2ecf20Sopenharmony_ci	return 0;
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/**
728c2ecf20Sopenharmony_ci * scan_for_dirty_cb - dirty space scan callback.
738c2ecf20Sopenharmony_ci * @c: the UBIFS file-system description object
748c2ecf20Sopenharmony_ci * @lprops: LEB properties to scan
758c2ecf20Sopenharmony_ci * @in_tree: whether the LEB properties are in main memory
768c2ecf20Sopenharmony_ci * @data: information passed to and from the caller of the scan
778c2ecf20Sopenharmony_ci *
788c2ecf20Sopenharmony_ci * This function returns a code that indicates whether the scan should continue
798c2ecf20Sopenharmony_ci * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
808c2ecf20Sopenharmony_ci * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
818c2ecf20Sopenharmony_ci * (%LPT_SCAN_STOP).
828c2ecf20Sopenharmony_ci */
838c2ecf20Sopenharmony_cistatic int scan_for_dirty_cb(struct ubifs_info *c,
848c2ecf20Sopenharmony_ci			     const struct ubifs_lprops *lprops, int in_tree,
858c2ecf20Sopenharmony_ci			     struct scan_data *data)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	int ret = LPT_SCAN_CONTINUE;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	/* Exclude LEBs that are currently in use */
908c2ecf20Sopenharmony_ci	if (lprops->flags & LPROPS_TAKEN)
918c2ecf20Sopenharmony_ci		return LPT_SCAN_CONTINUE;
928c2ecf20Sopenharmony_ci	/* Determine whether to add these LEB properties to the tree */
938c2ecf20Sopenharmony_ci	if (!in_tree && valuable(c, lprops))
948c2ecf20Sopenharmony_ci		ret |= LPT_SCAN_ADD;
958c2ecf20Sopenharmony_ci	/* Exclude LEBs with too little space */
968c2ecf20Sopenharmony_ci	if (lprops->free + lprops->dirty < data->min_space)
978c2ecf20Sopenharmony_ci		return ret;
988c2ecf20Sopenharmony_ci	/* If specified, exclude index LEBs */
998c2ecf20Sopenharmony_ci	if (data->exclude_index && lprops->flags & LPROPS_INDEX)
1008c2ecf20Sopenharmony_ci		return ret;
1018c2ecf20Sopenharmony_ci	/* If specified, exclude empty or freeable LEBs */
1028c2ecf20Sopenharmony_ci	if (lprops->free + lprops->dirty == c->leb_size) {
1038c2ecf20Sopenharmony_ci		if (!data->pick_free)
1048c2ecf20Sopenharmony_ci			return ret;
1058c2ecf20Sopenharmony_ci	/* Exclude LEBs with too little dirty space (unless it is empty) */
1068c2ecf20Sopenharmony_ci	} else if (lprops->dirty < c->dead_wm)
1078c2ecf20Sopenharmony_ci		return ret;
1088c2ecf20Sopenharmony_ci	/* Finally we found space */
1098c2ecf20Sopenharmony_ci	data->lnum = lprops->lnum;
1108c2ecf20Sopenharmony_ci	return LPT_SCAN_ADD | LPT_SCAN_STOP;
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci/**
1148c2ecf20Sopenharmony_ci * scan_for_dirty - find a data LEB with free space.
1158c2ecf20Sopenharmony_ci * @c: the UBIFS file-system description object
1168c2ecf20Sopenharmony_ci * @min_space: minimum amount free plus dirty space the returned LEB has to
1178c2ecf20Sopenharmony_ci *             have
1188c2ecf20Sopenharmony_ci * @pick_free: if it is OK to return a free or freeable LEB
1198c2ecf20Sopenharmony_ci * @exclude_index: whether to exclude index LEBs
1208c2ecf20Sopenharmony_ci *
1218c2ecf20Sopenharmony_ci * This function returns a pointer to the LEB properties found or a negative
1228c2ecf20Sopenharmony_ci * error code.
1238c2ecf20Sopenharmony_ci */
1248c2ecf20Sopenharmony_cistatic const struct ubifs_lprops *scan_for_dirty(struct ubifs_info *c,
1258c2ecf20Sopenharmony_ci						 int min_space, int pick_free,
1268c2ecf20Sopenharmony_ci						 int exclude_index)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	const struct ubifs_lprops *lprops;
1298c2ecf20Sopenharmony_ci	struct ubifs_lpt_heap *heap;
1308c2ecf20Sopenharmony_ci	struct scan_data data;
1318c2ecf20Sopenharmony_ci	int err, i;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	/* There may be an LEB with enough dirty space on the free heap */
1348c2ecf20Sopenharmony_ci	heap = &c->lpt_heap[LPROPS_FREE - 1];
1358c2ecf20Sopenharmony_ci	for (i = 0; i < heap->cnt; i++) {
1368c2ecf20Sopenharmony_ci		lprops = heap->arr[i];
1378c2ecf20Sopenharmony_ci		if (lprops->free + lprops->dirty < min_space)
1388c2ecf20Sopenharmony_ci			continue;
1398c2ecf20Sopenharmony_ci		if (lprops->dirty < c->dead_wm)
1408c2ecf20Sopenharmony_ci			continue;
1418c2ecf20Sopenharmony_ci		return lprops;
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci	/*
1448c2ecf20Sopenharmony_ci	 * A LEB may have fallen off of the bottom of the dirty heap, and ended
1458c2ecf20Sopenharmony_ci	 * up as uncategorized even though it has enough dirty space for us now,
1468c2ecf20Sopenharmony_ci	 * so check the uncategorized list. N.B. neither empty nor freeable LEBs
1478c2ecf20Sopenharmony_ci	 * can end up as uncategorized because they are kept on lists not
1488c2ecf20Sopenharmony_ci	 * finite-sized heaps.
1498c2ecf20Sopenharmony_ci	 */
1508c2ecf20Sopenharmony_ci	list_for_each_entry(lprops, &c->uncat_list, list) {
1518c2ecf20Sopenharmony_ci		if (lprops->flags & LPROPS_TAKEN)
1528c2ecf20Sopenharmony_ci			continue;
1538c2ecf20Sopenharmony_ci		if (lprops->free + lprops->dirty < min_space)
1548c2ecf20Sopenharmony_ci			continue;
1558c2ecf20Sopenharmony_ci		if (exclude_index && (lprops->flags & LPROPS_INDEX))
1568c2ecf20Sopenharmony_ci			continue;
1578c2ecf20Sopenharmony_ci		if (lprops->dirty < c->dead_wm)
1588c2ecf20Sopenharmony_ci			continue;
1598c2ecf20Sopenharmony_ci		return lprops;
1608c2ecf20Sopenharmony_ci	}
1618c2ecf20Sopenharmony_ci	/* We have looked everywhere in main memory, now scan the flash */
1628c2ecf20Sopenharmony_ci	if (c->pnodes_have >= c->pnode_cnt)
1638c2ecf20Sopenharmony_ci		/* All pnodes are in memory, so skip scan */
1648c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOSPC);
1658c2ecf20Sopenharmony_ci	data.min_space = min_space;
1668c2ecf20Sopenharmony_ci	data.pick_free = pick_free;
1678c2ecf20Sopenharmony_ci	data.lnum = -1;
1688c2ecf20Sopenharmony_ci	data.exclude_index = exclude_index;
1698c2ecf20Sopenharmony_ci	err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
1708c2ecf20Sopenharmony_ci				    (ubifs_lpt_scan_callback)scan_for_dirty_cb,
1718c2ecf20Sopenharmony_ci				    &data);
1728c2ecf20Sopenharmony_ci	if (err)
1738c2ecf20Sopenharmony_ci		return ERR_PTR(err);
1748c2ecf20Sopenharmony_ci	ubifs_assert(c, data.lnum >= c->main_first && data.lnum < c->leb_cnt);
1758c2ecf20Sopenharmony_ci	c->lscan_lnum = data.lnum;
1768c2ecf20Sopenharmony_ci	lprops = ubifs_lpt_lookup_dirty(c, data.lnum);
1778c2ecf20Sopenharmony_ci	if (IS_ERR(lprops))
1788c2ecf20Sopenharmony_ci		return lprops;
1798c2ecf20Sopenharmony_ci	ubifs_assert(c, lprops->lnum == data.lnum);
1808c2ecf20Sopenharmony_ci	ubifs_assert(c, lprops->free + lprops->dirty >= min_space);
1818c2ecf20Sopenharmony_ci	ubifs_assert(c, lprops->dirty >= c->dead_wm ||
1828c2ecf20Sopenharmony_ci		     (pick_free &&
1838c2ecf20Sopenharmony_ci		      lprops->free + lprops->dirty == c->leb_size));
1848c2ecf20Sopenharmony_ci	ubifs_assert(c, !(lprops->flags & LPROPS_TAKEN));
1858c2ecf20Sopenharmony_ci	ubifs_assert(c, !exclude_index || !(lprops->flags & LPROPS_INDEX));
1868c2ecf20Sopenharmony_ci	return lprops;
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci/**
1908c2ecf20Sopenharmony_ci * ubifs_find_dirty_leb - find a dirty LEB for the Garbage Collector.
1918c2ecf20Sopenharmony_ci * @c: the UBIFS file-system description object
1928c2ecf20Sopenharmony_ci * @ret_lp: LEB properties are returned here on exit
1938c2ecf20Sopenharmony_ci * @min_space: minimum amount free plus dirty space the returned LEB has to
1948c2ecf20Sopenharmony_ci *             have
1958c2ecf20Sopenharmony_ci * @pick_free: controls whether it is OK to pick empty or index LEBs
1968c2ecf20Sopenharmony_ci *
1978c2ecf20Sopenharmony_ci * This function tries to find a dirty logical eraseblock which has at least
1988c2ecf20Sopenharmony_ci * @min_space free and dirty space. It prefers to take an LEB from the dirty or
1998c2ecf20Sopenharmony_ci * dirty index heap, and it falls-back to LPT scanning if the heaps are empty
2008c2ecf20Sopenharmony_ci * or do not have an LEB which satisfies the @min_space criteria.
2018c2ecf20Sopenharmony_ci *
2028c2ecf20Sopenharmony_ci * Note, LEBs which have less than dead watermark of free + dirty space are
2038c2ecf20Sopenharmony_ci * never picked by this function.
2048c2ecf20Sopenharmony_ci *
2058c2ecf20Sopenharmony_ci * The additional @pick_free argument controls if this function has to return a
2068c2ecf20Sopenharmony_ci * free or freeable LEB if one is present. For example, GC must to set it to %1,
2078c2ecf20Sopenharmony_ci * when called from the journal space reservation function, because the
2088c2ecf20Sopenharmony_ci * appearance of free space may coincide with the loss of enough dirty space
2098c2ecf20Sopenharmony_ci * for GC to succeed anyway.
2108c2ecf20Sopenharmony_ci *
2118c2ecf20Sopenharmony_ci * In contrast, if the Garbage Collector is called from budgeting, it should
2128c2ecf20Sopenharmony_ci * just make free space, not return LEBs which are already free or freeable.
2138c2ecf20Sopenharmony_ci *
2148c2ecf20Sopenharmony_ci * In addition @pick_free is set to %2 by the recovery process in order to
2158c2ecf20Sopenharmony_ci * recover gc_lnum in which case an index LEB must not be returned.
2168c2ecf20Sopenharmony_ci *
2178c2ecf20Sopenharmony_ci * This function returns zero and the LEB properties of found dirty LEB in case
2188c2ecf20Sopenharmony_ci * of success, %-ENOSPC if no dirty LEB was found and a negative error code in
2198c2ecf20Sopenharmony_ci * case of other failures. The returned LEB is marked as "taken".
2208c2ecf20Sopenharmony_ci */
2218c2ecf20Sopenharmony_ciint ubifs_find_dirty_leb(struct ubifs_info *c, struct ubifs_lprops *ret_lp,
2228c2ecf20Sopenharmony_ci			 int min_space, int pick_free)
2238c2ecf20Sopenharmony_ci{
2248c2ecf20Sopenharmony_ci	int err = 0, sum, exclude_index = pick_free == 2 ? 1 : 0;
2258c2ecf20Sopenharmony_ci	const struct ubifs_lprops *lp = NULL, *idx_lp = NULL;
2268c2ecf20Sopenharmony_ci	struct ubifs_lpt_heap *heap, *idx_heap;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	ubifs_get_lprops(c);
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	if (pick_free) {
2318c2ecf20Sopenharmony_ci		int lebs, rsvd_idx_lebs = 0;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci		spin_lock(&c->space_lock);
2348c2ecf20Sopenharmony_ci		lebs = c->lst.empty_lebs + c->idx_gc_cnt;
2358c2ecf20Sopenharmony_ci		lebs += c->freeable_cnt - c->lst.taken_empty_lebs;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci		/*
2388c2ecf20Sopenharmony_ci		 * Note, the index may consume more LEBs than have been reserved
2398c2ecf20Sopenharmony_ci		 * for it. It is OK because it might be consolidated by GC.
2408c2ecf20Sopenharmony_ci		 * But if the index takes fewer LEBs than it is reserved for it,
2418c2ecf20Sopenharmony_ci		 * this function must avoid picking those reserved LEBs.
2428c2ecf20Sopenharmony_ci		 */
2438c2ecf20Sopenharmony_ci		if (c->bi.min_idx_lebs >= c->lst.idx_lebs) {
2448c2ecf20Sopenharmony_ci			rsvd_idx_lebs = c->bi.min_idx_lebs -  c->lst.idx_lebs;
2458c2ecf20Sopenharmony_ci			exclude_index = 1;
2468c2ecf20Sopenharmony_ci		}
2478c2ecf20Sopenharmony_ci		spin_unlock(&c->space_lock);
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci		/* Check if there are enough free LEBs for the index */
2508c2ecf20Sopenharmony_ci		if (rsvd_idx_lebs < lebs) {
2518c2ecf20Sopenharmony_ci			/* OK, try to find an empty LEB */
2528c2ecf20Sopenharmony_ci			lp = ubifs_fast_find_empty(c);
2538c2ecf20Sopenharmony_ci			if (lp)
2548c2ecf20Sopenharmony_ci				goto found;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci			/* Or a freeable LEB */
2578c2ecf20Sopenharmony_ci			lp = ubifs_fast_find_freeable(c);
2588c2ecf20Sopenharmony_ci			if (lp)
2598c2ecf20Sopenharmony_ci				goto found;
2608c2ecf20Sopenharmony_ci		} else
2618c2ecf20Sopenharmony_ci			/*
2628c2ecf20Sopenharmony_ci			 * We cannot pick free/freeable LEBs in the below code.
2638c2ecf20Sopenharmony_ci			 */
2648c2ecf20Sopenharmony_ci			pick_free = 0;
2658c2ecf20Sopenharmony_ci	} else {
2668c2ecf20Sopenharmony_ci		spin_lock(&c->space_lock);
2678c2ecf20Sopenharmony_ci		exclude_index = (c->bi.min_idx_lebs >= c->lst.idx_lebs);
2688c2ecf20Sopenharmony_ci		spin_unlock(&c->space_lock);
2698c2ecf20Sopenharmony_ci	}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	/* Look on the dirty and dirty index heaps */
2728c2ecf20Sopenharmony_ci	heap = &c->lpt_heap[LPROPS_DIRTY - 1];
2738c2ecf20Sopenharmony_ci	idx_heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	if (idx_heap->cnt && !exclude_index) {
2768c2ecf20Sopenharmony_ci		idx_lp = idx_heap->arr[0];
2778c2ecf20Sopenharmony_ci		sum = idx_lp->free + idx_lp->dirty;
2788c2ecf20Sopenharmony_ci		/*
2798c2ecf20Sopenharmony_ci		 * Since we reserve thrice as much space for the index than it
2808c2ecf20Sopenharmony_ci		 * actually takes, it does not make sense to pick indexing LEBs
2818c2ecf20Sopenharmony_ci		 * with less than, say, half LEB of dirty space. May be half is
2828c2ecf20Sopenharmony_ci		 * not the optimal boundary - this should be tested and
2838c2ecf20Sopenharmony_ci		 * checked. This boundary should determine how much we use
2848c2ecf20Sopenharmony_ci		 * in-the-gaps to consolidate the index comparing to how much
2858c2ecf20Sopenharmony_ci		 * we use garbage collector to consolidate it. The "half"
2868c2ecf20Sopenharmony_ci		 * criteria just feels to be fine.
2878c2ecf20Sopenharmony_ci		 */
2888c2ecf20Sopenharmony_ci		if (sum < min_space || sum < c->half_leb_size)
2898c2ecf20Sopenharmony_ci			idx_lp = NULL;
2908c2ecf20Sopenharmony_ci	}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	if (heap->cnt) {
2938c2ecf20Sopenharmony_ci		lp = heap->arr[0];
2948c2ecf20Sopenharmony_ci		if (lp->dirty + lp->free < min_space)
2958c2ecf20Sopenharmony_ci			lp = NULL;
2968c2ecf20Sopenharmony_ci	}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	/* Pick the LEB with most space */
2998c2ecf20Sopenharmony_ci	if (idx_lp && lp) {
3008c2ecf20Sopenharmony_ci		if (idx_lp->free + idx_lp->dirty >= lp->free + lp->dirty)
3018c2ecf20Sopenharmony_ci			lp = idx_lp;
3028c2ecf20Sopenharmony_ci	} else if (idx_lp && !lp)
3038c2ecf20Sopenharmony_ci		lp = idx_lp;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	if (lp) {
3068c2ecf20Sopenharmony_ci		ubifs_assert(c, lp->free + lp->dirty >= c->dead_wm);
3078c2ecf20Sopenharmony_ci		goto found;
3088c2ecf20Sopenharmony_ci	}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	/* Did not find a dirty LEB on the dirty heaps, have to scan */
3118c2ecf20Sopenharmony_ci	dbg_find("scanning LPT for a dirty LEB");
3128c2ecf20Sopenharmony_ci	lp = scan_for_dirty(c, min_space, pick_free, exclude_index);
3138c2ecf20Sopenharmony_ci	if (IS_ERR(lp)) {
3148c2ecf20Sopenharmony_ci		err = PTR_ERR(lp);
3158c2ecf20Sopenharmony_ci		goto out;
3168c2ecf20Sopenharmony_ci	}
3178c2ecf20Sopenharmony_ci	ubifs_assert(c, lp->dirty >= c->dead_wm ||
3188c2ecf20Sopenharmony_ci		     (pick_free && lp->free + lp->dirty == c->leb_size));
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_cifound:
3218c2ecf20Sopenharmony_ci	dbg_find("found LEB %d, free %d, dirty %d, flags %#x",
3228c2ecf20Sopenharmony_ci		 lp->lnum, lp->free, lp->dirty, lp->flags);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
3258c2ecf20Sopenharmony_ci			     lp->flags | LPROPS_TAKEN, 0);
3268c2ecf20Sopenharmony_ci	if (IS_ERR(lp)) {
3278c2ecf20Sopenharmony_ci		err = PTR_ERR(lp);
3288c2ecf20Sopenharmony_ci		goto out;
3298c2ecf20Sopenharmony_ci	}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	memcpy(ret_lp, lp, sizeof(struct ubifs_lprops));
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ciout:
3348c2ecf20Sopenharmony_ci	ubifs_release_lprops(c);
3358c2ecf20Sopenharmony_ci	return err;
3368c2ecf20Sopenharmony_ci}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci/**
3398c2ecf20Sopenharmony_ci * scan_for_free_cb - free space scan callback.
3408c2ecf20Sopenharmony_ci * @c: the UBIFS file-system description object
3418c2ecf20Sopenharmony_ci * @lprops: LEB properties to scan
3428c2ecf20Sopenharmony_ci * @in_tree: whether the LEB properties are in main memory
3438c2ecf20Sopenharmony_ci * @data: information passed to and from the caller of the scan
3448c2ecf20Sopenharmony_ci *
3458c2ecf20Sopenharmony_ci * This function returns a code that indicates whether the scan should continue
3468c2ecf20Sopenharmony_ci * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
3478c2ecf20Sopenharmony_ci * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
3488c2ecf20Sopenharmony_ci * (%LPT_SCAN_STOP).
3498c2ecf20Sopenharmony_ci */
3508c2ecf20Sopenharmony_cistatic int scan_for_free_cb(struct ubifs_info *c,
3518c2ecf20Sopenharmony_ci			    const struct ubifs_lprops *lprops, int in_tree,
3528c2ecf20Sopenharmony_ci			    struct scan_data *data)
3538c2ecf20Sopenharmony_ci{
3548c2ecf20Sopenharmony_ci	int ret = LPT_SCAN_CONTINUE;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	/* Exclude LEBs that are currently in use */
3578c2ecf20Sopenharmony_ci	if (lprops->flags & LPROPS_TAKEN)
3588c2ecf20Sopenharmony_ci		return LPT_SCAN_CONTINUE;
3598c2ecf20Sopenharmony_ci	/* Determine whether to add these LEB properties to the tree */
3608c2ecf20Sopenharmony_ci	if (!in_tree && valuable(c, lprops))
3618c2ecf20Sopenharmony_ci		ret |= LPT_SCAN_ADD;
3628c2ecf20Sopenharmony_ci	/* Exclude index LEBs */
3638c2ecf20Sopenharmony_ci	if (lprops->flags & LPROPS_INDEX)
3648c2ecf20Sopenharmony_ci		return ret;
3658c2ecf20Sopenharmony_ci	/* Exclude LEBs with too little space */
3668c2ecf20Sopenharmony_ci	if (lprops->free < data->min_space)
3678c2ecf20Sopenharmony_ci		return ret;
3688c2ecf20Sopenharmony_ci	/* If specified, exclude empty LEBs */
3698c2ecf20Sopenharmony_ci	if (!data->pick_free && lprops->free == c->leb_size)
3708c2ecf20Sopenharmony_ci		return ret;
3718c2ecf20Sopenharmony_ci	/*
3728c2ecf20Sopenharmony_ci	 * LEBs that have only free and dirty space must not be allocated
3738c2ecf20Sopenharmony_ci	 * because they may have been unmapped already or they may have data
3748c2ecf20Sopenharmony_ci	 * that is obsolete only because of nodes that are still sitting in a
3758c2ecf20Sopenharmony_ci	 * wbuf.
3768c2ecf20Sopenharmony_ci	 */
3778c2ecf20Sopenharmony_ci	if (lprops->free + lprops->dirty == c->leb_size && lprops->dirty > 0)
3788c2ecf20Sopenharmony_ci		return ret;
3798c2ecf20Sopenharmony_ci	/* Finally we found space */
3808c2ecf20Sopenharmony_ci	data->lnum = lprops->lnum;
3818c2ecf20Sopenharmony_ci	return LPT_SCAN_ADD | LPT_SCAN_STOP;
3828c2ecf20Sopenharmony_ci}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci/**
3858c2ecf20Sopenharmony_ci * do_find_free_space - find a data LEB with free space.
3868c2ecf20Sopenharmony_ci * @c: the UBIFS file-system description object
3878c2ecf20Sopenharmony_ci * @min_space: minimum amount of free space required
3888c2ecf20Sopenharmony_ci * @pick_free: whether it is OK to scan for empty LEBs
3898c2ecf20Sopenharmony_ci * @squeeze: whether to try to find space in a non-empty LEB first
3908c2ecf20Sopenharmony_ci *
3918c2ecf20Sopenharmony_ci * This function returns a pointer to the LEB properties found or a negative
3928c2ecf20Sopenharmony_ci * error code.
3938c2ecf20Sopenharmony_ci */
3948c2ecf20Sopenharmony_cistatic
3958c2ecf20Sopenharmony_ciconst struct ubifs_lprops *do_find_free_space(struct ubifs_info *c,
3968c2ecf20Sopenharmony_ci					      int min_space, int pick_free,
3978c2ecf20Sopenharmony_ci					      int squeeze)
3988c2ecf20Sopenharmony_ci{
3998c2ecf20Sopenharmony_ci	const struct ubifs_lprops *lprops;
4008c2ecf20Sopenharmony_ci	struct ubifs_lpt_heap *heap;
4018c2ecf20Sopenharmony_ci	struct scan_data data;
4028c2ecf20Sopenharmony_ci	int err, i;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	if (squeeze) {
4058c2ecf20Sopenharmony_ci		lprops = ubifs_fast_find_free(c);
4068c2ecf20Sopenharmony_ci		if (lprops && lprops->free >= min_space)
4078c2ecf20Sopenharmony_ci			return lprops;
4088c2ecf20Sopenharmony_ci	}
4098c2ecf20Sopenharmony_ci	if (pick_free) {
4108c2ecf20Sopenharmony_ci		lprops = ubifs_fast_find_empty(c);
4118c2ecf20Sopenharmony_ci		if (lprops)
4128c2ecf20Sopenharmony_ci			return lprops;
4138c2ecf20Sopenharmony_ci	}
4148c2ecf20Sopenharmony_ci	if (!squeeze) {
4158c2ecf20Sopenharmony_ci		lprops = ubifs_fast_find_free(c);
4168c2ecf20Sopenharmony_ci		if (lprops && lprops->free >= min_space)
4178c2ecf20Sopenharmony_ci			return lprops;
4188c2ecf20Sopenharmony_ci	}
4198c2ecf20Sopenharmony_ci	/* There may be an LEB with enough free space on the dirty heap */
4208c2ecf20Sopenharmony_ci	heap = &c->lpt_heap[LPROPS_DIRTY - 1];
4218c2ecf20Sopenharmony_ci	for (i = 0; i < heap->cnt; i++) {
4228c2ecf20Sopenharmony_ci		lprops = heap->arr[i];
4238c2ecf20Sopenharmony_ci		if (lprops->free >= min_space)
4248c2ecf20Sopenharmony_ci			return lprops;
4258c2ecf20Sopenharmony_ci	}
4268c2ecf20Sopenharmony_ci	/*
4278c2ecf20Sopenharmony_ci	 * A LEB may have fallen off of the bottom of the free heap, and ended
4288c2ecf20Sopenharmony_ci	 * up as uncategorized even though it has enough free space for us now,
4298c2ecf20Sopenharmony_ci	 * so check the uncategorized list. N.B. neither empty nor freeable LEBs
4308c2ecf20Sopenharmony_ci	 * can end up as uncategorized because they are kept on lists not
4318c2ecf20Sopenharmony_ci	 * finite-sized heaps.
4328c2ecf20Sopenharmony_ci	 */
4338c2ecf20Sopenharmony_ci	list_for_each_entry(lprops, &c->uncat_list, list) {
4348c2ecf20Sopenharmony_ci		if (lprops->flags & LPROPS_TAKEN)
4358c2ecf20Sopenharmony_ci			continue;
4368c2ecf20Sopenharmony_ci		if (lprops->flags & LPROPS_INDEX)
4378c2ecf20Sopenharmony_ci			continue;
4388c2ecf20Sopenharmony_ci		if (lprops->free >= min_space)
4398c2ecf20Sopenharmony_ci			return lprops;
4408c2ecf20Sopenharmony_ci	}
4418c2ecf20Sopenharmony_ci	/* We have looked everywhere in main memory, now scan the flash */
4428c2ecf20Sopenharmony_ci	if (c->pnodes_have >= c->pnode_cnt)
4438c2ecf20Sopenharmony_ci		/* All pnodes are in memory, so skip scan */
4448c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOSPC);
4458c2ecf20Sopenharmony_ci	data.min_space = min_space;
4468c2ecf20Sopenharmony_ci	data.pick_free = pick_free;
4478c2ecf20Sopenharmony_ci	data.lnum = -1;
4488c2ecf20Sopenharmony_ci	err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
4498c2ecf20Sopenharmony_ci				    (ubifs_lpt_scan_callback)scan_for_free_cb,
4508c2ecf20Sopenharmony_ci				    &data);
4518c2ecf20Sopenharmony_ci	if (err)
4528c2ecf20Sopenharmony_ci		return ERR_PTR(err);
4538c2ecf20Sopenharmony_ci	ubifs_assert(c, data.lnum >= c->main_first && data.lnum < c->leb_cnt);
4548c2ecf20Sopenharmony_ci	c->lscan_lnum = data.lnum;
4558c2ecf20Sopenharmony_ci	lprops = ubifs_lpt_lookup_dirty(c, data.lnum);
4568c2ecf20Sopenharmony_ci	if (IS_ERR(lprops))
4578c2ecf20Sopenharmony_ci		return lprops;
4588c2ecf20Sopenharmony_ci	ubifs_assert(c, lprops->lnum == data.lnum);
4598c2ecf20Sopenharmony_ci	ubifs_assert(c, lprops->free >= min_space);
4608c2ecf20Sopenharmony_ci	ubifs_assert(c, !(lprops->flags & LPROPS_TAKEN));
4618c2ecf20Sopenharmony_ci	ubifs_assert(c, !(lprops->flags & LPROPS_INDEX));
4628c2ecf20Sopenharmony_ci	return lprops;
4638c2ecf20Sopenharmony_ci}
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci/**
4668c2ecf20Sopenharmony_ci * ubifs_find_free_space - find a data LEB with free space.
4678c2ecf20Sopenharmony_ci * @c: the UBIFS file-system description object
4688c2ecf20Sopenharmony_ci * @min_space: minimum amount of required free space
4698c2ecf20Sopenharmony_ci * @offs: contains offset of where free space starts on exit
4708c2ecf20Sopenharmony_ci * @squeeze: whether to try to find space in a non-empty LEB first
4718c2ecf20Sopenharmony_ci *
4728c2ecf20Sopenharmony_ci * This function looks for an LEB with at least @min_space bytes of free space.
4738c2ecf20Sopenharmony_ci * It tries to find an empty LEB if possible. If no empty LEBs are available,
4748c2ecf20Sopenharmony_ci * this function searches for a non-empty data LEB. The returned LEB is marked
4758c2ecf20Sopenharmony_ci * as "taken".
4768c2ecf20Sopenharmony_ci *
4778c2ecf20Sopenharmony_ci * This function returns found LEB number in case of success, %-ENOSPC if it
4788c2ecf20Sopenharmony_ci * failed to find a LEB with @min_space bytes of free space and other a negative
4798c2ecf20Sopenharmony_ci * error codes in case of failure.
4808c2ecf20Sopenharmony_ci */
4818c2ecf20Sopenharmony_ciint ubifs_find_free_space(struct ubifs_info *c, int min_space, int *offs,
4828c2ecf20Sopenharmony_ci			  int squeeze)
4838c2ecf20Sopenharmony_ci{
4848c2ecf20Sopenharmony_ci	const struct ubifs_lprops *lprops;
4858c2ecf20Sopenharmony_ci	int lebs, rsvd_idx_lebs, pick_free = 0, err, lnum, flags;
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	dbg_find("min_space %d", min_space);
4888c2ecf20Sopenharmony_ci	ubifs_get_lprops(c);
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	/* Check if there are enough empty LEBs for commit */
4918c2ecf20Sopenharmony_ci	spin_lock(&c->space_lock);
4928c2ecf20Sopenharmony_ci	if (c->bi.min_idx_lebs > c->lst.idx_lebs)
4938c2ecf20Sopenharmony_ci		rsvd_idx_lebs = c->bi.min_idx_lebs -  c->lst.idx_lebs;
4948c2ecf20Sopenharmony_ci	else
4958c2ecf20Sopenharmony_ci		rsvd_idx_lebs = 0;
4968c2ecf20Sopenharmony_ci	lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt -
4978c2ecf20Sopenharmony_ci	       c->lst.taken_empty_lebs;
4988c2ecf20Sopenharmony_ci	if (rsvd_idx_lebs < lebs)
4998c2ecf20Sopenharmony_ci		/*
5008c2ecf20Sopenharmony_ci		 * OK to allocate an empty LEB, but we still don't want to go
5018c2ecf20Sopenharmony_ci		 * looking for one if there aren't any.
5028c2ecf20Sopenharmony_ci		 */
5038c2ecf20Sopenharmony_ci		if (c->lst.empty_lebs - c->lst.taken_empty_lebs > 0) {
5048c2ecf20Sopenharmony_ci			pick_free = 1;
5058c2ecf20Sopenharmony_ci			/*
5068c2ecf20Sopenharmony_ci			 * Because we release the space lock, we must account
5078c2ecf20Sopenharmony_ci			 * for this allocation here. After the LEB properties
5088c2ecf20Sopenharmony_ci			 * flags have been updated, we subtract one. Note, the
5098c2ecf20Sopenharmony_ci			 * result of this is that lprops also decreases
5108c2ecf20Sopenharmony_ci			 * @taken_empty_lebs in 'ubifs_change_lp()', so it is
5118c2ecf20Sopenharmony_ci			 * off by one for a short period of time which may
5128c2ecf20Sopenharmony_ci			 * introduce a small disturbance to budgeting
5138c2ecf20Sopenharmony_ci			 * calculations, but this is harmless because at the
5148c2ecf20Sopenharmony_ci			 * worst case this would make the budgeting subsystem
5158c2ecf20Sopenharmony_ci			 * be more pessimistic than needed.
5168c2ecf20Sopenharmony_ci			 *
5178c2ecf20Sopenharmony_ci			 * Fundamentally, this is about serialization of the
5188c2ecf20Sopenharmony_ci			 * budgeting and lprops subsystems. We could make the
5198c2ecf20Sopenharmony_ci			 * @space_lock a mutex and avoid dropping it before
5208c2ecf20Sopenharmony_ci			 * calling 'ubifs_change_lp()', but mutex is more
5218c2ecf20Sopenharmony_ci			 * heavy-weight, and we want budgeting to be as fast as
5228c2ecf20Sopenharmony_ci			 * possible.
5238c2ecf20Sopenharmony_ci			 */
5248c2ecf20Sopenharmony_ci			c->lst.taken_empty_lebs += 1;
5258c2ecf20Sopenharmony_ci		}
5268c2ecf20Sopenharmony_ci	spin_unlock(&c->space_lock);
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci	lprops = do_find_free_space(c, min_space, pick_free, squeeze);
5298c2ecf20Sopenharmony_ci	if (IS_ERR(lprops)) {
5308c2ecf20Sopenharmony_ci		err = PTR_ERR(lprops);
5318c2ecf20Sopenharmony_ci		goto out;
5328c2ecf20Sopenharmony_ci	}
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	lnum = lprops->lnum;
5358c2ecf20Sopenharmony_ci	flags = lprops->flags | LPROPS_TAKEN;
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	lprops = ubifs_change_lp(c, lprops, LPROPS_NC, LPROPS_NC, flags, 0);
5388c2ecf20Sopenharmony_ci	if (IS_ERR(lprops)) {
5398c2ecf20Sopenharmony_ci		err = PTR_ERR(lprops);
5408c2ecf20Sopenharmony_ci		goto out;
5418c2ecf20Sopenharmony_ci	}
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	if (pick_free) {
5448c2ecf20Sopenharmony_ci		spin_lock(&c->space_lock);
5458c2ecf20Sopenharmony_ci		c->lst.taken_empty_lebs -= 1;
5468c2ecf20Sopenharmony_ci		spin_unlock(&c->space_lock);
5478c2ecf20Sopenharmony_ci	}
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	*offs = c->leb_size - lprops->free;
5508c2ecf20Sopenharmony_ci	ubifs_release_lprops(c);
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	if (*offs == 0) {
5538c2ecf20Sopenharmony_ci		/*
5548c2ecf20Sopenharmony_ci		 * Ensure that empty LEBs have been unmapped. They may not have
5558c2ecf20Sopenharmony_ci		 * been, for example, because of an unclean unmount.  Also
5568c2ecf20Sopenharmony_ci		 * LEBs that were freeable LEBs (free + dirty == leb_size) will
5578c2ecf20Sopenharmony_ci		 * not have been unmapped.
5588c2ecf20Sopenharmony_ci		 */
5598c2ecf20Sopenharmony_ci		err = ubifs_leb_unmap(c, lnum);
5608c2ecf20Sopenharmony_ci		if (err)
5618c2ecf20Sopenharmony_ci			return err;
5628c2ecf20Sopenharmony_ci	}
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	dbg_find("found LEB %d, free %d", lnum, c->leb_size - *offs);
5658c2ecf20Sopenharmony_ci	ubifs_assert(c, *offs <= c->leb_size - min_space);
5668c2ecf20Sopenharmony_ci	return lnum;
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ciout:
5698c2ecf20Sopenharmony_ci	if (pick_free) {
5708c2ecf20Sopenharmony_ci		spin_lock(&c->space_lock);
5718c2ecf20Sopenharmony_ci		c->lst.taken_empty_lebs -= 1;
5728c2ecf20Sopenharmony_ci		spin_unlock(&c->space_lock);
5738c2ecf20Sopenharmony_ci	}
5748c2ecf20Sopenharmony_ci	ubifs_release_lprops(c);
5758c2ecf20Sopenharmony_ci	return err;
5768c2ecf20Sopenharmony_ci}
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci/**
5798c2ecf20Sopenharmony_ci * scan_for_idx_cb - callback used by the scan for a free LEB for the index.
5808c2ecf20Sopenharmony_ci * @c: the UBIFS file-system description object
5818c2ecf20Sopenharmony_ci * @lprops: LEB properties to scan
5828c2ecf20Sopenharmony_ci * @in_tree: whether the LEB properties are in main memory
5838c2ecf20Sopenharmony_ci * @data: information passed to and from the caller of the scan
5848c2ecf20Sopenharmony_ci *
5858c2ecf20Sopenharmony_ci * This function returns a code that indicates whether the scan should continue
5868c2ecf20Sopenharmony_ci * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
5878c2ecf20Sopenharmony_ci * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
5888c2ecf20Sopenharmony_ci * (%LPT_SCAN_STOP).
5898c2ecf20Sopenharmony_ci */
5908c2ecf20Sopenharmony_cistatic int scan_for_idx_cb(struct ubifs_info *c,
5918c2ecf20Sopenharmony_ci			   const struct ubifs_lprops *lprops, int in_tree,
5928c2ecf20Sopenharmony_ci			   struct scan_data *data)
5938c2ecf20Sopenharmony_ci{
5948c2ecf20Sopenharmony_ci	int ret = LPT_SCAN_CONTINUE;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	/* Exclude LEBs that are currently in use */
5978c2ecf20Sopenharmony_ci	if (lprops->flags & LPROPS_TAKEN)
5988c2ecf20Sopenharmony_ci		return LPT_SCAN_CONTINUE;
5998c2ecf20Sopenharmony_ci	/* Determine whether to add these LEB properties to the tree */
6008c2ecf20Sopenharmony_ci	if (!in_tree && valuable(c, lprops))
6018c2ecf20Sopenharmony_ci		ret |= LPT_SCAN_ADD;
6028c2ecf20Sopenharmony_ci	/* Exclude index LEBS */
6038c2ecf20Sopenharmony_ci	if (lprops->flags & LPROPS_INDEX)
6048c2ecf20Sopenharmony_ci		return ret;
6058c2ecf20Sopenharmony_ci	/* Exclude LEBs that cannot be made empty */
6068c2ecf20Sopenharmony_ci	if (lprops->free + lprops->dirty != c->leb_size)
6078c2ecf20Sopenharmony_ci		return ret;
6088c2ecf20Sopenharmony_ci	/*
6098c2ecf20Sopenharmony_ci	 * We are allocating for the index so it is safe to allocate LEBs with
6108c2ecf20Sopenharmony_ci	 * only free and dirty space, because write buffers are sync'd at commit
6118c2ecf20Sopenharmony_ci	 * start.
6128c2ecf20Sopenharmony_ci	 */
6138c2ecf20Sopenharmony_ci	data->lnum = lprops->lnum;
6148c2ecf20Sopenharmony_ci	return LPT_SCAN_ADD | LPT_SCAN_STOP;
6158c2ecf20Sopenharmony_ci}
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci/**
6188c2ecf20Sopenharmony_ci * scan_for_leb_for_idx - scan for a free LEB for the index.
6198c2ecf20Sopenharmony_ci * @c: the UBIFS file-system description object
6208c2ecf20Sopenharmony_ci */
6218c2ecf20Sopenharmony_cistatic const struct ubifs_lprops *scan_for_leb_for_idx(struct ubifs_info *c)
6228c2ecf20Sopenharmony_ci{
6238c2ecf20Sopenharmony_ci	const struct ubifs_lprops *lprops;
6248c2ecf20Sopenharmony_ci	struct scan_data data;
6258c2ecf20Sopenharmony_ci	int err;
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	data.lnum = -1;
6288c2ecf20Sopenharmony_ci	err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
6298c2ecf20Sopenharmony_ci				    (ubifs_lpt_scan_callback)scan_for_idx_cb,
6308c2ecf20Sopenharmony_ci				    &data);
6318c2ecf20Sopenharmony_ci	if (err)
6328c2ecf20Sopenharmony_ci		return ERR_PTR(err);
6338c2ecf20Sopenharmony_ci	ubifs_assert(c, data.lnum >= c->main_first && data.lnum < c->leb_cnt);
6348c2ecf20Sopenharmony_ci	c->lscan_lnum = data.lnum;
6358c2ecf20Sopenharmony_ci	lprops = ubifs_lpt_lookup_dirty(c, data.lnum);
6368c2ecf20Sopenharmony_ci	if (IS_ERR(lprops))
6378c2ecf20Sopenharmony_ci		return lprops;
6388c2ecf20Sopenharmony_ci	ubifs_assert(c, lprops->lnum == data.lnum);
6398c2ecf20Sopenharmony_ci	ubifs_assert(c, lprops->free + lprops->dirty == c->leb_size);
6408c2ecf20Sopenharmony_ci	ubifs_assert(c, !(lprops->flags & LPROPS_TAKEN));
6418c2ecf20Sopenharmony_ci	ubifs_assert(c, !(lprops->flags & LPROPS_INDEX));
6428c2ecf20Sopenharmony_ci	return lprops;
6438c2ecf20Sopenharmony_ci}
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci/**
6468c2ecf20Sopenharmony_ci * ubifs_find_free_leb_for_idx - find a free LEB for the index.
6478c2ecf20Sopenharmony_ci * @c: the UBIFS file-system description object
6488c2ecf20Sopenharmony_ci *
6498c2ecf20Sopenharmony_ci * This function looks for a free LEB and returns that LEB number. The returned
6508c2ecf20Sopenharmony_ci * LEB is marked as "taken", "index".
6518c2ecf20Sopenharmony_ci *
6528c2ecf20Sopenharmony_ci * Only empty LEBs are allocated. This is for two reasons. First, the commit
6538c2ecf20Sopenharmony_ci * calculates the number of LEBs to allocate based on the assumption that they
6548c2ecf20Sopenharmony_ci * will be empty. Secondly, free space at the end of an index LEB is not
6558c2ecf20Sopenharmony_ci * guaranteed to be empty because it may have been used by the in-the-gaps
6568c2ecf20Sopenharmony_ci * method prior to an unclean unmount.
6578c2ecf20Sopenharmony_ci *
6588c2ecf20Sopenharmony_ci * If no LEB is found %-ENOSPC is returned. For other failures another negative
6598c2ecf20Sopenharmony_ci * error code is returned.
6608c2ecf20Sopenharmony_ci */
6618c2ecf20Sopenharmony_ciint ubifs_find_free_leb_for_idx(struct ubifs_info *c)
6628c2ecf20Sopenharmony_ci{
6638c2ecf20Sopenharmony_ci	const struct ubifs_lprops *lprops;
6648c2ecf20Sopenharmony_ci	int lnum = -1, err, flags;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	ubifs_get_lprops(c);
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	lprops = ubifs_fast_find_empty(c);
6698c2ecf20Sopenharmony_ci	if (!lprops) {
6708c2ecf20Sopenharmony_ci		lprops = ubifs_fast_find_freeable(c);
6718c2ecf20Sopenharmony_ci		if (!lprops) {
6728c2ecf20Sopenharmony_ci			/*
6738c2ecf20Sopenharmony_ci			 * The first condition means the following: go scan the
6748c2ecf20Sopenharmony_ci			 * LPT if there are uncategorized lprops, which means
6758c2ecf20Sopenharmony_ci			 * there may be freeable LEBs there (UBIFS does not
6768c2ecf20Sopenharmony_ci			 * store the information about freeable LEBs in the
6778c2ecf20Sopenharmony_ci			 * master node).
6788c2ecf20Sopenharmony_ci			 */
6798c2ecf20Sopenharmony_ci			if (c->in_a_category_cnt != c->main_lebs ||
6808c2ecf20Sopenharmony_ci			    c->lst.empty_lebs - c->lst.taken_empty_lebs > 0) {
6818c2ecf20Sopenharmony_ci				ubifs_assert(c, c->freeable_cnt == 0);
6828c2ecf20Sopenharmony_ci				lprops = scan_for_leb_for_idx(c);
6838c2ecf20Sopenharmony_ci				if (IS_ERR(lprops)) {
6848c2ecf20Sopenharmony_ci					err = PTR_ERR(lprops);
6858c2ecf20Sopenharmony_ci					goto out;
6868c2ecf20Sopenharmony_ci				}
6878c2ecf20Sopenharmony_ci			}
6888c2ecf20Sopenharmony_ci		}
6898c2ecf20Sopenharmony_ci	}
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	if (!lprops) {
6928c2ecf20Sopenharmony_ci		err = -ENOSPC;
6938c2ecf20Sopenharmony_ci		goto out;
6948c2ecf20Sopenharmony_ci	}
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	lnum = lprops->lnum;
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	dbg_find("found LEB %d, free %d, dirty %d, flags %#x",
6998c2ecf20Sopenharmony_ci		 lnum, lprops->free, lprops->dirty, lprops->flags);
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci	flags = lprops->flags | LPROPS_TAKEN | LPROPS_INDEX;
7028c2ecf20Sopenharmony_ci	lprops = ubifs_change_lp(c, lprops, c->leb_size, 0, flags, 0);
7038c2ecf20Sopenharmony_ci	if (IS_ERR(lprops)) {
7048c2ecf20Sopenharmony_ci		err = PTR_ERR(lprops);
7058c2ecf20Sopenharmony_ci		goto out;
7068c2ecf20Sopenharmony_ci	}
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	ubifs_release_lprops(c);
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	/*
7118c2ecf20Sopenharmony_ci	 * Ensure that empty LEBs have been unmapped. They may not have been,
7128c2ecf20Sopenharmony_ci	 * for example, because of an unclean unmount. Also LEBs that were
7138c2ecf20Sopenharmony_ci	 * freeable LEBs (free + dirty == leb_size) will not have been unmapped.
7148c2ecf20Sopenharmony_ci	 */
7158c2ecf20Sopenharmony_ci	err = ubifs_leb_unmap(c, lnum);
7168c2ecf20Sopenharmony_ci	if (err) {
7178c2ecf20Sopenharmony_ci		ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
7188c2ecf20Sopenharmony_ci				    LPROPS_TAKEN | LPROPS_INDEX, 0);
7198c2ecf20Sopenharmony_ci		return err;
7208c2ecf20Sopenharmony_ci	}
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	return lnum;
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ciout:
7258c2ecf20Sopenharmony_ci	ubifs_release_lprops(c);
7268c2ecf20Sopenharmony_ci	return err;
7278c2ecf20Sopenharmony_ci}
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_cistatic int cmp_dirty_idx(const struct ubifs_lprops **a,
7308c2ecf20Sopenharmony_ci			 const struct ubifs_lprops **b)
7318c2ecf20Sopenharmony_ci{
7328c2ecf20Sopenharmony_ci	const struct ubifs_lprops *lpa = *a;
7338c2ecf20Sopenharmony_ci	const struct ubifs_lprops *lpb = *b;
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	return lpa->dirty + lpa->free - lpb->dirty - lpb->free;
7368c2ecf20Sopenharmony_ci}
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci/**
7398c2ecf20Sopenharmony_ci * ubifs_save_dirty_idx_lnums - save an array of the most dirty index LEB nos.
7408c2ecf20Sopenharmony_ci * @c: the UBIFS file-system description object
7418c2ecf20Sopenharmony_ci *
7428c2ecf20Sopenharmony_ci * This function is called each commit to create an array of LEB numbers of
7438c2ecf20Sopenharmony_ci * dirty index LEBs sorted in order of dirty and free space.  This is used by
7448c2ecf20Sopenharmony_ci * the in-the-gaps method of TNC commit.
7458c2ecf20Sopenharmony_ci */
7468c2ecf20Sopenharmony_ciint ubifs_save_dirty_idx_lnums(struct ubifs_info *c)
7478c2ecf20Sopenharmony_ci{
7488c2ecf20Sopenharmony_ci	int i;
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	ubifs_get_lprops(c);
7518c2ecf20Sopenharmony_ci	/* Copy the LPROPS_DIRTY_IDX heap */
7528c2ecf20Sopenharmony_ci	c->dirty_idx.cnt = c->lpt_heap[LPROPS_DIRTY_IDX - 1].cnt;
7538c2ecf20Sopenharmony_ci	memcpy(c->dirty_idx.arr, c->lpt_heap[LPROPS_DIRTY_IDX - 1].arr,
7548c2ecf20Sopenharmony_ci	       sizeof(void *) * c->dirty_idx.cnt);
7558c2ecf20Sopenharmony_ci	/* Sort it so that the dirtiest is now at the end */
7568c2ecf20Sopenharmony_ci	sort(c->dirty_idx.arr, c->dirty_idx.cnt, sizeof(void *),
7578c2ecf20Sopenharmony_ci	     (int (*)(const void *, const void *))cmp_dirty_idx, NULL);
7588c2ecf20Sopenharmony_ci	dbg_find("found %d dirty index LEBs", c->dirty_idx.cnt);
7598c2ecf20Sopenharmony_ci	if (c->dirty_idx.cnt)
7608c2ecf20Sopenharmony_ci		dbg_find("dirtiest index LEB is %d with dirty %d and free %d",
7618c2ecf20Sopenharmony_ci			 c->dirty_idx.arr[c->dirty_idx.cnt - 1]->lnum,
7628c2ecf20Sopenharmony_ci			 c->dirty_idx.arr[c->dirty_idx.cnt - 1]->dirty,
7638c2ecf20Sopenharmony_ci			 c->dirty_idx.arr[c->dirty_idx.cnt - 1]->free);
7648c2ecf20Sopenharmony_ci	/* Replace the lprops pointers with LEB numbers */
7658c2ecf20Sopenharmony_ci	for (i = 0; i < c->dirty_idx.cnt; i++)
7668c2ecf20Sopenharmony_ci		c->dirty_idx.arr[i] = (void *)(size_t)c->dirty_idx.arr[i]->lnum;
7678c2ecf20Sopenharmony_ci	ubifs_release_lprops(c);
7688c2ecf20Sopenharmony_ci	return 0;
7698c2ecf20Sopenharmony_ci}
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci/**
7728c2ecf20Sopenharmony_ci * scan_dirty_idx_cb - callback used by the scan for a dirty index LEB.
7738c2ecf20Sopenharmony_ci * @c: the UBIFS file-system description object
7748c2ecf20Sopenharmony_ci * @lprops: LEB properties to scan
7758c2ecf20Sopenharmony_ci * @in_tree: whether the LEB properties are in main memory
7768c2ecf20Sopenharmony_ci * @data: information passed to and from the caller of the scan
7778c2ecf20Sopenharmony_ci *
7788c2ecf20Sopenharmony_ci * This function returns a code that indicates whether the scan should continue
7798c2ecf20Sopenharmony_ci * (%LPT_SCAN_CONTINUE), whether the LEB properties should be added to the tree
7808c2ecf20Sopenharmony_ci * in main memory (%LPT_SCAN_ADD), or whether the scan should stop
7818c2ecf20Sopenharmony_ci * (%LPT_SCAN_STOP).
7828c2ecf20Sopenharmony_ci */
7838c2ecf20Sopenharmony_cistatic int scan_dirty_idx_cb(struct ubifs_info *c,
7848c2ecf20Sopenharmony_ci			   const struct ubifs_lprops *lprops, int in_tree,
7858c2ecf20Sopenharmony_ci			   struct scan_data *data)
7868c2ecf20Sopenharmony_ci{
7878c2ecf20Sopenharmony_ci	int ret = LPT_SCAN_CONTINUE;
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	/* Exclude LEBs that are currently in use */
7908c2ecf20Sopenharmony_ci	if (lprops->flags & LPROPS_TAKEN)
7918c2ecf20Sopenharmony_ci		return LPT_SCAN_CONTINUE;
7928c2ecf20Sopenharmony_ci	/* Determine whether to add these LEB properties to the tree */
7938c2ecf20Sopenharmony_ci	if (!in_tree && valuable(c, lprops))
7948c2ecf20Sopenharmony_ci		ret |= LPT_SCAN_ADD;
7958c2ecf20Sopenharmony_ci	/* Exclude non-index LEBs */
7968c2ecf20Sopenharmony_ci	if (!(lprops->flags & LPROPS_INDEX))
7978c2ecf20Sopenharmony_ci		return ret;
7988c2ecf20Sopenharmony_ci	/* Exclude LEBs with too little space */
7998c2ecf20Sopenharmony_ci	if (lprops->free + lprops->dirty < c->min_idx_node_sz)
8008c2ecf20Sopenharmony_ci		return ret;
8018c2ecf20Sopenharmony_ci	/* Finally we found space */
8028c2ecf20Sopenharmony_ci	data->lnum = lprops->lnum;
8038c2ecf20Sopenharmony_ci	return LPT_SCAN_ADD | LPT_SCAN_STOP;
8048c2ecf20Sopenharmony_ci}
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci/**
8078c2ecf20Sopenharmony_ci * find_dirty_idx_leb - find a dirty index LEB.
8088c2ecf20Sopenharmony_ci * @c: the UBIFS file-system description object
8098c2ecf20Sopenharmony_ci *
8108c2ecf20Sopenharmony_ci * This function returns LEB number upon success and a negative error code upon
8118c2ecf20Sopenharmony_ci * failure.  In particular, -ENOSPC is returned if a dirty index LEB is not
8128c2ecf20Sopenharmony_ci * found.
8138c2ecf20Sopenharmony_ci *
8148c2ecf20Sopenharmony_ci * Note that this function scans the entire LPT but it is called very rarely.
8158c2ecf20Sopenharmony_ci */
8168c2ecf20Sopenharmony_cistatic int find_dirty_idx_leb(struct ubifs_info *c)
8178c2ecf20Sopenharmony_ci{
8188c2ecf20Sopenharmony_ci	const struct ubifs_lprops *lprops;
8198c2ecf20Sopenharmony_ci	struct ubifs_lpt_heap *heap;
8208c2ecf20Sopenharmony_ci	struct scan_data data;
8218c2ecf20Sopenharmony_ci	int err, i, ret;
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	/* Check all structures in memory first */
8248c2ecf20Sopenharmony_ci	data.lnum = -1;
8258c2ecf20Sopenharmony_ci	heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
8268c2ecf20Sopenharmony_ci	for (i = 0; i < heap->cnt; i++) {
8278c2ecf20Sopenharmony_ci		lprops = heap->arr[i];
8288c2ecf20Sopenharmony_ci		ret = scan_dirty_idx_cb(c, lprops, 1, &data);
8298c2ecf20Sopenharmony_ci		if (ret & LPT_SCAN_STOP)
8308c2ecf20Sopenharmony_ci			goto found;
8318c2ecf20Sopenharmony_ci	}
8328c2ecf20Sopenharmony_ci	list_for_each_entry(lprops, &c->frdi_idx_list, list) {
8338c2ecf20Sopenharmony_ci		ret = scan_dirty_idx_cb(c, lprops, 1, &data);
8348c2ecf20Sopenharmony_ci		if (ret & LPT_SCAN_STOP)
8358c2ecf20Sopenharmony_ci			goto found;
8368c2ecf20Sopenharmony_ci	}
8378c2ecf20Sopenharmony_ci	list_for_each_entry(lprops, &c->uncat_list, list) {
8388c2ecf20Sopenharmony_ci		ret = scan_dirty_idx_cb(c, lprops, 1, &data);
8398c2ecf20Sopenharmony_ci		if (ret & LPT_SCAN_STOP)
8408c2ecf20Sopenharmony_ci			goto found;
8418c2ecf20Sopenharmony_ci	}
8428c2ecf20Sopenharmony_ci	if (c->pnodes_have >= c->pnode_cnt)
8438c2ecf20Sopenharmony_ci		/* All pnodes are in memory, so skip scan */
8448c2ecf20Sopenharmony_ci		return -ENOSPC;
8458c2ecf20Sopenharmony_ci	err = ubifs_lpt_scan_nolock(c, -1, c->lscan_lnum,
8468c2ecf20Sopenharmony_ci				    (ubifs_lpt_scan_callback)scan_dirty_idx_cb,
8478c2ecf20Sopenharmony_ci				    &data);
8488c2ecf20Sopenharmony_ci	if (err)
8498c2ecf20Sopenharmony_ci		return err;
8508c2ecf20Sopenharmony_cifound:
8518c2ecf20Sopenharmony_ci	ubifs_assert(c, data.lnum >= c->main_first && data.lnum < c->leb_cnt);
8528c2ecf20Sopenharmony_ci	c->lscan_lnum = data.lnum;
8538c2ecf20Sopenharmony_ci	lprops = ubifs_lpt_lookup_dirty(c, data.lnum);
8548c2ecf20Sopenharmony_ci	if (IS_ERR(lprops))
8558c2ecf20Sopenharmony_ci		return PTR_ERR(lprops);
8568c2ecf20Sopenharmony_ci	ubifs_assert(c, lprops->lnum == data.lnum);
8578c2ecf20Sopenharmony_ci	ubifs_assert(c, lprops->free + lprops->dirty >= c->min_idx_node_sz);
8588c2ecf20Sopenharmony_ci	ubifs_assert(c, !(lprops->flags & LPROPS_TAKEN));
8598c2ecf20Sopenharmony_ci	ubifs_assert(c, (lprops->flags & LPROPS_INDEX));
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	dbg_find("found dirty LEB %d, free %d, dirty %d, flags %#x",
8628c2ecf20Sopenharmony_ci		 lprops->lnum, lprops->free, lprops->dirty, lprops->flags);
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	lprops = ubifs_change_lp(c, lprops, LPROPS_NC, LPROPS_NC,
8658c2ecf20Sopenharmony_ci				 lprops->flags | LPROPS_TAKEN, 0);
8668c2ecf20Sopenharmony_ci	if (IS_ERR(lprops))
8678c2ecf20Sopenharmony_ci		return PTR_ERR(lprops);
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	return lprops->lnum;
8708c2ecf20Sopenharmony_ci}
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci/**
8738c2ecf20Sopenharmony_ci * get_idx_gc_leb - try to get a LEB number from trivial GC.
8748c2ecf20Sopenharmony_ci * @c: the UBIFS file-system description object
8758c2ecf20Sopenharmony_ci */
8768c2ecf20Sopenharmony_cistatic int get_idx_gc_leb(struct ubifs_info *c)
8778c2ecf20Sopenharmony_ci{
8788c2ecf20Sopenharmony_ci	const struct ubifs_lprops *lp;
8798c2ecf20Sopenharmony_ci	int err, lnum;
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	err = ubifs_get_idx_gc_leb(c);
8828c2ecf20Sopenharmony_ci	if (err < 0)
8838c2ecf20Sopenharmony_ci		return err;
8848c2ecf20Sopenharmony_ci	lnum = err;
8858c2ecf20Sopenharmony_ci	/*
8868c2ecf20Sopenharmony_ci	 * The LEB was due to be unmapped after the commit but
8878c2ecf20Sopenharmony_ci	 * it is needed now for this commit.
8888c2ecf20Sopenharmony_ci	 */
8898c2ecf20Sopenharmony_ci	lp = ubifs_lpt_lookup_dirty(c, lnum);
8908c2ecf20Sopenharmony_ci	if (IS_ERR(lp))
8918c2ecf20Sopenharmony_ci		return PTR_ERR(lp);
8928c2ecf20Sopenharmony_ci	lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
8938c2ecf20Sopenharmony_ci			     lp->flags | LPROPS_INDEX, -1);
8948c2ecf20Sopenharmony_ci	if (IS_ERR(lp))
8958c2ecf20Sopenharmony_ci		return PTR_ERR(lp);
8968c2ecf20Sopenharmony_ci	dbg_find("LEB %d, dirty %d and free %d flags %#x",
8978c2ecf20Sopenharmony_ci		 lp->lnum, lp->dirty, lp->free, lp->flags);
8988c2ecf20Sopenharmony_ci	return lnum;
8998c2ecf20Sopenharmony_ci}
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci/**
9028c2ecf20Sopenharmony_ci * find_dirtiest_idx_leb - find dirtiest index LEB from dirtiest array.
9038c2ecf20Sopenharmony_ci * @c: the UBIFS file-system description object
9048c2ecf20Sopenharmony_ci */
9058c2ecf20Sopenharmony_cistatic int find_dirtiest_idx_leb(struct ubifs_info *c)
9068c2ecf20Sopenharmony_ci{
9078c2ecf20Sopenharmony_ci	const struct ubifs_lprops *lp;
9088c2ecf20Sopenharmony_ci	int lnum;
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	while (1) {
9118c2ecf20Sopenharmony_ci		if (!c->dirty_idx.cnt)
9128c2ecf20Sopenharmony_ci			return -ENOSPC;
9138c2ecf20Sopenharmony_ci		/* The lprops pointers were replaced by LEB numbers */
9148c2ecf20Sopenharmony_ci		lnum = (size_t)c->dirty_idx.arr[--c->dirty_idx.cnt];
9158c2ecf20Sopenharmony_ci		lp = ubifs_lpt_lookup(c, lnum);
9168c2ecf20Sopenharmony_ci		if (IS_ERR(lp))
9178c2ecf20Sopenharmony_ci			return PTR_ERR(lp);
9188c2ecf20Sopenharmony_ci		if ((lp->flags & LPROPS_TAKEN) || !(lp->flags & LPROPS_INDEX))
9198c2ecf20Sopenharmony_ci			continue;
9208c2ecf20Sopenharmony_ci		lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
9218c2ecf20Sopenharmony_ci				     lp->flags | LPROPS_TAKEN, 0);
9228c2ecf20Sopenharmony_ci		if (IS_ERR(lp))
9238c2ecf20Sopenharmony_ci			return PTR_ERR(lp);
9248c2ecf20Sopenharmony_ci		break;
9258c2ecf20Sopenharmony_ci	}
9268c2ecf20Sopenharmony_ci	dbg_find("LEB %d, dirty %d and free %d flags %#x", lp->lnum, lp->dirty,
9278c2ecf20Sopenharmony_ci		 lp->free, lp->flags);
9288c2ecf20Sopenharmony_ci	ubifs_assert(c, lp->flags & LPROPS_TAKEN);
9298c2ecf20Sopenharmony_ci	ubifs_assert(c, lp->flags & LPROPS_INDEX);
9308c2ecf20Sopenharmony_ci	return lnum;
9318c2ecf20Sopenharmony_ci}
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci/**
9348c2ecf20Sopenharmony_ci * ubifs_find_dirty_idx_leb - try to find dirtiest index LEB as at last commit.
9358c2ecf20Sopenharmony_ci * @c: the UBIFS file-system description object
9368c2ecf20Sopenharmony_ci *
9378c2ecf20Sopenharmony_ci * This function attempts to find an untaken index LEB with the most free and
9388c2ecf20Sopenharmony_ci * dirty space that can be used without overwriting index nodes that were in the
9398c2ecf20Sopenharmony_ci * last index committed.
9408c2ecf20Sopenharmony_ci */
9418c2ecf20Sopenharmony_ciint ubifs_find_dirty_idx_leb(struct ubifs_info *c)
9428c2ecf20Sopenharmony_ci{
9438c2ecf20Sopenharmony_ci	int err;
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	ubifs_get_lprops(c);
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci	/*
9488c2ecf20Sopenharmony_ci	 * We made an array of the dirtiest index LEB numbers as at the start of
9498c2ecf20Sopenharmony_ci	 * last commit.  Try that array first.
9508c2ecf20Sopenharmony_ci	 */
9518c2ecf20Sopenharmony_ci	err = find_dirtiest_idx_leb(c);
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_ci	/* Next try scanning the entire LPT */
9548c2ecf20Sopenharmony_ci	if (err == -ENOSPC)
9558c2ecf20Sopenharmony_ci		err = find_dirty_idx_leb(c);
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci	/* Finally take any index LEBs awaiting trivial GC */
9588c2ecf20Sopenharmony_ci	if (err == -ENOSPC)
9598c2ecf20Sopenharmony_ci		err = get_idx_gc_leb(c);
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_ci	ubifs_release_lprops(c);
9628c2ecf20Sopenharmony_ci	return err;
9638c2ecf20Sopenharmony_ci}
964