Skip to content

Commit 5225459

Browse files
foxengwkozaczuk
authored andcommitted
virtio-fs: minor code improvements in filesystem
These include: - Checking memory allocations - Using smart pointers where possible - Using static_cast instead of reinterpret_cast or C-style cast where possible - Formatting and consistency Signed-off-by: Fotis Xenakis <[email protected]> Message-Id: <VI1PR03MB438375635D92107C6567FFA8A6D40@VI1PR03MB4383.eurprd03.prod.outlook.com>
1 parent 360ab40 commit 5225459

File tree

4 files changed

+245
-230
lines changed

4 files changed

+245
-230
lines changed

fs/virtiofs/virtiofs.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct virtiofs_file_data {
3232
uint64_t file_handle;
3333
};
3434

35-
void virtiofs_set_vnode(struct vnode *vnode, struct virtiofs_inode *inode);
35+
void virtiofs_set_vnode(struct vnode* vnode, struct virtiofs_inode* inode);
3636

3737
extern struct vfsops virtiofs_vfsops;
3838
extern struct vnops virtiofs_vnops;

fs/virtiofs/virtiofs_i.hh

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,29 @@
1212
#include <osv/mutex.h>
1313
#include <osv/waitqueue.hh>
1414

15-
struct fuse_request
16-
{
15+
struct fuse_request {
1716
struct fuse_in_header in_header;
1817
struct fuse_out_header out_header;
1918

20-
void *input_args_data;
19+
void* input_args_data;
2120
size_t input_args_size;
2221

23-
void *output_args_data;
22+
void* output_args_data;
2423
size_t output_args_size;
2524

2625
mutex_t req_mutex;
2726
waitqueue req_wait;
2827
};
2928

3029
struct fuse_strategy {
31-
void *drv;
32-
int (*make_request)(void*, struct fuse_request*);
30+
void* drv;
31+
int (*make_request)(void*, fuse_request*);
3332
};
3433

35-
int fuse_req_send_and_receive_reply(fuse_strategy* strategy, uint32_t opcode, uint64_t nodeid,
36-
void *input_args_data, size_t input_args_size,
37-
void *output_args_data, size_t output_args_size);
34+
int fuse_req_send_and_receive_reply(fuse_strategy* strategy, uint32_t opcode,
35+
uint64_t nodeid, void* input_args_data, size_t input_args_size,
36+
void* output_args_data, size_t output_args_size);
3837

39-
void fuse_req_wait(struct fuse_request* req);
38+
void fuse_req_wait(fuse_request* req);
4039

4140
#endif

fs/virtiofs/virtiofs_vfsops.cc

Lines changed: 62 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,21 @@
1313
#include "virtiofs.hh"
1414
#include "virtiofs_i.hh"
1515

16-
static int virtiofs_mount(struct mount *mp, const char *dev, int flags, const void *data);
17-
static int virtiofs_sync(struct mount *mp);
18-
static int virtiofs_statfs(struct mount *mp, struct statfs *statp);
19-
static int virtiofs_unmount(struct mount *mp, int flags);
16+
static std::atomic<uint64_t> fuse_unique_id(1);
2017

21-
#define virtiofs_vget ((vfsop_vget_t)vfs_nullop)
22-
23-
struct vfsops virtiofs_vfsops = {
24-
virtiofs_mount, /* mount */
25-
virtiofs_unmount, /* unmount */
26-
virtiofs_sync, /* sync */
27-
virtiofs_vget, /* vget */
28-
virtiofs_statfs, /* statfs */
29-
&virtiofs_vnops /* vnops */
30-
};
31-
32-
std::atomic<uint64_t> fuse_unique_id(1);
33-
34-
int fuse_req_send_and_receive_reply(fuse_strategy* strategy, uint32_t opcode, uint64_t nodeid,
35-
void *input_args_data, size_t input_args_size, void *output_args_data, size_t output_args_size)
18+
int fuse_req_send_and_receive_reply(fuse_strategy* strategy, uint32_t opcode,
19+
uint64_t nodeid, void* input_args_data, size_t input_args_size,
20+
void* output_args_data, size_t output_args_size)
3621
{
37-
auto *req = new (std::nothrow) fuse_request();
38-
39-
req->in_header.len = 0; //TODO
22+
std::unique_ptr<fuse_request> req {new (std::nothrow) fuse_request()};
23+
if (!req) {
24+
return ENOMEM;
25+
}
26+
req->in_header.len = sizeof(req->in_header) + input_args_size;
4027
req->in_header.opcode = opcode;
41-
req->in_header.unique = fuse_unique_id.fetch_add(1, std::memory_order_relaxed);
28+
req->in_header.unique = fuse_unique_id.fetch_add(1,
29+
std::memory_order_relaxed);
4230
req->in_header.nodeid = nodeid;
43-
req->in_header.uid = 0;
44-
req->in_header.gid = 0;
45-
req->in_header.pid = 0;
4631

4732
req->input_args_data = input_args_data;
4833
req->input_args_size = input_args_size;
@@ -51,18 +36,17 @@ int fuse_req_send_and_receive_reply(fuse_strategy* strategy, uint32_t opcode, ui
5136
req->output_args_size = output_args_size;
5237

5338
assert(strategy->drv);
54-
strategy->make_request(strategy->drv, req);
55-
fuse_req_wait(req);
39+
strategy->make_request(strategy->drv, req.get());
40+
fuse_req_wait(req.get());
5641

5742
int error = -req->out_header.error;
58-
delete req;
5943

6044
return error;
6145
}
6246

63-
void virtiofs_set_vnode(struct vnode *vnode, struct virtiofs_inode *inode)
47+
void virtiofs_set_vnode(struct vnode* vnode, struct virtiofs_inode* inode)
6448
{
65-
if (vnode == nullptr || inode == nullptr) {
49+
if (!vnode || !inode) {
6650
return;
6751
}
6852

@@ -82,81 +66,85 @@ void virtiofs_set_vnode(struct vnode *vnode, struct virtiofs_inode *inode)
8266
vnode->v_size = inode->attr.size;
8367
}
8468

85-
static int
86-
virtiofs_mount(struct mount *mp, const char *dev, int flags, const void *data) {
87-
struct device *device;
88-
int error = -1;
69+
static int virtiofs_mount(struct mount* mp, const char* dev, int flags,
70+
const void* data)
71+
{
72+
struct device* device;
8973

90-
error = device_open(dev + 5, DO_RDWR, &device);
74+
int error = device_open(dev + strlen("/dev/"), DO_RDWR, &device);
9175
if (error) {
9276
kprintf("[virtiofs] Error opening device!\n");
9377
return error;
9478
}
9579

96-
mp->m_dev = device;
97-
98-
auto *in_args = new(std::nothrow) fuse_init_in();
80+
std::unique_ptr<fuse_init_in> in_args {new (std::nothrow) fuse_init_in()};
81+
std::unique_ptr<fuse_init_out> out_args {new (std::nothrow) fuse_init_out};
82+
if (!in_args || !out_args) {
83+
return ENOMEM;
84+
}
9985
in_args->major = FUSE_KERNEL_VERSION;
10086
in_args->minor = FUSE_KERNEL_MINOR_VERSION;
10187
in_args->max_readahead = PAGE_SIZE;
102-
in_args->flags = 0; //TODO Investigate which flags to set
103-
104-
auto *out_args = new(std::nothrow) fuse_init_out();
88+
in_args->flags = 0; // TODO: Verify that we need not set any flag
10589

106-
auto *strategy = reinterpret_cast<fuse_strategy *>(device->private_data);
90+
auto* strategy = static_cast<fuse_strategy*>(device->private_data);
10791
error = fuse_req_send_and_receive_reply(strategy, FUSE_INIT, FUSE_ROOT_ID,
108-
in_args, sizeof(*in_args), out_args, sizeof(*out_args));
109-
110-
if (!error) {
111-
virtiofs_debug("Initialized fuse filesystem with version major: %d, minor: %d\n",
112-
out_args->major, out_args->minor);
113-
114-
auto *root_node = new virtiofs_inode();
115-
root_node->nodeid = FUSE_ROOT_ID;
116-
root_node->attr.mode = S_IFDIR;
92+
in_args.get(), sizeof(*in_args), out_args.get(), sizeof(*out_args));
93+
if (error) {
94+
kprintf("[virtiofs] Failed to initialize fuse filesystem!\n");
95+
return error;
96+
}
97+
// TODO: Handle version negotiation
11798

118-
virtiofs_set_vnode(mp->m_root->d_vnode, root_node);
99+
virtiofs_debug("Initialized fuse filesystem with version major: %d, "
100+
"minor: %d\n", out_args->major, out_args->minor);
119101

120-
mp->m_data = strategy;
121-
mp->m_dev = device;
122-
} else {
123-
kprintf("[virtiofs] Failed to initialized fuse filesystem!\n");
102+
auto* root_node {new (std::nothrow) virtiofs_inode()};
103+
if (!root_node) {
104+
return ENOMEM;
124105
}
106+
root_node->nodeid = FUSE_ROOT_ID;
107+
root_node->attr.mode = S_IFDIR;
125108

126-
delete out_args;
127-
delete in_args;
109+
virtiofs_set_vnode(mp->m_root->d_vnode, root_node);
128110

129-
return error;
130-
}
111+
mp->m_data = strategy;
112+
mp->m_dev = device;
131113

132-
static int virtiofs_sync(struct mount *mp) {
133114
return 0;
134115
}
135116

136-
static int virtiofs_statfs(struct mount *mp, struct statfs *statp)
117+
static int virtiofs_sync(struct mount* mp)
137118
{
138-
//TODO
139-
//struct virtiofs_info *virtiofs = (struct virtiofs_info *) mp->m_data;
119+
return 0;
120+
}
140121

141-
//statp->f_bsize = sb->block_size;
122+
static int virtiofs_statfs(struct mount* mp, struct statfs* statp)
123+
{
124+
// TODO: Call FUSE_STATFS
142125

143-
// Total blocks
144-
//statp->f_blocks = sb->structure_info_blocks_count + sb->structure_info_first_block;
145126
// Read only. 0 blocks free
146127
statp->f_bfree = 0;
147128
statp->f_bavail = 0;
148129

149130
statp->f_ffree = 0;
150-
//statp->f_files = sb->inodes_count; //Needs to be inode count
151-
152-
statp->f_namelen = 0; //FIXME
153131

154132
return 0;
155133
}
156134

157-
static int
158-
virtiofs_unmount(struct mount *mp, int flags)
135+
static int virtiofs_unmount(struct mount* mp, int flags)
159136
{
160-
struct device *dev = mp->m_dev;
137+
struct device* dev = mp->m_dev;
161138
return device_close(dev);
162139
}
140+
141+
#define virtiofs_vget ((vfsop_vget_t)vfs_nullop)
142+
143+
struct vfsops virtiofs_vfsops = {
144+
virtiofs_mount, /* mount */
145+
virtiofs_unmount, /* unmount */
146+
virtiofs_sync, /* sync */
147+
virtiofs_vget, /* vget */
148+
virtiofs_statfs, /* statfs */
149+
&virtiofs_vnops /* vnops */
150+
};

0 commit comments

Comments
 (0)