@@ -2820,13 +2820,39 @@ static int dfs_file_open(struct inode *inode, struct file *file)
28202820 return nonseekable_open (inode , file );
28212821}
28222822
2823+ /**
2824+ * provide_user_output - provide output to the user reading a debugfs file.
2825+ * @val: boolean value for the answer
2826+ * @u: the buffer to store the answer at
2827+ * @count: size of the buffer
2828+ * @ppos: position in the @u output buffer
2829+ *
2830+ * This is a simple helper function which stores @val boolean value in the user
2831+ * buffer when the user reads one of UBIFS debugfs files. Returns amount of
2832+ * bytes written to @u in case of success and a negative error code in case of
2833+ * failure.
2834+ */
2835+ static int provide_user_output (int val , char __user * u , size_t count ,
2836+ loff_t * ppos )
2837+ {
2838+ char buf [3 ];
2839+
2840+ if (val )
2841+ buf [0 ] = '1' ;
2842+ else
2843+ buf [0 ] = '0' ;
2844+ buf [1 ] = '\n' ;
2845+ buf [2 ] = 0x00 ;
2846+
2847+ return simple_read_from_buffer (u , count , ppos , buf , 2 );
2848+ }
2849+
28232850static ssize_t dfs_file_read (struct file * file , char __user * u , size_t count ,
28242851 loff_t * ppos )
28252852{
28262853 struct dentry * dent = file -> f_path .dentry ;
28272854 struct ubifs_info * c = file -> private_data ;
28282855 struct ubifs_debug_info * d = c -> dbg ;
2829- char buf [3 ];
28302856 int val ;
28312857
28322858 if (dent == d -> dfs_chk_gen )
@@ -2844,14 +2870,33 @@ static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
28442870 else
28452871 return - EINVAL ;
28462872
2847- if (val )
2848- buf [0 ] = '1' ;
2849- else
2850- buf [0 ] = '0' ;
2851- buf [1 ] = '\n' ;
2852- buf [2 ] = 0x00 ;
2873+ return provide_user_output (val , u , count , ppos );
2874+ }
28532875
2854- return simple_read_from_buffer (u , count , ppos , buf , 2 );
2876+ /**
2877+ * interpret_user_input - interpret user debugfs file input.
2878+ * @u: user-provided buffer with the input
2879+ * @count: buffer size
2880+ *
2881+ * This is a helper function which interpret user input to a boolean UBIFS
2882+ * debugfs file. Returns %0 or %1 in case of success and a negative error code
2883+ * in case of failure.
2884+ */
2885+ static int interpret_user_input (const char __user * u , size_t count )
2886+ {
2887+ size_t buf_size ;
2888+ char buf [8 ];
2889+
2890+ buf_size = min_t (size_t , count , (sizeof (buf ) - 1 ));
2891+ if (copy_from_user (buf , u , buf_size ))
2892+ return - EFAULT ;
2893+
2894+ if (buf [0 ] == '1' )
2895+ return 1 ;
2896+ else if (buf [0 ] == '0' )
2897+ return 0 ;
2898+
2899+ return - EINVAL ;
28552900}
28562901
28572902static ssize_t dfs_file_write (struct file * file , const char __user * u ,
@@ -2860,8 +2905,6 @@ static ssize_t dfs_file_write(struct file *file, const char __user *u,
28602905 struct ubifs_info * c = file -> private_data ;
28612906 struct ubifs_debug_info * d = c -> dbg ;
28622907 struct dentry * dent = file -> f_path .dentry ;
2863- size_t buf_size ;
2864- char buf [8 ];
28652908 int val ;
28662909
28672910 /*
@@ -2891,16 +2934,9 @@ static ssize_t dfs_file_write(struct file *file, const char __user *u,
28912934 return count ;
28922935 }
28932936
2894- buf_size = min_t (size_t , count , (sizeof (buf ) - 1 ));
2895- if (copy_from_user (buf , u , buf_size ))
2896- return - EFAULT ;
2897-
2898- if (buf [0 ] == '1' )
2899- val = 1 ;
2900- else if (buf [0 ] == '0' )
2901- val = 0 ;
2902- else
2903- return - EINVAL ;
2937+ val = interpret_user_input (u , count );
2938+ if (val < 0 )
2939+ return val ;
29042940
29052941 if (dent == d -> dfs_chk_gen )
29062942 d -> chk_gen = val ;
0 commit comments