CardSight 0.1.0
C API reference for the CardSight firmware and application
Loading...
Searching...
No Matches
vfs.h File Reference

Virtual file system: block devices, mount points, one name space. More...

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <hal/blkdev.h>

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...

Macros

#define VFS_FD_BASE   3
 First file descriptor number; 0..2 are the console.
#define VFS_NAME_MAX   255
 Longest file name, excluding the terminating zero.
#define VFS_PATH_MAX   512
 Longest path, including the terminating zero.
#define VFS_BLK_NAME   32
 Longest block device name in a mount description.
#define VFS_O_READ   0x01
 Open for reading.
#define VFS_O_WRITE   0x02
 Open for writing.
#define VFS_O_CREATE   0x04
 Make the file when it does not exist.
#define VFS_O_EXCL   0x08
 With VFS_O_CREATE: fail when it exists.
#define VFS_O_TRUNC   0x10
 With VFS_O_WRITE: drop the contents.
#define VFS_O_APPEND   0x20
 Writes go to the end.
#define VFS_FS_NODEV   0x1
 File system type flag: the type needs no block device.
#define VFS_FS_READ_ONLY   0x2
 File system type flag: every volume of this type is read only.
#define VFS_MOUNT_READ_ONLY   0x1
 Mount flag: mount read only, passed to the type's mount hook.
#define VFS_MOUNT_PROBE   0x2
 Mount flag: the mount is a probe attempt, so failures are not errors.

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.

Detailed Description

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.

Function Documentation

◆ vfs_close()

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.

Parameters
[in]descriptorDescriptor from vfs_open().
Returns
0, the type's close error, or -EBADF.

◆ vfs_closedir()

void vfs_closedir ( struct vfs_dir * dir)

Close a directory and free its wrapper.

Parameters
[in]dirDirectory from vfs_opendir(); NULL is ignored.

◆ vfs_ext_register()

int vfs_ext_register ( void )

Register the ext types: ext2, ext3, ext4.

Returns
0 on success, or the negative errno from vfs_register().

◆ vfs_ff_register()

int vfs_ff_register ( void )

Register the FatFs types: fat, fat32, exfat.

Returns
0 on success, or the negative errno from vfs_register().

◆ vfs_format()

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.

Parameters
[in]blkBlock device name.
[in]fstypeFile system name, or NULL for the first registered type.
[in]labelVolume label, may be NULL.
[in]argType-specific argument: allocation unit, or the ext2/ext3/ext4 choice.
Returns
0 on success, -ENODEV, -ENOTSUP when the type cannot format, or the formatter's error.

◆ vfs_format_expect()

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.

Parameters
[in]bdevDevice being formatted.
[in]sectorsExpected sector count; ignored when zero.

◆ vfs_format_report_write()

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().

Parameters
[in]bdevDevice being formatted.
[in]sectorsNumber of sectors just written.

◆ vfs_fstat()

int vfs_fstat ( int descriptor,
struct vfs_stat * st )

Report the metadata of an open file.

Parameters
[in]descriptorDescriptor from vfs_open().
[out]stReceives the metadata.
Returns
0, -EBADF, -ENOTSUP when the type has no fstat hook, or the type's error.

◆ vfs_fsync()

int vfs_fsync ( int descriptor)

Flush an open file to its medium.

Parameters
[in]descriptorDescriptor from vfs_open().
Returns
0, -EBADF, or the type's flush error.

◆ vfs_ftruncate()

int vfs_ftruncate ( int descriptor,
uint64_t size )

Resize an open file.

Parameters
[in]descriptorDescriptor from vfs_open().
[in]sizeNew size in bytes.
Returns
0, -EBADF, -ENOTSUP when the type has no truncate hook, or the type's error.

◆ vfs_lseek()

int64_t vfs_lseek ( int descriptor,
int64_t offset,
int whence )

Move the file position of an open descriptor.

Parameters
[in]descriptorDescriptor from vfs_open().
[in]offsetByte offset, interpreted according to whence.
[in]whence0 from the start, 1 from the current position, 2 from the end.
Returns
The new position, -EBADF, -ENOTSUP when the type has no seek hook, or the type's error.

◆ vfs_mkdir()

int vfs_mkdir ( const char * path)

Create a directory.

Parameters
[in]pathPath of the new directory.
Returns
0, -ENOENT, -ENOTSUP when the mount has no mkdir hook, or the type's error.

◆ vfs_mount()

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.

Parameters
[in]blkBlock device name, or NULL for a nodev file system.
[in]targetMount point; one level only.
[in]fstypeFile system name, or NULL to try every type.
Returns
0 on success, or the negative errno from vfs_mount_ex().

◆ vfs_mount_ex()

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.

Parameters
[in]blkBlock device name, NULL for file systems flagged VFS_FS_NODEV.
[in]targetMount point; one level only.
[in]fstypeFile system name, or NULL to probe every device-backed type.
[in]flagsVFS_MOUNT_* flags passed to the type's mount hook.
Returns
0 on success, -EINVAL, -ENAMETOOLONG, -ENODEV, -EBUSY, -ENOMEM, or the error from the file system's mount hook.

◆ vfs_mount_get()

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.

Parameters
[in]indexOrdinal among the live mounts, starting at zero.
[out]infoReceives target, device name, type name and open count.
Returns
0 on success, or -ENOENT after the last one.

◆ vfs_open()

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.

Parameters
[in]pathPath in the VFS namespace.
[in]flagsVFS_O_* access and creation flags.
Returns
A descriptor at or above VFS_FD_BASE, or a negative errno.

◆ vfs_opendir()

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.

Parameters
[in]pathDirectory path.
[out]errorReceives 0 or the negative error; may be NULL.
Returns
The directory handle, or NULL on failure.

◆ vfs_probe()

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.

Parameters
[in]blkDevice name.
[out]infoReceives type, label, sizes and the read-only flag.
Returns
0 on success, -EINVAL, -ENODEV, -EBUSY when the device is in use, or the error from the file system.

◆ vfs_read()

int vfs_read ( int descriptor,
void * buffer,
size_t length )

Read from an open descriptor.

Parameters
[in]descriptorDescriptor from vfs_open().
[out]bufferDestination buffer.
[in]lengthMaximum number of bytes to read.
Returns
The number of bytes read, or a negative errno.

◆ vfs_readdir()

int vfs_readdir ( struct vfs_dir * dir,
struct vfs_dirent * entry )

Read the next entry of an open directory.

Parameters
[in]dirDirectory from vfs_opendir().
[out]entryReceives name, metadata and the directory flag.
Returns
0 on success, -ENOENT at the end of the directory, or a negative errno.

◆ vfs_register()

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.

Parameters
[in]fsDescriptor; name and mount must be set.
Returns
0 on success, -EINVAL for a missing mandatory field, or -ENOMEM when the type table is full.

◆ vfs_rename()

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.

Parameters
[in]fromExisting path.
[in]toNew path.
Returns
0, -ENOENT, -EXDEV when both paths are on different mounts, -ENOTSUP, or the type's error.

◆ vfs_rmdir()

int vfs_rmdir ( const char * path)

Remove an empty directory.

Parameters
[in]pathPath of the directory.
Returns
0, -ENOENT, -ENOTSUP when the mount has no rmdir hook, or the type's error.

◆ vfs_set_format_progress()

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.

Parameters
[in]callbackProgress function, or NULL to disable reporting.
[in]contextOpaque pointer passed back to callback.

◆ vfs_stat()

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.

Parameters
[in]pathPath in the VFS namespace.
[out]stReceives the metadata.
Returns
0, -ENOENT, -ENOTSUP when the mount has no stat hook, or the type's error.

◆ vfs_statfs()

int vfs_statfs ( const char * path,
uint64_t * total,
uint64_t * free_bytes )

Report the capacity of the file system holding a path.

Parameters
[in]pathAny path inside the mounted file system.
[out]totalReceives the total size in bytes.
[out]free_bytesReceives the free space in bytes.
Returns
0, -ENOENT, -ENOTSUP when the type has no statfs hook, or the type's error.

◆ vfs_sync()

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.

Returns
0 on success, or the first sync error encountered.

◆ vfs_sync_mount()

int vfs_sync_mount ( const char * target)

Flush every file on one mount.

Parameters
[in]targetMount point.
Returns
0, -ENOENT for an unknown target, or the type's sync error.

◆ vfs_umount()

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.

Parameters
[in]targetMount point.
Returns
0 on success, -EINVAL for an invalid or unknown target, -EBUSY while files or directories are open below it, or the type's umount error.

◆ vfs_unlink()

int vfs_unlink ( const char * path)

Remove a file.

Parameters
[in]pathPath of the file.
Returns
0, -ENOENT, -ENOTSUP when the mount has no unlink hook, or the type's error.

◆ vfs_unregister()

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.

Parameters
[in]fsDescriptor previously passed to vfs_register().
Returns
0 on success, or -ENOENT when fs is not registered.

◆ vfs_write()

int vfs_write ( int descriptor,
const void * buffer,
size_t length )

Write to an open descriptor.

Parameters
[in]descriptorDescriptor from vfs_open().
[in]bufferSource buffer.
[in]lengthNumber of bytes to write.
Returns
The number of bytes written, -EBADF, -EROFS when the type has no write hook, or the type's error.