From file to device …
Interesting question arose last week from one of the media developers at QNX working with a third party library. The API that the third party was providing to him involved him providing a file path to the library, that was then stored and passed back to him as a plugin that he was supposed to use for some specialized device access (in this case a custom CD drive handling).
The problem, other than the not so elegant API required by this third party library, was that while the library needed a filename to work with, his plugin needed to send drive level control commands in the form of custom devctl()‘s.
This reminded me of some control commands that had been put in to the block/filesystem level in the early days of Neutrino. Hunting around I found the devctl() commands I was looking for in <sys/dcmd_blk.h>:
#define DCMD_FSYS_MOUNTED_ON __DIOF(_DCMD_FSYS, 16, char) #define DCMD_FSYS_MOUNTED_AT __DIOF(_DCMD_FSYS, 17, char) #define DCMD_FSYS_MOUNTED_BY __DIOF(_DCMD_FSYS, 18, char)
When you have a file descriptor that points to something handled by of of the block oriented filesystems or block driver (ie devb-*), then you can use these handy commands to dig down or up or to determine where an entry is mounted in the filesystem.
This command will return the pathname of the underlying device, partition or file the current entry was mounted on. For example a file descriptor to a filesystem file will generally point back to a partition (or the raw block device), a partition file descriptor will point back to the raw device. In this way you can burrow your way down to the controlling device.
This command tries to root out and determine where the file descriptor you are referencing has been mounted if it is a singular mount point such as a partition. This is an upward version of MOUNTED_ON, but since multiple users can exist for a single device, it isn’t as generally useful as the MOUNTED_ON command.
This command tells you where the object that you are referencing was mounted at. This is similar to the type of information that you get back from the mount utility, but in this case the MOUNTED_AT command is a specific command for the block filesystem.
Due to the overlay nature of the Neutrino pathname space, these commands are really only applicable for file entries since directory entry resolution isn’t as explicit as for files*, but can come in handy if you are in a pinch like we were with this silly third party library API.
* We’ll leave the magic of mountpoints, overlays and the contents of the /proc/mount directory for a rain(ier) day I think.