@@ -250,8 +250,8 @@ EXPORT_SYMBOL_GPL(ffs_lock);
250250static struct ffs_dev * _ffs_find_dev (const char * name );
251251static struct ffs_dev * _ffs_alloc_dev (void );
252252static void _ffs_free_dev (struct ffs_dev * dev );
253- static void * ffs_acquire_dev (const char * dev_name );
254- static void ffs_release_dev (struct ffs_data * ffs_data );
253+ static int ffs_acquire_dev (const char * dev_name , struct ffs_data * ffs_data );
254+ static void ffs_release_dev (struct ffs_dev * ffs_dev );
255255static int ffs_ready (struct ffs_data * ffs );
256256static void ffs_closed (struct ffs_data * ffs );
257257
@@ -1562,8 +1562,8 @@ static int ffs_fs_parse_param(struct fs_context *fc, struct fs_parameter *param)
15621562static int ffs_fs_get_tree (struct fs_context * fc )
15631563{
15641564 struct ffs_sb_fill_data * ctx = fc -> fs_private ;
1565- void * ffs_dev ;
15661565 struct ffs_data * ffs ;
1566+ int ret ;
15671567
15681568 ENTER ();
15691569
@@ -1582,13 +1582,12 @@ static int ffs_fs_get_tree(struct fs_context *fc)
15821582 return - ENOMEM ;
15831583 }
15841584
1585- ffs_dev = ffs_acquire_dev (ffs -> dev_name );
1586- if (IS_ERR ( ffs_dev ) ) {
1585+ ret = ffs_acquire_dev (ffs -> dev_name , ffs );
1586+ if (ret ) {
15871587 ffs_data_put (ffs );
1588- return PTR_ERR ( ffs_dev ) ;
1588+ return ret ;
15891589 }
15901590
1591- ffs -> private_data = ffs_dev ;
15921591 ctx -> ffs_data = ffs ;
15931592 return get_tree_nodev (fc , ffs_sb_fill );
15941593}
@@ -1599,7 +1598,6 @@ static void ffs_fs_free_fc(struct fs_context *fc)
15991598
16001599 if (ctx ) {
16011600 if (ctx -> ffs_data ) {
1602- ffs_release_dev (ctx -> ffs_data );
16031601 ffs_data_put (ctx -> ffs_data );
16041602 }
16051603
@@ -1638,10 +1636,8 @@ ffs_fs_kill_sb(struct super_block *sb)
16381636 ENTER ();
16391637
16401638 kill_litter_super (sb );
1641- if (sb -> s_fs_info ) {
1642- ffs_release_dev (sb -> s_fs_info );
1639+ if (sb -> s_fs_info )
16431640 ffs_data_closed (sb -> s_fs_info );
1644- }
16451641}
16461642
16471643static struct file_system_type ffs_fs_type = {
@@ -1711,6 +1707,7 @@ static void ffs_data_put(struct ffs_data *ffs)
17111707 if (unlikely (refcount_dec_and_test (& ffs -> ref ))) {
17121708 pr_info ("%s(): freeing\n" , __func__ );
17131709 ffs_data_clear (ffs );
1710+ ffs_release_dev (ffs -> private_data );
17141711 BUG_ON (waitqueue_active (& ffs -> ev .waitq ) ||
17151712 waitqueue_active (& ffs -> ep0req_completion .wait ) ||
17161713 waitqueue_active (& ffs -> wait ));
@@ -3040,6 +3037,7 @@ static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
30403037 struct ffs_function * func = ffs_func_from_usb (f );
30413038 struct f_fs_opts * ffs_opts =
30423039 container_of (f -> fi , struct f_fs_opts , func_inst );
3040+ struct ffs_data * ffs_data ;
30433041 int ret ;
30443042
30453043 ENTER ();
@@ -3054,12 +3052,13 @@ static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f,
30543052 if (!ffs_opts -> no_configfs )
30553053 ffs_dev_lock ();
30563054 ret = ffs_opts -> dev -> desc_ready ? 0 : - ENODEV ;
3057- func -> ffs = ffs_opts -> dev -> ffs_data ;
3055+ ffs_data = ffs_opts -> dev -> ffs_data ;
30583056 if (!ffs_opts -> no_configfs )
30593057 ffs_dev_unlock ();
30603058 if (ret )
30613059 return ERR_PTR (ret );
30623060
3061+ func -> ffs = ffs_data ;
30633062 func -> conf = c ;
30643063 func -> gadget = c -> cdev -> gadget ;
30653064
@@ -3514,6 +3513,7 @@ static void ffs_free_inst(struct usb_function_instance *f)
35143513 struct f_fs_opts * opts ;
35153514
35163515 opts = to_f_fs_opts (f );
3516+ ffs_release_dev (opts -> dev );
35173517 ffs_dev_lock ();
35183518 _ffs_free_dev (opts -> dev );
35193519 ffs_dev_unlock ();
@@ -3701,47 +3701,48 @@ static void _ffs_free_dev(struct ffs_dev *dev)
37013701{
37023702 list_del (& dev -> entry );
37033703
3704- /* Clear the private_data pointer to stop incorrect dev access */
3705- if (dev -> ffs_data )
3706- dev -> ffs_data -> private_data = NULL ;
3707-
37083704 kfree (dev );
37093705 if (list_empty (& ffs_devices ))
37103706 functionfs_cleanup ();
37113707}
37123708
3713- static void * ffs_acquire_dev (const char * dev_name )
3709+ static int ffs_acquire_dev (const char * dev_name , struct ffs_data * ffs_data )
37143710{
3711+ int ret = 0 ;
37153712 struct ffs_dev * ffs_dev ;
37163713
37173714 ENTER ();
37183715 ffs_dev_lock ();
37193716
37203717 ffs_dev = _ffs_find_dev (dev_name );
3721- if (!ffs_dev )
3722- ffs_dev = ERR_PTR ( - ENOENT ) ;
3723- else if (ffs_dev -> mounted )
3724- ffs_dev = ERR_PTR ( - EBUSY ) ;
3725- else if (ffs_dev -> ffs_acquire_dev_callback &&
3726- ffs_dev -> ffs_acquire_dev_callback (ffs_dev ))
3727- ffs_dev = ERR_PTR ( - ENOENT ) ;
3728- else
3718+ if (!ffs_dev ) {
3719+ ret = - ENOENT ;
3720+ } else if (ffs_dev -> mounted ) {
3721+ ret = - EBUSY ;
3722+ } else if (ffs_dev -> ffs_acquire_dev_callback &&
3723+ ffs_dev -> ffs_acquire_dev_callback (ffs_dev )) {
3724+ ret = - ENOENT ;
3725+ } else {
37293726 ffs_dev -> mounted = true;
3727+ ffs_dev -> ffs_data = ffs_data ;
3728+ ffs_data -> private_data = ffs_dev ;
3729+ }
37303730
37313731 ffs_dev_unlock ();
3732- return ffs_dev ;
3732+ return ret ;
37333733}
37343734
3735- static void ffs_release_dev (struct ffs_data * ffs_data )
3735+ static void ffs_release_dev (struct ffs_dev * ffs_dev )
37363736{
3737- struct ffs_dev * ffs_dev ;
3738-
37393737 ENTER ();
37403738 ffs_dev_lock ();
37413739
3742- ffs_dev = ffs_data -> private_data ;
3743- if (ffs_dev ) {
3740+ if (ffs_dev && ffs_dev -> mounted ) {
37443741 ffs_dev -> mounted = false;
3742+ if (ffs_dev -> ffs_data ) {
3743+ ffs_dev -> ffs_data -> private_data = NULL ;
3744+ ffs_dev -> ffs_data = NULL ;
3745+ }
37453746
37463747 if (ffs_dev -> ffs_release_dev_callback )
37473748 ffs_dev -> ffs_release_dev_callback (ffs_dev );
@@ -3769,7 +3770,6 @@ static int ffs_ready(struct ffs_data *ffs)
37693770 }
37703771
37713772 ffs_obj -> desc_ready = true;
3772- ffs_obj -> ffs_data = ffs ;
37733773
37743774 if (ffs_obj -> ffs_ready_callback ) {
37753775 ret = ffs_obj -> ffs_ready_callback (ffs );
@@ -3797,7 +3797,6 @@ static void ffs_closed(struct ffs_data *ffs)
37973797 goto done ;
37983798
37993799 ffs_obj -> desc_ready = false;
3800- ffs_obj -> ffs_data = NULL ;
38013800
38023801 if (test_and_clear_bit (FFS_FL_CALL_CLOSED_CALLBACK , & ffs -> flags ) &&
38033802 ffs_obj -> ffs_closed_callback )
0 commit comments