All of lore.kernel.org
 help / color / mirror / Atom feed
From: Maxim Devaev <mdevaev@gmail.com>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: linux-usb@vger.kernel.org, Felipe Balbi <balbi@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Cai Huoqing <caihuoqing@baidu.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] usb: gadget: f_mass_storage: break IO operations via configfs
Date: Sun, 10 Apr 2022 01:42:28 +0300	[thread overview]
Message-ID: <20220410014228.117d9f66@reki> (raw)
In-Reply-To: <YlHrBdYkBSR0L6FL@rowland.harvard.edu>

В Sat, 9 Apr 2022 16:22:29 -0400
Alan Stern <stern@rowland.harvard.edu> wrote:

> On Sat, Apr 09, 2022 at 05:08:37PM +0300, Maxim Devaev wrote:
> > В Sat, 9 Apr 2022 09:46:32 -0400
> > Alan Stern <stern@rowland.harvard.edu> wrote:
> >   
> > > On Sat, Apr 09, 2022 at 11:57:56AM +0300, Maxim Devaev wrote:  
> > > > В Fri, 8 Apr 2022 10:59:45 -0400
> > > > Alan Stern <stern@rowland.harvard.edu> wrote:    
> > > > > > At least there is one situation where the behavior of f_mass_storage differs
> > > > > > from the behavior of a real drive. What happens when you click on the physical
> > > > > > "eject" button?      
> > > > > 
> > > > > If the host has prevented ejection, nothing happens.  Otherwise the disc 
> > > > > gets ejected.
> > > > >     
> > > > > > Yes, the OS can block this, but the problem is that we don't have
> > > > > > an "eject" here.      
> > > > > 
> > > > > What do you mean?  Writing an empty string to the sysfs "file" attribute 
> > > > > is the virtual analog of pressing the eject button.    
> > > > 
> > > > But I can't eject the disc event it's not mounted on Linux host. It seems to me
> > > > it differs from the real drive behavior.    
> > > 
> > > It sounds like either there's a bug or else you're not doing the right 
> > > thing.  Tell me exactly what you do when this fails.  
> > 
> > I'm using Raspberry Pi with DWC2. So:
> > - Connect RPi-based gadget to the Linux host.
> > - Set image in the "file" attribute.  
> 
> Exactly what is the full pathname you're using for the "file" attribute?

/sys/kernel/config/usb_gadget/kvmd/functions/mass_storage.usb0/lun.0/file

> > - Mount gadget's drive on the Linux host.
> > - Umount it.
> > - Try to eject using emptying the "file" attribute.
> > - Get EBUSY error.  
> 
> This must mean that some program on the host is keeping the device file 
> open, even though it isn't mounted.  (I tried running a similar test on 
> my system and it worked perfectly, with no other programs accessing the 
> device).  You might be able to identify which program is accessing the 
> device by running lsof on the host and searching the output for the 
> device name.

I've been thinking about it too. I tried lsof but it didn't display anything
related with sr0 device. But after execution the "eject" command on the host,
I was able to emptify the "file" attribute of the gadget.

> I also tried sending a USR1 signal to the driver's kernel thread while 
> an image was mounted and being accessed.  It did clear the prevent_allow 
> flag, so I could eject the image.  But it also caused a 30-second delay 
> on the host, as predicted.  Now, maybe you don't care about such delays 
> when you're going to eject the media anyway, but it still seems like a 
> bad thing to do.

It looks like the prevent_medium_removal flag switching really works better in this case.
> 
> > > > I have reflected on the rest of your arguments and changed my mind.
> > > > I think that "forced_eject" for a specific lun without interrupting operations would
> > > > really be the best solution. I wrote a simple patch and tested it, everything seems
> > > > to work. What do you think about something like this?
> > > > 
> > > > 
> > > > static ssize_t fsg_lun_opts_forced_eject_store(struct config_item *item,
> > > >                                                const char *page, size_t len)
> > > > {
> > > >         struct fsg_lun_opts *opts = to_fsg_lun_opts(item);
> > > >         struct fsg_opts *fsg_opts = to_fsg_opts(opts->group.cg_item.ci_parent);
> > > >         int ret;
> > > > 
> > > >         opts->lun->prevent_medium_removal = 0;
> > > >         ret = fsg_store_file(opts->lun, &fsg_opts->common->filesem, "", 0);
> > > >         return ret < 0 ? ret : len;
> > > > }
> > > > 
> > > > CONFIGFS_ATTR_WO(fsg_lun_opts_, forced_eject);    
> > > 
> > > The basic idea is right.  But this should not be a CONFIGFS option; it 
> > > should be an ordinary LUN attribute.  For an example, see the definition of 
> > > file_store() in f_mass_storage.c; your routine should look very similar.  
> > 
> > Okay, but where this attribute is located in sysfs? How can I use it?  
> 
> Well, it's going to be in different places depending on what UDC driver 
> your gadget uses.  On my system I'm using the dummy_udc driver, so the 
> sysfs "file" attribute is located at:
> 
> 	/sys/devices/platform/dummy_ucd.0/gadget/lun0/file
> 
> If instead you're looking at
> 
> 	/sys/module/g_mass_storage/parameters/file
> 
> or in some configfs directory, that's the wrong place.  You can eject 
> the media simply by doing (as root):
> 
> 	echo >/sys/devices/.../gadget/lun0/file
> 
> (fill in the "..." appropriately for your system).
> 
> > Sorry for the stupid question.  
> 
> Not at all.

Thanks! Unfortunately I'm using dwc2 driver and it doesn't have any gadget parameters
outside of the configfs:

[root@pikvm ~]# find /sys -iname lun0
[root@pikvm ~]# find /sys -iname lun.0
/sys/kernel/config/usb_gadget/kvmd/functions/mass_storage.usb0/lun.0
[root@pikvm ~]#

So in my local case configfs is only way to place forced_eject :(
Could we add both device attrs and configfs file?

  reply	other threads:[~2022-04-09 22:42 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-06  9:24 [PATCH] usb: gadget: f_mass_storage: break IO operations via configfs Maxim Devaev
2022-04-06 15:24 ` Alan Stern
2022-04-06 16:52   ` Maxim Devaev
2022-04-06 17:51     ` Alan Stern
2022-04-06 18:36       ` Maxim Devaev
2022-04-07 16:06         ` Alan Stern
2022-04-07 17:47           ` Maxim Devaev
2022-04-08 14:59             ` Alan Stern
2022-04-09  8:57               ` Maxim Devaev
2022-04-09 13:46                 ` Alan Stern
2022-04-09 14:08                   ` Maxim Devaev
2022-04-09 20:22                     ` Alan Stern
2022-04-09 22:42                       ` Maxim Devaev [this message]
2022-04-10  1:57                         ` Alan Stern
2022-04-10  2:18                           ` Maxim Devaev
2022-04-10 15:21                             ` Alan Stern
2022-04-10 16:14                               ` Maxim Devaev

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220410014228.117d9f66@reki \
    --to=mdevaev@gmail.com \
    --cc=balbi@kernel.org \
    --cc=caihuoqing@baidu.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=stern@rowland.harvard.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.