/* This function returns %1 if a bud reference already exists for the LEB. %0 is returned if the reference node is new, otherwise %-EINVAL is returned if validation failed.  */
static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
/* This function returns %1 if a bud reference already exists for the LEB. %0 is returned if the reference node is new, otherwise %-EINVAL is returned if validation failed.  */
static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
{
	struct ubifs_bud *bud;
	int lnum = le32_to_cpu(ref->lnum);
	unsigned int offs = le32_to_cpu(ref->offs);
	unsigned int jhead = le32_to_cpu(ref->jhead);
	if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
	    lnum < c->main_first || offs > c->leb_size ||
	    offs & (c->min_io_size - 1))
		return -EINVAL;
	bud = ubifs_search_bud(c, lnum);
	if (bud) {
		if (bud->jhead == jhead && bud->start <= offs)
			return 1;
		ubifs_err("bud at LEB %d:%d was already referred", lnum, offs);
		return -EINVAL;
	}
	return 0;
}