/* This function looks for a wear leveling entry with erase counter closest to min + @diff, where min is the smallest erase counter.  */
static struct ubi_wl_entry* find_wl_entry(struct ubi_device *ubi, struct rb_root *root, int diff)
/* This function looks for a wear leveling entry with erase counter closest to min + @diff, where min is the smallest erase counter.  */
static struct ubi_wl_entry* find_wl_entry(struct ubi_device *ubi, struct rb_root *root, int diff)
{
	struct rb_node *p;
	struct ubi_wl_entry *e, *prev_e = NULL;
	int max;
	e = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
	max = e->ec + diff;
	p = root->rb_node;
	while (p) {
		struct ubi_wl_entry *e1;
		e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
		if (e1->ec >= max)
			p = p->rb_left;
		else {
			p = p->rb_right;
			prev_e = e;
			e = e1;
		}
	}
	if (prev_e && !ubi->fm_disabled &&
	    !ubi->fm && e->pnum < UBI_FM_MAX_START)
		return prev_e;
	return e;
}