PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
superblock.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/fs/sysfs.h>
4#include <kernel/utils/ref.h>
5
6#include <stdint.h>
7#include <sys/io.h>
8#include <sys/list.h>
9
10typedef struct filesystem filesystem_t;
11typedef struct superblock superblock_t;
12typedef struct superblock_ops superblock_ops_t;
13typedef struct dentry_ops dentry_ops_t;
14typedef struct inode inode_t;
15typedef struct dentry dentry_t;
16
17/**
18 * @brief Mountable filesystem.
19 * @defgroup kernel_fs_superblock Superblock
20 * @ingroup kernel_fs
21 *
22 * A superblock represents a mounted filesystem, it can be thought of as "filesystem + device". The filesystem is
23 * just the format of the data, e.g. fat32, ramfs, sysfs, etc. and the device provides the data. The superblock is the
24 * combination of both, e.g. a fat32 filesystem on /dev/sda1.
25 *
26 * In the case of certain special filesystems like ramfs or sysfs there is no underlying device, in this case
27 * the device name is simply set to `VFS_DEVICE_NAME_NONE`.
28 *
29 * @{
30 */
31
32/**
33 * @brief Superblock ID type.
34 */
36
37/**
38 * @brief Superblock structure.
39 * @struct superblock_t
40 *
41 * Superblocks are owned by the VFS, not the filesystem.
42 */
43typedef struct superblock
44{
50 void* private;
55 char deviceName[MAX_NAME];
56 /**
57 * The number of mounts of this superblock.
58 *
59 * Note that this does need to be separate from the reference count as a superblock is referenced by mounts,
60 * but it can also be referenced by other things like open files.
61 */
62 atomic_uint64_t mountCount;
64
65/**
66 * @brief Superblock operations structure.
67 * @struct superblock_ops_t
68 */
69typedef struct superblock_ops
70{
71 /**
72 * Called when the VFS needs to create a new inode, if not specified `heap_alloc()` is used.
73 * This is usefull as it lets filesystems allocate a structure larget than `inode_t` and use the additional
74 * space for private data in addition to the `private` pointer in `inode_t`.
75 */
76 inode_t* (*allocInode)(superblock_t* superblock);
77 /**
78 * Called when the VFS wants to free an inode, if not specified `free()` is used.
79 */
80 void (*freeInode)(superblock_t* superblock, inode_t* inode);
81 /**
82 * Called when the filesystem is superblock is being freed to give the filesystem a chance to clean up any private
83 * data.
84 */
85 void (*cleanup)(superblock_t* superblock);
86 /**
87 * Called when the the superblocks `mountCount` reaches zero, meaning it is not visible anywhere in any namespace.
88 */
89 void (*unmount)(superblock_t* superblock);
91
92/**
93 * @brief Create a new superblock.
94 *
95 * This does not add the superblock to the superblock cache, the `vfs_mount()` function will do that using
96 * `vfs_add_superblock()`.
97 *
98 * There is no `superblock_free()` instead use `UNREF()`.
99 *
100 * Note that the superblock's `root` dentry must be created and assigned after calling this function.
101 *
102 * @param fs The filesystem type of the superblock.
103 * @param deviceName The device name, or `VFS_DEVICE_NAME_NONE` for no device.
104 * @param ops The superblock operations, can be NULL.
105 * @param dentryOps The dentry operations for dentries in this superblock, can be NULL.
106 * @return On success, the new superblock. On failure, returns `NULL` and `errno` is set.
107 */
108superblock_t* superblock_new(filesystem_t* fs, const char* deviceName, const superblock_ops_t* ops,
109 const dentry_ops_t* dentryOps);
110
111/**
112 * @brief Increment the mount count of a superblock.
113 *
114 * @param superblock Pointer to the superblock.
115 */
117
118/**
119 * @brief Decrement the mount count of a superblock.
120 *
121 * If the mount count reaches zero, the `unmount` operation is called if its not `NULL`.
122 *
123 * @param superblock Pointer to the superblock.
124 */
126
127/** @} */
#define MAX_NAME
Maximum length of names.
Definition MAX_NAME.h:11
void superblock_inc_mount_count(superblock_t *superblock)
Increment the mount count of a superblock.
Definition superblock.c:60
uint64_t superblock_id_t
Superblock ID type.
Definition superblock.h:35
superblock_t * superblock_new(filesystem_t *fs, const char *deviceName, const superblock_ops_t *ops, const dentry_ops_t *dentryOps)
Create a new superblock.
Definition superblock.c:35
void superblock_dec_mount_count(superblock_t *superblock)
Decrement the mount count of a superblock.
Definition superblock.c:65
static socket_family_ops_t ops
Definition local.c:505
static dentry_ops_t dentryOps
Definition ramfs.c:181
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
Dentry operations structure.
Definition dentry.h:72
Directory entry structure.
Definition dentry.h:84
Filesystem structure, represents a filesystem type, e.g. fat32, ramfs, sysfs, etc.
Definition filesystem.h:31
Inode structure.
Definition inode.h:56
A entry in a doubly linked list.
Definition list.h:36
Reference counting structure.
Definition ref.h:30
Superblock operations structure.
Definition superblock.h:70
Superblock structure.
Definition superblock.h:44
uint64_t blockSize
Definition superblock.h:48
uint64_t maxFileSize
Definition superblock.h:49
const superblock_ops_t * ops
Definition superblock.h:52
atomic_uint64_t mountCount
Definition superblock.h:62
filesystem_t * fs
Definition superblock.h:54
list_entry_t entry
Definition superblock.h:46
dentry_t * root
Definition superblock.h:51
const dentry_ops_t * dentryOps
Definition superblock.h:53
superblock_id_t id
Definition superblock.h:47