@@ -972,11 +972,39 @@ void dbg_dump_index(struct ubifs_info *c)
972972void dbg_save_space_info (struct ubifs_info * c )
973973{
974974 struct ubifs_debug_info * d = c -> dbg ;
975-
976- ubifs_get_lp_stats (c , & d -> saved_lst );
975+ int freeable_cnt ;
977976
978977 spin_lock (& c -> space_lock );
978+ memcpy (& d -> saved_lst , & c -> lst , sizeof (struct ubifs_lp_stats ));
979+
980+ /*
981+ * We use a dirty hack here and zero out @c->freeable_cnt, because it
982+ * affects the free space calculations, and UBIFS might not know about
983+ * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
984+ * only when we read their lprops, and we do this only lazily, upon the
985+ * need. So at any given point of time @c->freeable_cnt might be not
986+ * exactly accurate.
987+ *
988+ * Just one example about the issue we hit when we did not zero
989+ * @c->freeable_cnt.
990+ * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
991+ * amount of free space in @d->saved_free
992+ * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
993+ * information from flash, where we cache LEBs from various
994+ * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
995+ * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
996+ * -> 'ubifs_get_pnode()' -> 'update_cats()'
997+ * -> 'ubifs_add_to_cat()').
998+ * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
999+ * becomes %1.
1000+ * 4. We calculate the amount of free space when the re-mount is
1001+ * finished in 'dbg_check_space_info()' and it does not match
1002+ * @d->saved_free.
1003+ */
1004+ freeable_cnt = c -> freeable_cnt ;
1005+ c -> freeable_cnt = 0 ;
9791006 d -> saved_free = ubifs_get_free_space_nolock (c );
1007+ c -> freeable_cnt = freeable_cnt ;
9801008 spin_unlock (& c -> space_lock );
9811009}
9821010
@@ -993,12 +1021,15 @@ int dbg_check_space_info(struct ubifs_info *c)
9931021{
9941022 struct ubifs_debug_info * d = c -> dbg ;
9951023 struct ubifs_lp_stats lst ;
996- long long avail , free ;
1024+ long long free ;
1025+ int freeable_cnt ;
9971026
9981027 spin_lock (& c -> space_lock );
999- avail = ubifs_calc_available (c , c -> min_idx_lebs );
1028+ freeable_cnt = c -> freeable_cnt ;
1029+ c -> freeable_cnt = 0 ;
1030+ free = ubifs_get_free_space_nolock (c );
1031+ c -> freeable_cnt = freeable_cnt ;
10001032 spin_unlock (& c -> space_lock );
1001- free = ubifs_get_free_space (c );
10021033
10031034 if (free != d -> saved_free ) {
10041035 ubifs_err ("free space changed from %lld to %lld" ,
@@ -2806,40 +2837,38 @@ int dbg_debugfs_init_fs(struct ubifs_info *c)
28062837 struct ubifs_debug_info * d = c -> dbg ;
28072838
28082839 sprintf (d -> dfs_dir_name , "ubi%d_%d" , c -> vi .ubi_num , c -> vi .vol_id );
2809- d -> dfs_dir = debugfs_create_dir (d -> dfs_dir_name , dfs_rootdir );
2810- if (IS_ERR (d -> dfs_dir )) {
2811- err = PTR_ERR (d -> dfs_dir );
2812- ubifs_err ("cannot create \"%s\" debugfs directory, error %d\n" ,
2813- d -> dfs_dir_name , err );
2840+ fname = d -> dfs_dir_name ;
2841+ dent = debugfs_create_dir (fname , dfs_rootdir );
2842+ if (IS_ERR_OR_NULL (dent ))
28142843 goto out ;
2815- }
2844+ d -> dfs_dir = dent ;
28162845
28172846 fname = "dump_lprops" ;
28182847 dent = debugfs_create_file (fname , S_IWUSR , d -> dfs_dir , c , & dfs_fops );
2819- if (IS_ERR (dent ))
2848+ if (IS_ERR_OR_NULL (dent ))
28202849 goto out_remove ;
28212850 d -> dfs_dump_lprops = dent ;
28222851
28232852 fname = "dump_budg" ;
28242853 dent = debugfs_create_file (fname , S_IWUSR , d -> dfs_dir , c , & dfs_fops );
2825- if (IS_ERR (dent ))
2854+ if (IS_ERR_OR_NULL (dent ))
28262855 goto out_remove ;
28272856 d -> dfs_dump_budg = dent ;
28282857
28292858 fname = "dump_tnc" ;
28302859 dent = debugfs_create_file (fname , S_IWUSR , d -> dfs_dir , c , & dfs_fops );
2831- if (IS_ERR (dent ))
2860+ if (IS_ERR_OR_NULL (dent ))
28322861 goto out_remove ;
28332862 d -> dfs_dump_tnc = dent ;
28342863
28352864 return 0 ;
28362865
28372866out_remove :
2838- err = PTR_ERR (dent );
2839- ubifs_err ("cannot create \"%s\" debugfs directory, error %d\n" ,
2840- fname , err );
28412867 debugfs_remove_recursive (d -> dfs_dir );
28422868out :
2869+ err = dent ? PTR_ERR (dent ) : - ENODEV ;
2870+ ubifs_err ("cannot create \"%s\" debugfs directory, error %d\n" ,
2871+ fname , err );
28432872 return err ;
28442873}
28452874
0 commit comments