/* This function looks for a wear leveling entry with erase counter closest to @max and less then @max.  */
static struct ubi_wl_entry* find_wl_entry(struct rb_root *root, int max)
/* This function looks for a wear leveling entry with erase counter closest to @max and less then @max.  */
static struct ubi_wl_entry* find_wl_entry(struct rb_root *root, int max)
{
	struct rb_node *p;
	struct ubi_wl_entry *e;
	e = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
	max += e->ec;
	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;
			e = e1;
		}
	}
	return e;
}