1

I have a system which is a USB gadget. It exposes one of its partitions over USB using functionfs to allow the host to provide update files:

set -e
CONFIGFS_ROOT=/dev/gadget
CONFIGFS_DEV=$CONFIGFS_ROOT/usb_gadget/device

mkdir -p $CONFIGFS_ROOT
mount -t configfs none $CONFIGFS_ROOT
mkdir $CONFIGFS_DEV
cd $CONFIGFS_DEV

### (device-level strings etc. omitted for brevity) ###

# Set up a config
mkdir configs/mass_storage.1
mkdir configs/mass_storage.1/strings/0x409
echo "Mass-storage configuration" >configs/mass_storage.1/strings/0x409/configuration
echo 100 >configs/mass_storage.1/MaxPower

# Add the function and connect our partition
mkdir functions/mass_storage.usb
readlink -e "/dev/disk/by-label/data" >functions/mass_storage.usb/lun.0/file

# Associate the function with the first config
ln -s functions/mass_storage.usb configs/mass_storage.1

This works nicely the first time the host mounts it and writes its data (using VFAT filesystem) - we can then mount it somewhere on the device and use it.

But if we write our reply and the host sends us more updates, then I see data corruption, and I'm presuming that's caused by the USB mass-storage interface modifying the partition underneath the block cache layer.

I take care to ensure that the two sides don't simultaneously mount the filesystem, and the host side uses eject to tell it that it's being removed and possibly written elsewhere (as a USB mass-storage it's automatically a removable device). But on the device side, the partition is local (flash) storage and not removable.

Is there anything I can do on the device to let it know that the storage may have been written by the mass-storage gadget function, and that it can't assume its cached blocks are correct?

6
  • 1
    Flushing the VFS (echo 1 > /proc/sys/vm/drop_caches) may suffice - but it's a sledgehammer to crack a nut. Commented Oct 13, 2023 at 11:17
  • Can you umount the local flash? Commented Oct 13, 2023 at 11:22
  • @Ole It is unmounted, when it's available to the host (sorry if I wasn't clear enough with "I take care to ensure that the two sides don't simultaneously mount the filesystem". Commented Oct 13, 2023 at 11:35
  • @symcbean, I was looking for something like that for a single device. But it may be that invalidating the whole virtual-memory cache is as close as we can get. Thanks for the suggestion (I was looking in /sys/block/… for controls, and forgot about /proc/sys/). Commented Oct 13, 2023 at 11:42
  • I usually use sync when I want my cache to be written. It even has some parameters. You should be able to specify file system or file bo be synced. Commented Oct 13, 2023 at 12:17

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.