/* Validate the request */
                err = -EINVAL;
-               if (req.lnum < 0 || req.lnum >= vol->reserved_pebs ||
+               if (!ubi_leb_valid(vol, req.lnum) ||
                    req.bytes < 0 || req.bytes > vol->usable_leb_size)
                        break;
 
                        break;
                }
 
-               if (lnum < 0 || lnum >= vol->reserved_pebs) {
+               if (!ubi_leb_valid(vol, lnum)) {
                        err = -EINVAL;
                        break;
                }
 
        if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
                return -EROFS;
 
-       if (lnum < 0 || lnum >= vol->reserved_pebs || offset < 0 || len < 0 ||
+       if (!ubi_leb_valid(vol, lnum) || offset < 0 || len < 0 ||
            offset + len > vol->usable_leb_size ||
            offset & (ubi->min_io_size - 1) || len & (ubi->min_io_size - 1))
                return -EINVAL;
        if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
                return -EROFS;
 
-       if (lnum < 0 || lnum >= vol->reserved_pebs || len < 0 ||
+       if (!ubi_leb_valid(vol, lnum) || len < 0 ||
            len > vol->usable_leb_size || len & (ubi->min_io_size - 1))
                return -EINVAL;
 
        if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
                return -EROFS;
 
-       if (lnum < 0 || lnum >= vol->reserved_pebs)
+       if (!ubi_leb_valid(vol, lnum))
                return -EINVAL;
 
        if (vol->upd_marker)
        if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
                return -EROFS;
 
-       if (lnum < 0 || lnum >= vol->reserved_pebs)
+       if (!ubi_leb_valid(vol, lnum))
                return -EINVAL;
 
        if (vol->upd_marker)
        if (desc->mode == UBI_READONLY || vol->vol_type == UBI_STATIC_VOLUME)
                return -EROFS;
 
-       if (lnum < 0 || lnum >= vol->reserved_pebs)
+       if (!ubi_leb_valid(vol, lnum))
                return -EINVAL;
 
        if (vol->upd_marker)
 
        dbg_gen("test LEB %d:%d", vol->vol_id, lnum);
 
-       if (lnum < 0 || lnum >= vol->reserved_pebs)
+       if (!ubi_leb_valid(vol, lnum))
                return -EINVAL;
 
        if (vol->upd_marker)
 
 void ubi_calculate_reserved(struct ubi_device *ubi);
 int ubi_check_pattern(const void *buf, uint8_t patt, int size);
 
+static inline bool ubi_leb_valid(struct ubi_volume *vol, int lnum)
+{
+       return lnum >= 0 && lnum < vol->reserved_pebs;
+}
+
 /* eba.c */
 int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
                      int lnum);