/* This function returns zero in case of success and a negative error code in case of failure.  */
int ubi_wl_flush(struct ubi_device *ubi)
/* This function returns zero in case of success and a negative error code in case of failure.  */
int ubi_wl_flush(struct ubi_device *ubi)
{
	int err;
	dbg_wl("flush (%d pending works)", ubi->works_count);
	while (ubi->works_count) {
		err = do_work(ubi);
		if (err)
			return err;
	}
	down_write(&ubi->work_sem);
	up_write(&ubi->work_sem);
	while (ubi->works_count) {
		dbg_wl("flush more (%d pending works)", ubi->works_count);
		err = do_work(ubi);
		if (err)
			return err;
	}
	return 0;
}