|
CardSight 0.1.0
C API reference for the CardSight firmware and application
|
Virtual file system: block devices, mount points, one name space. More...
Go to the source code of this file.
Data Structures | |
| struct | vfs_stat |
| Metadata of a file or directory. More... | |
| struct | vfs_dirent |
| One directory entry as returned by vfs_readdir(). More... | |
| struct | vfs_volume_info |
| Description of a volume found by vfs_probe(). More... | |
| struct | vfs_fs |
| Operations a file system type provides to the VFS. More... | |
| struct | vfs_mount_info |
| One entry of the mount table. More... | |
Functions | |
| int | vfs_register (const struct vfs_fs *fs) |
| Add a file system type to the VFS. | |
| int | vfs_unregister (const struct vfs_fs *fs) |
| Remove a file system type from the VFS. | |
| int | vfs_mount (const char *blk, const char *target, const char *fstype) |
| Mount a block device or a nodev file system at a target with default flags. | |
| int | vfs_mount_ex (const char *blk, const char *target, const char *fstype, uint32_t flags) |
| Mount a block device or a nodev file system at a target. | |
| int | vfs_umount (const char *target) |
| Unmount the file system at a target. | |
| int | vfs_format (const char *blk, const char *fstype, const char *label, uint32_t arg) |
| Make a new file system on an unmounted block device. | |
| void | vfs_set_format_progress (void(*callback)(void *context, uint64_t done, uint64_t total), void *context) |
| Observe vfs_format() progress. | |
| void | vfs_format_expect (struct block_device *bdev, uint64_t sectors) |
| Declare how many sectors the running format will write. | |
| void | vfs_format_report_write (struct block_device *bdev, uint64_t sectors) |
| Report sectors written by a formatter. | |
| int | vfs_probe (const char *blk, struct vfs_volume_info *info) |
| Probe an unmounted block device. | |
| int | vfs_open (const char *path, uint32_t flags) |
| Open a file and return a VFS descriptor. | |
| int | vfs_read (int descriptor, void *buffer, size_t length) |
| Read from an open descriptor. | |
| int | vfs_write (int descriptor, const void *buffer, size_t length) |
| Write to an open descriptor. | |
| int64_t | vfs_lseek (int descriptor, int64_t offset, int whence) |
| Move the file position of an open descriptor. | |
| int | vfs_close (int descriptor) |
| Close a descriptor. | |
| int | vfs_fstat (int descriptor, struct vfs_stat *st) |
| Report the metadata of an open file. | |
| int | vfs_ftruncate (int descriptor, uint64_t size) |
| Resize an open file. | |
| int | vfs_fsync (int descriptor) |
| Flush an open file to its medium. | |
| int | vfs_sync_mount (const char *target) |
| Flush every file on one mount. | |
| int | vfs_stat (const char *path, struct vfs_stat *st) |
| Report the metadata of a path. | |
| int | vfs_statfs (const char *path, uint64_t *total, uint64_t *free_bytes) |
| Report the capacity of the file system holding a path. | |
| int | vfs_mkdir (const char *path) |
| Create a directory. | |
| int | vfs_unlink (const char *path) |
| Remove a file. | |
| int | vfs_rmdir (const char *path) |
| Remove an empty directory. | |
| int | vfs_rename (const char *from, const char *to) |
| Rename or move an entry. | |
| struct vfs_dir * | vfs_opendir (const char *path, int *error) |
| Open a directory. | |
| int | vfs_readdir (struct vfs_dir *dir, struct vfs_dirent *entry) |
| Read the next entry of an open directory. | |
| void | vfs_closedir (struct vfs_dir *dir) |
| Close a directory and free its wrapper. | |
| int | vfs_sync (void) |
| Write everything out on every mount. | |
| int | vfs_mount_get (uint32_t index, struct vfs_mount_info *info) |
| Report one mount table entry by ordinal. | |
| void | vfs_os_lock (void) |
| Take the recursive VFS lock, waiting without a bound. | |
| void | vfs_os_unlock (void) |
| Release the recursive VFS lock. | |
| int | vfs_ff_register (void) |
| Register the FatFs types: fat, fat32, exfat. | |
| int | vfs_ext_register (void) |
| Register the ext types: ext2, ext3, ext4. | |
Virtual file system: block devices, mount points, one name space.
A file system type (FAT, ext) is mounted from a block device at a mount point "/name"; paths then go through the mount table to the file system. Absolute paths start at the root, "/" lists the mount points when nothing is mounted there. File descriptors are numbers from VFS_FD_BASE up; the C library's open/read/fopen use them. All calls return a negative errno on failure.
One lock serialises the calls except the long ones: a format and a probe claim the device instead, so they do not stop every other call.
| int vfs_close | ( | int | descriptor | ) |
Close a descriptor.
The slot is released and the mount's open count decremented even when the close hook reports an error, so a failing medium cannot leak descriptors.
| [in] | descriptor | Descriptor from vfs_open(). |
| void vfs_closedir | ( | struct vfs_dir * | dir | ) |
Close a directory and free its wrapper.
| [in] | dir | Directory from vfs_opendir(); NULL is ignored. |
| int vfs_ext_register | ( | void | ) |
Register the ext types: ext2, ext3, ext4.
| int vfs_ff_register | ( | void | ) |
Register the FatFs types: fat, fat32, exfat.
| int vfs_format | ( | const char * | blk, |
| const char * | fstype, | ||
| const char * | label, | ||
| uint32_t | arg ) |
Make a new file system on an unmounted block device.
A format is minutes of card I/O, so the VFS lock is not taken; the block device is opened and claimed instead, which keeps mounts out. The format progress state is armed around the type's format hook. The old contents are lost.
| [in] | blk | Block device name. |
| [in] | fstype | File system name, or NULL for the first registered type. |
| [in] | label | Volume label, may be NULL. |
| [in] | arg | Type-specific argument: allocation unit, or the ext2/ext3/ext4 choice. |
| void vfs_format_expect | ( | struct block_device * | bdev, |
| uint64_t | sectors ) |
Declare how many sectors the running format will write.
For file system drivers. A formatter announces how many sectors it is about to write, then reports every write; the value is ignored unless a format of bdev is in progress.
| [in] | bdev | Device being formatted. |
| [in] | sectors | Expected sector count; ignored when zero. |
| void vfs_format_report_write | ( | struct block_device * | bdev, |
| uint64_t | sectors ) |
Report sectors written by a formatter.
For file system drivers. Updates the approximate progress with a high-water mark and does nothing outside vfs_format().
| [in] | bdev | Device being formatted. |
| [in] | sectors | Number of sectors just written. |
| int vfs_fstat | ( | int | descriptor, |
| struct vfs_stat * | st ) |
Report the metadata of an open file.
| [in] | descriptor | Descriptor from vfs_open(). |
| [out] | st | Receives the metadata. |
| int vfs_fsync | ( | int | descriptor | ) |
Flush an open file to its medium.
| [in] | descriptor | Descriptor from vfs_open(). |
| int vfs_ftruncate | ( | int | descriptor, |
| uint64_t | size ) |
Resize an open file.
| [in] | descriptor | Descriptor from vfs_open(). |
| [in] | size | New size in bytes. |
| int64_t vfs_lseek | ( | int | descriptor, |
| int64_t | offset, | ||
| int | whence ) |
Move the file position of an open descriptor.
| [in] | descriptor | Descriptor from vfs_open(). |
| [in] | offset | Byte offset, interpreted according to whence. |
| [in] | whence | 0 from the start, 1 from the current position, 2 from the end. |
| int vfs_mkdir | ( | const char * | path | ) |
Create a directory.
| [in] | path | Path of the new directory. |
| int vfs_mount | ( | const char * | blk, |
| const char * | target, | ||
| const char * | fstype ) |
Mount a block device or a nodev file system at a target with default flags.
| [in] | blk | Block device name, or NULL for a nodev file system. |
| [in] | target | Mount point; one level only. |
| [in] | fstype | File system name, or NULL to try every type. |
| int vfs_mount_ex | ( | const char * | blk, |
| const char * | target, | ||
| const char * | fstype, | ||
| uint32_t | flags ) |
Mount a block device or a nodev file system at a target.
Under the VFS lock: the target is normalised and rejected if deeper than one level or too long. A named type is looked up; unless it is a nodev type, the block device is opened (512-byte sectors only), a duplicate target is refused, and the device is claimed so a second mount cannot take it. A free slot is reserved, the mount hook runs, and a successful mount is recorded and sorted into the lookup table. Any failure releases the claim and closes the device.
| [in] | blk | Block device name, NULL for file systems flagged VFS_FS_NODEV. |
| [in] | target | Mount point; one level only. |
| [in] | fstype | File system name, or NULL to probe every device-backed type. |
| [in] | flags | VFS_MOUNT_* flags passed to the type's mount hook. |
| int vfs_mount_get | ( | uint32_t | index, |
| struct vfs_mount_info * | info ) |
Report one mount table entry by ordinal.
The slots are walked in order, counting only live mounts.
| [in] | index | Ordinal among the live mounts, starting at zero. |
| [out] | info | Receives target, device name, type name and open count. |
| int vfs_open | ( | const char * | path, |
| uint32_t | flags ) |
Open a file and return a VFS descriptor.
Under the lock: resolves the path, calls the type's open hook, then takes the first free file slot. With every slot busy the handle is closed again and -EMFILE returned. VFS_O_APPEND seeks to the end once here, so later writes append.
| [in] | path | Path in the VFS namespace. |
| [in] | flags | VFS_O_* access and creation flags. |
| struct vfs_dir * vfs_opendir | ( | const char * | path, |
| int * | error ) |
Open a directory.
"/" with nothing mounted there produces a synthetic directory whose readdir() lists the mount points. Otherwise the type's opendir hook runs and its handle is wrapped in a heap object so it can outlive the lock.
| [in] | path | Directory path. |
| [out] | error | Receives 0 or the negative error; may be NULL. |
| int vfs_probe | ( | const char * | blk, |
| struct vfs_volume_info * | info ) |
Probe an unmounted block device.
The device is opened and claimed, then each registered device-backed type is tried with a read-only mount until one succeeds; the probe mounts the volume briefly, reads its description, and unmounts it again.
| [in] | blk | Device name. |
| [out] | info | Receives type, label, sizes and the read-only flag. |
| int vfs_read | ( | int | descriptor, |
| void * | buffer, | ||
| size_t | length ) |
Read from an open descriptor.
| [in] | descriptor | Descriptor from vfs_open(). |
| [out] | buffer | Destination buffer. |
| [in] | length | Maximum number of bytes to read. |
| int vfs_readdir | ( | struct vfs_dir * | dir, |
| struct vfs_dirent * | entry ) |
Read the next entry of an open directory.
| [in] | dir | Directory from vfs_opendir(). |
| [out] | entry | Receives name, metadata and the directory flag. |
| int vfs_register | ( | const struct vfs_fs * | fs | ) |
Add a file system type to the VFS.
Registration is idempotent: an already registered pointer returns 0. The lock is held while the type table changes.
| [in] | fs | Descriptor; name and mount must be set. |
| int vfs_rename | ( | const char * | from, |
| const char * | to ) |
Rename or move an entry.
Both paths are resolved under the lock; a rename never crosses a mount boundary.
| [in] | from | Existing path. |
| [in] | to | New path. |
| int vfs_rmdir | ( | const char * | path | ) |
Remove an empty directory.
| [in] | path | Path of the directory. |
| void vfs_set_format_progress | ( | void(* | callback )(void *context, uint64_t done, uint64_t total), |
| void * | context ) |
Observe vfs_format() progress.
The callback gets the highest reported sector and the device size, from the thread that formats. This is an approximate position indicator.
| [in] | callback | Progress function, or NULL to disable reporting. |
| [in] | context | Opaque pointer passed back to callback. |
| int vfs_stat | ( | const char * | path, |
| struct vfs_stat * | st ) |
Report the metadata of a path.
The root and any mount point itself report as a directory without consulting a file system; every other path is resolved and handed to the mount's stat hook.
| [in] | path | Path in the VFS namespace. |
| [out] | st | Receives the metadata. |
| int vfs_statfs | ( | const char * | path, |
| uint64_t * | total, | ||
| uint64_t * | free_bytes ) |
Report the capacity of the file system holding a path.
| [in] | path | Any path inside the mounted file system. |
| [out] | total | Receives the total size in bytes. |
| [out] | free_bytes | Receives the free space in bytes. |
| int vfs_sync | ( | void | ) |
Write everything out on every mount.
All mounts are visited even after a failure, so one bad medium does not leave the others dirty; only the first error is reported.
| int vfs_sync_mount | ( | const char * | target | ) |
Flush every file on one mount.
| [in] | target | Mount point. |
| int vfs_umount | ( | const char * | target | ) |
Unmount the file system at a target.
Under the lock: validates the target, refuses while files or directories are open below it, runs the umount hook, releases the device claim and closes it, then frees the slot.
| [in] | target | Mount point. |
| int vfs_unlink | ( | const char * | path | ) |
Remove a file.
| [in] | path | Path of the file. |
| int vfs_unregister | ( | const struct vfs_fs * | fs | ) |
Remove a file system type from the VFS.
The table is compacted over the hole, because lookups stop at the first NULL slot.
| [in] | fs | Descriptor previously passed to vfs_register(). |
fs is not registered. | int vfs_write | ( | int | descriptor, |
| const void * | buffer, | ||
| size_t | length ) |
Write to an open descriptor.
| [in] | descriptor | Descriptor from vfs_open(). |
| [in] | buffer | Source buffer. |
| [in] | length | Number of bytes to write. |