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?
/sys/block/…for controls, and forgot about/proc/sys/).syncwhen 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.