f2fs: fix to handle error paths of {new,change}_curseg()
[linux-2.6-microblaze.git] / fs / f2fs / segment.c
index edddbbd..c9b0ef7 100644 (file)
@@ -2863,7 +2863,7 @@ bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno)
  * This function always allocates a used segment(from dirty seglist) by SSR
  * manner, so it should recover the existing segment information of valid blocks
  */
-static void change_curseg(struct f2fs_sb_info *sbi, int type)
+static int change_curseg(struct f2fs_sb_info *sbi, int type)
 {
        struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
        struct curseg_info *curseg = CURSEG_I(sbi, type);
@@ -2888,21 +2888,23 @@ static void change_curseg(struct f2fs_sb_info *sbi, int type)
        if (IS_ERR(sum_page)) {
                /* GC won't be able to use stale summary pages by cp_error */
                memset(curseg->sum_blk, 0, SUM_ENTRY_SIZE);
-               return;
+               return PTR_ERR(sum_page);
        }
        sum_node = (struct f2fs_summary_block *)page_address(sum_page);
        memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
        f2fs_put_page(sum_page, 1);
+       return 0;
 }
 
 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type,
                                int alloc_mode, unsigned long long age);
 
-static void get_atssr_segment(struct f2fs_sb_info *sbi, int type,
+static int get_atssr_segment(struct f2fs_sb_info *sbi, int type,
                                        int target_type, int alloc_mode,
                                        unsigned long long age)
 {
        struct curseg_info *curseg = CURSEG_I(sbi, type);
+       int ret = 0;
 
        curseg->seg_type = target_type;
 
@@ -2910,38 +2912,41 @@ static void get_atssr_segment(struct f2fs_sb_info *sbi, int type,
                struct seg_entry *se = get_seg_entry(sbi, curseg->next_segno);
 
                curseg->seg_type = se->type;
-               change_curseg(sbi, type);
+               ret = change_curseg(sbi, type);
        } else {
                /* allocate cold segment by default */
                curseg->seg_type = CURSEG_COLD_DATA;
-               new_curseg(sbi, type, true);
+               ret = new_curseg(sbi, type, true);
        }
        stat_inc_seg_type(sbi, curseg);
+       return ret;
 }
 
-static void __f2fs_init_atgc_curseg(struct f2fs_sb_info *sbi)
+static int __f2fs_init_atgc_curseg(struct f2fs_sb_info *sbi)
 {
        struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_ALL_DATA_ATGC);
+       int ret = 0;
 
        if (!sbi->am.atgc_enabled)
-               return;
+               return 0;
 
        f2fs_down_read(&SM_I(sbi)->curseg_lock);
 
        mutex_lock(&curseg->curseg_mutex);
        down_write(&SIT_I(sbi)->sentry_lock);
 
-       get_atssr_segment(sbi, CURSEG_ALL_DATA_ATGC, CURSEG_COLD_DATA, SSR, 0);
+       ret = get_atssr_segment(sbi, CURSEG_ALL_DATA_ATGC,
+                                       CURSEG_COLD_DATA, SSR, 0);
 
        up_write(&SIT_I(sbi)->sentry_lock);
        mutex_unlock(&curseg->curseg_mutex);
 
        f2fs_up_read(&SM_I(sbi)->curseg_lock);
-
+       return ret;
 }
-void f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi)
+int f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi)
 {
-       __f2fs_init_atgc_curseg(sbi);
+       return __f2fs_init_atgc_curseg(sbi);
 }
 
 static void __f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi, int type)
@@ -3069,11 +3074,12 @@ static bool need_new_seg(struct f2fs_sb_info *sbi, int type)
        return false;
 }
 
-void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
+int f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
                                        unsigned int start, unsigned int end)
 {
        struct curseg_info *curseg = CURSEG_I(sbi, type);
        unsigned int segno;
+       int ret = 0;
 
        f2fs_down_read(&SM_I(sbi)->curseg_lock);
        mutex_lock(&curseg->curseg_mutex);
@@ -3084,9 +3090,9 @@ void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
                goto unlock;
 
        if (f2fs_need_SSR(sbi) && get_ssr_segment(sbi, type, SSR, 0))
-               change_curseg(sbi, type);
+               ret = change_curseg(sbi, type);
        else
-               new_curseg(sbi, type, true);
+               ret = new_curseg(sbi, type, true);
 
        stat_inc_seg_type(sbi, curseg);
 
@@ -3100,6 +3106,7 @@ unlock:
 
        mutex_unlock(&curseg->curseg_mutex);
        f2fs_up_read(&SM_I(sbi)->curseg_lock);
+       return ret;
 }
 
 static int __allocate_new_segment(struct f2fs_sb_info *sbi, int type,
@@ -3486,14 +3493,17 @@ int f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
        bool from_gc = (type == CURSEG_ALL_DATA_ATGC);
        struct seg_entry *se = NULL;
        bool segment_full = false;
+       int ret = 0;
 
        f2fs_down_read(&SM_I(sbi)->curseg_lock);
 
        mutex_lock(&curseg->curseg_mutex);
        down_write(&sit_i->sentry_lock);
 
-       if (curseg->segno == NULL_SEGNO)
+       if (curseg->segno == NULL_SEGNO) {
+               ret = -ENOSPC;
                goto out_err;
+       }
 
        if (from_gc) {
                f2fs_bug_on(sbi, GET_SEGNO(sbi, old_blkaddr) == NULL_SEGNO);
@@ -3546,17 +3556,17 @@ int f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
                }
 
                if (from_gc) {
-                       get_atssr_segment(sbi, type, se->type,
+                       ret = get_atssr_segment(sbi, type, se->type,
                                                AT_SSR, se->mtime);
                } else {
                        if (need_new_seg(sbi, type))
-                               new_curseg(sbi, type, false);
+                               ret = new_curseg(sbi, type, false);
                        else
-                               change_curseg(sbi, type);
+                               ret = change_curseg(sbi, type);
                        stat_inc_seg_type(sbi, curseg);
                }
 
-               if (curseg->segno == NULL_SEGNO)
+               if (ret)
                        goto out_err;
        }
 
@@ -3599,7 +3609,7 @@ out_err:
        up_write(&sit_i->sentry_lock);
        mutex_unlock(&curseg->curseg_mutex);
        f2fs_up_read(&SM_I(sbi)->curseg_lock);
-       return -ENOSPC;
+       return ret;
 
 }
 
@@ -3828,7 +3838,8 @@ void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
        /* change the current segment */
        if (segno != curseg->segno) {
                curseg->next_segno = segno;
-               change_curseg(sbi, type);
+               if (change_curseg(sbi, type))
+                       goto out_unlock;
        }
 
        curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
@@ -3854,12 +3865,14 @@ void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
        if (recover_curseg) {
                if (old_cursegno != curseg->segno) {
                        curseg->next_segno = old_cursegno;
-                       change_curseg(sbi, type);
+                       if (change_curseg(sbi, type))
+                               goto out_unlock;
                }
                curseg->next_blkoff = old_blkoff;
                curseg->alloc_type = old_alloc_type;
        }
 
+out_unlock:
        up_write(&sit_i->sentry_lock);
        mutex_unlock(&curseg->curseg_mutex);
        f2fs_up_write(&SM_I(sbi)->curseg_lock);