13
13
#include " virtiofs.hh"
14
14
#include " virtiofs_i.hh"
15
15
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 );
20
17
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)
36
21
{
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;
40
27
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);
42
30
req->in_header .nodeid = nodeid;
43
- req->in_header .uid = 0 ;
44
- req->in_header .gid = 0 ;
45
- req->in_header .pid = 0 ;
46
31
47
32
req->input_args_data = input_args_data;
48
33
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
51
36
req->output_args_size = output_args_size;
52
37
53
38
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 () );
56
41
57
42
int error = -req->out_header .error ;
58
- delete req;
59
43
60
44
return error;
61
45
}
62
46
63
- void virtiofs_set_vnode (struct vnode * vnode, struct virtiofs_inode * inode)
47
+ void virtiofs_set_vnode (struct vnode * vnode, struct virtiofs_inode * inode)
64
48
{
65
- if (vnode == nullptr || inode == nullptr ) {
49
+ if (! vnode || ! inode) {
66
50
return ;
67
51
}
68
52
@@ -82,81 +66,85 @@ void virtiofs_set_vnode(struct vnode *vnode, struct virtiofs_inode *inode)
82
66
vnode->v_size = inode->attr .size ;
83
67
}
84
68
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 ;
89
73
90
- error = device_open (dev + 5 , DO_RDWR, &device);
74
+ int error = device_open (dev + strlen ( " /dev/ " ) , DO_RDWR, &device);
91
75
if (error) {
92
76
kprintf (" [virtiofs] Error opening device!\n " );
93
77
return error;
94
78
}
95
79
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
+ }
99
85
in_args->major = FUSE_KERNEL_VERSION;
100
86
in_args->minor = FUSE_KERNEL_MINOR_VERSION;
101
87
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
105
89
106
- auto * strategy = reinterpret_cast <fuse_strategy *>(device->private_data );
90
+ auto * strategy = static_cast <fuse_strategy*>(device->private_data );
107
91
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
117
98
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 );
119
101
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;
124
105
}
106
+ root_node->nodeid = FUSE_ROOT_ID;
107
+ root_node->attr .mode = S_IFDIR;
125
108
126
- delete out_args;
127
- delete in_args;
109
+ virtiofs_set_vnode (mp->m_root ->d_vnode , root_node);
128
110
129
- return error ;
130
- }
111
+ mp-> m_data = strategy ;
112
+ mp-> m_dev = device;
131
113
132
- static int virtiofs_sync (struct mount *mp) {
133
114
return 0 ;
134
115
}
135
116
136
- static int virtiofs_statfs (struct mount *mp, struct statfs *statp )
117
+ static int virtiofs_sync (struct mount * mp )
137
118
{
138
- // TODO
139
- // struct virtiofs_info *virtiofs = (struct virtiofs_info *) mp->m_data;
119
+ return 0 ;
120
+ }
140
121
141
- // statp->f_bsize = sb->block_size;
122
+ static int virtiofs_statfs (struct mount * mp, struct statfs * statp)
123
+ {
124
+ // TODO: Call FUSE_STATFS
142
125
143
- // Total blocks
144
- // statp->f_blocks = sb->structure_info_blocks_count + sb->structure_info_first_block;
145
126
// Read only. 0 blocks free
146
127
statp->f_bfree = 0 ;
147
128
statp->f_bavail = 0 ;
148
129
149
130
statp->f_ffree = 0 ;
150
- // statp->f_files = sb->inodes_count; //Needs to be inode count
151
-
152
- statp->f_namelen = 0 ; // FIXME
153
131
154
132
return 0 ;
155
133
}
156
134
157
- static int
158
- virtiofs_unmount (struct mount *mp, int flags)
135
+ static int virtiofs_unmount (struct mount * mp, int flags)
159
136
{
160
- struct device * dev = mp->m_dev ;
137
+ struct device * dev = mp->m_dev ;
161
138
return device_close (dev);
162
139
}
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