Skip to content

Commit 360ab40

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

File tree

2 files changed

+52
-43
lines changed

2 files changed

+52
-43
lines changed

drivers/virtio-fs.cc

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
using namespace memory;
3030

31-
void fuse_req_wait(struct fuse_request* req)
31+
void fuse_req_wait(fuse_request* req)
3232
{
3333
WITH_LOCK(req->req_mutex) {
3434
req->req_wait.wait(req->req_mutex);
@@ -37,37 +37,37 @@ void fuse_req_wait(struct fuse_request* req)
3737

3838
namespace virtio {
3939

40-
static int fuse_make_request(void *driver, struct fuse_request* req)
40+
static int fuse_make_request(void* driver, fuse_request* req)
4141
{
42-
auto fs_driver = reinterpret_cast<fs*>(driver);
42+
auto fs_driver = static_cast<fs*>(driver);
4343
return fs_driver->make_request(req);
4444
}
4545

46-
static void fuse_req_done(struct fuse_request* req)
46+
static void fuse_req_done(fuse_request* req)
4747
{
4848
WITH_LOCK(req->req_mutex) {
4949
req->req_wait.wake_one(req->req_mutex);
5050
}
5151
}
5252

53-
static void fuse_req_enqueue_input(vring* queue, struct fuse_request* req)
53+
static void fuse_req_enqueue_input(vring* queue, fuse_request* req)
5454
{
5555
// Header goes first
5656
queue->add_out_sg(&req->in_header, sizeof(struct fuse_in_header));
57-
//
57+
5858
// Add fuse in arguments as out sg
59-
if (req->input_args_size) {
59+
if (req->input_args_size > 0) {
6060
queue->add_out_sg(req->input_args_data, req->input_args_size);
6161
}
6262
}
6363

64-
static void fuse_req_enqueue_output(vring* queue, struct fuse_request* req)
64+
static void fuse_req_enqueue_output(vring* queue, fuse_request* req)
6565
{
6666
// Header goes first
6767
queue->add_in_sg(&req->out_header, sizeof(struct fuse_out_header));
68-
//
68+
6969
// Add fuse out arguments as in sg
70-
if (req->output_args_size) {
70+
if (req->output_args_size > 0) {
7171
queue->add_in_sg(req->output_args_data, req->output_args_size);
7272
}
7373
}
@@ -93,14 +93,13 @@ struct driver fs_driver = {
9393
bool fs::ack_irq()
9494
{
9595
auto isr = _dev.read_and_ack_isr();
96-
auto queue = get_virt_queue(VQ_REQUEST);
96+
auto* queue = get_virt_queue(VQ_REQUEST);
9797

9898
if (isr) {
9999
queue->disable_interrupts();
100100
return true;
101-
} else {
102-
return false;
103101
}
102+
return false;
104103
}
105104

106105
fs::fs(virtio_device& virtio_dev)
@@ -113,7 +112,6 @@ fs::fs(virtio_device& virtio_dev)
113112
// Steps 4, 5 & 6 - negotiate and confirm features
114113
setup_features();
115114
read_config();
116-
117115
if (_config.num_queues < 1) {
118116
virtio_i("Expected at least one request queue -> baling out!\n");
119117
return;
@@ -122,29 +120,32 @@ fs::fs(virtio_device& virtio_dev)
122120
// Step 7 - generic init of virtqueues
123121
probe_virt_queues();
124122

125-
//register the single irq callback for the block
123+
// register the single irq callback for the block
126124
sched::thread* t = sched::thread::make([this] { this->req_done(); },
127-
sched::thread::attr().name("virtio-fs"));
125+
sched::thread::attr().name("virtio-fs"));
128126
t->start();
129-
auto queue = get_virt_queue(VQ_REQUEST);
127+
auto* queue = get_virt_queue(VQ_REQUEST);
130128

131129
interrupt_factory int_factory;
132-
int_factory.register_msi_bindings = [queue, t](interrupt_manager &msi) {
133-
msi.easy_register( {{ VQ_REQUEST, [=] { queue->disable_interrupts(); }, t }});
130+
int_factory.register_msi_bindings = [queue, t](interrupt_manager& msi) {
131+
msi.easy_register({
132+
{VQ_HIPRIO, nullptr, nullptr},
133+
{VQ_REQUEST, [=] { queue->disable_interrupts(); }, t}
134+
});
134135
};
135136

136-
int_factory.create_pci_interrupt = [this,t](pci::device &pci_dev) {
137+
int_factory.create_pci_interrupt = [this, t](pci::device& pci_dev) {
137138
return new pci_interrupt(
138139
pci_dev,
139140
[=] { return this->ack_irq(); },
140141
[=] { t->wake(); });
141142
};
142143

143144
#ifndef AARCH64_PORT_STUB
144-
int_factory.create_gsi_edge_interrupt = [this,t]() {
145+
int_factory.create_gsi_edge_interrupt = [this, t]() {
145146
return new gsi_edge_interrupt(
146-
_dev.get_irq(),
147-
[=] { if (this->ack_irq()) t->wake(); });
147+
_dev.get_irq(),
148+
[=] { if (this->ack_irq()) t->wake(); });
148149
};
149150
#endif
150151

@@ -159,37 +160,40 @@ fs::fs(virtio_device& virtio_dev)
159160
std::string dev_name("virtiofs");
160161
dev_name += std::to_string(_disk_idx++);
161162

162-
struct device *dev = device_create(&fs_driver, dev_name.c_str(), D_BLK); //TODO Should it be really D_BLK?
163-
struct fuse_strategy *strategy = reinterpret_cast<struct fuse_strategy*>(dev->private_data);
163+
struct device* dev = device_create(&fs_driver, dev_name.c_str(), D_BLK); // TODO Should it be really D_BLK?
164+
auto* strategy = static_cast<fuse_strategy*>(dev->private_data);
164165
strategy->drv = this;
165166
strategy->make_request = fuse_make_request;
166167

167-
debugf("virtio-fs: Add device instance %d as [%s]\n", _id, dev_name.c_str());
168+
debugf("virtio-fs: Add device instance %d as [%s]\n", _id,
169+
dev_name.c_str());
168170
}
169171

170172
fs::~fs()
171173
{
172-
//TODO: In theory maintain the list of free instances and gc it
174+
// TODO: In theory maintain the list of free instances and gc it
173175
// including the thread objects and their stack
174176
}
175177

176178
void fs::read_config()
177179
{
178-
virtio_conf_read(0, &(_config.tag[0]), sizeof(_config.tag));
179-
virtio_conf_read(offsetof(fs_config,num_queues), &(_config.num_queues), sizeof(_config.num_queues));
180-
debugf("virtio-fs: Detected device with tag: [%s] and num_queues: %d\n", _config.tag, _config.num_queues);
180+
virtio_conf_read(0, &_config, sizeof(_config));
181+
debugf("virtio-fs: Detected device with tag: [%s] and num_queues: %d\n",
182+
_config.tag, _config.num_queues);
181183
}
182184

183185
void fs::req_done()
184186
{
185187
auto* queue = get_virt_queue(VQ_REQUEST);
186-
fs_req* req;
187188

188-
while (1) {
189+
while (true) {
189190
virtio_driver::wait_for_queue(queue, &vring::used_ring_not_empty);
190191

192+
fs_req* req;
191193
u32 len;
192-
while((req = static_cast<fs_req*>(queue->get_buf_elem(&len))) != nullptr) {
194+
while ((req = static_cast<fs_req*>(queue->get_buf_elem(&len))) !=
195+
nullptr) {
196+
193197
fuse_req_done(req->fuse_req);
194198
delete req;
195199
queue->get_buf_finalize();
@@ -200,12 +204,13 @@ void fs::req_done()
200204
}
201205
}
202206

203-
int fs::make_request(struct fuse_request* req)
207+
int fs::make_request(fuse_request* req)
204208
{
205209
// The lock is here for parallel requests protection
206210
WITH_LOCK(_lock) {
207-
208-
if (!req) return EIO;
211+
if (!req) {
212+
return EIO;
213+
}
209214

210215
auto* queue = get_virt_queue(VQ_REQUEST);
211216

@@ -214,7 +219,10 @@ int fs::make_request(struct fuse_request* req)
214219
fuse_req_enqueue_input(queue, req);
215220
fuse_req_enqueue_output(queue, req);
216221

217-
auto* fs_request = new fs_req(req);
222+
auto* fs_request = new (std::nothrow) fs_req(req);
223+
if (!fs_request) {
224+
return ENOMEM;
225+
}
218226
queue->add_buf_wait(fs_request);
219227
queue->kick();
220228

drivers/virtio-fs.hh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
namespace virtio {
1818

1919
enum {
20-
VQ_HIPRIO,
21-
VQ_REQUEST
20+
VQ_HIPRIO = 0,
21+
VQ_REQUEST = 1
2222
};
2323

2424
class fs : public virtio_driver {
@@ -34,26 +34,27 @@ public:
3434
virtual std::string get_name() const { return _driver_name; }
3535
void read_config();
3636

37-
int make_request(struct fuse_request*);
37+
int make_request(fuse_request*);
3838

3939
void req_done();
4040
int64_t size();
4141

4242
bool ack_irq();
4343

4444
static hw_driver* probe(hw_device* dev);
45+
4546
private:
4647
struct fs_req {
47-
fs_req(struct fuse_request* f) :fuse_req(f) {};
48+
fs_req(fuse_request* f) : fuse_req(f) {};
4849
~fs_req() {};
4950

50-
struct fuse_request* fuse_req;
51+
fuse_request* fuse_req;
5152
};
5253

5354
std::string _driver_name;
5455
fs_config _config;
5556

56-
//maintains the virtio instance number for multiple drives
57+
// maintains the virtio instance number for multiple drives
5758
static int _instance;
5859
int _id;
5960
// This mutex protects parallel make_request invocations

0 commit comments

Comments
 (0)