unsigned int old_zoneno = GET_ZONE_FROM_SEG(sbi, *newseg);
        bool init = true;
        int i;
+       int ret = 0;
 
        spin_lock(&free_i->segmap_lock);
 
        if (secno >= MAIN_SECS(sbi)) {
                secno = find_first_zero_bit(free_i->free_secmap,
                                                        MAIN_SECS(sbi));
-               f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
+               if (secno >= MAIN_SECS(sbi)) {
+                       ret = -ENOSPC;
+                       goto out_unlock;
+               }
        }
        segno = GET_SEG_FROM_SEC(sbi, secno);
        zoneno = GET_ZONE_FROM_SEC(sbi, secno);
        f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
        __set_inuse(sbi, segno);
        *newseg = segno;
+out_unlock:
        spin_unlock(&free_i->segmap_lock);
+
+       if (ret) {
+               f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_NO_SEGMENT);
+               f2fs_bug_on(sbi, 1);
+       }
 }
 
 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
 
        STOP_CP_REASON_CORRUPTED_SUMMARY,
        STOP_CP_REASON_UPDATE_INODE,
        STOP_CP_REASON_FLUSH_FAIL,
+       STOP_CP_REASON_NO_SEGMENT,
        STOP_CP_REASON_MAX,
 };