Skip to content

Commit 486c2cd

Browse files
authored
Merge pull request raspberrypi#711 from wedsonaf/error-code
rust: move error codes to the `error::code` module
2 parents 1a3b646 + f79631b commit 486c2cd

31 files changed

+938
-951
lines changed

drivers/android/allocation.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ impl<'a> Allocation<'a> {
5757
T: FnMut(&Pages<0>, usize, usize) -> Result,
5858
{
5959
// Check that the request is within the buffer.
60-
if offset.checked_add(size).ok_or(Error::EINVAL)? > self.size {
61-
return Err(Error::EINVAL);
60+
if offset.checked_add(size).ok_or(EINVAL)? > self.size {
61+
return Err(EINVAL);
6262
}
6363
offset += self.offset;
6464
let mut page_index = offset >> bindings::PAGE_SHIFT;
@@ -157,15 +157,15 @@ impl<'a, 'b> AllocationView<'a, 'b> {
157157
}
158158

159159
pub(crate) fn read<T>(&self, offset: usize) -> Result<T> {
160-
if offset.checked_add(size_of::<T>()).ok_or(Error::EINVAL)? > self.limit {
161-
return Err(Error::EINVAL);
160+
if offset.checked_add(size_of::<T>()).ok_or(EINVAL)? > self.limit {
161+
return Err(EINVAL);
162162
}
163163
self.alloc.read(offset)
164164
}
165165

166166
pub(crate) fn write<T>(&self, offset: usize, obj: &T) -> Result {
167-
if offset.checked_add(size_of::<T>()).ok_or(Error::EINVAL)? > self.limit {
168-
return Err(Error::EINVAL);
167+
if offset.checked_add(size_of::<T>()).ok_or(EINVAL)? > self.limit {
168+
return Err(EINVAL);
169169
}
170170
self.alloc.write(offset, obj)
171171
}

drivers/android/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ impl Context {
4747
pub(crate) fn set_manager_node(&self, node_ref: NodeRef) -> Result {
4848
let mut manager = self.manager.lock();
4949
if manager.node.is_some() {
50-
return Err(Error::EBUSY);
50+
return Err(EBUSY);
5151
}
5252
security::binder_set_context_mgr(&node_ref.node.owner.cred)?;
5353

5454
// TODO: Get the actual caller id.
5555
let caller_uid = bindings::kuid_t::default();
5656
if let Some(ref uid) = manager.uid {
5757
if uid.val != caller_uid.val {
58-
return Err(Error::EPERM);
58+
return Err(EPERM);
5959
}
6060
}
6161

drivers/android/process.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use core::{convert::TryFrom, mem::take, ops::Range};
44
use kernel::{
55
bindings,
66
cred::Credential,
7-
file::File,
8-
file_operations::{FileOperations, IoctlCommand, IoctlHandler, PollTable},
7+
file::{self, File, IoctlCommand, IoctlHandler, PollTable},
98
io_buffer::{IoBufferReader, IoBufferWriter},
109
linked_list::List,
1110
mm,
@@ -164,7 +163,7 @@ impl ProcessInner {
164163
if node_cookie == cookie {
165164
Ok(Some(node.clone()))
166165
} else {
167-
Err(Error::EINVAL)
166+
Err(EINVAL)
168167
}
169168
}
170169
}
@@ -436,14 +435,14 @@ impl Process {
436435
break;
437436
}
438437
if *handle == target {
439-
target = target.checked_add(1).ok_or(Error::ENOMEM)?;
438+
target = target.checked_add(1).ok_or(ENOMEM)?;
440439
}
441440
}
442441

443442
// Ensure the process is still alive while we insert a new reference.
444443
let inner = self.inner.lock();
445444
if inner.is_dead {
446-
return Err(Error::ESRCH);
445+
return Err(ESRCH);
447446
}
448447
refs.by_global_id
449448
.insert(reserve1.into_node(node_ref.node.global_id, target));
@@ -466,7 +465,7 @@ impl Process {
466465
.lock()
467466
.by_handle
468467
.get(&handle)
469-
.ok_or(Error::ENOENT)?
468+
.ok_or(ENOENT)?
470469
.node_ref
471470
.clone(strong)
472471
}
@@ -482,7 +481,7 @@ impl Process {
482481
if inc && handle == 0 {
483482
if let Ok(node_ref) = self.ctx.get_manager_node(strong) {
484483
if core::ptr::eq(self, &*node_ref.node.owner) {
485-
return Err(Error::EINVAL);
484+
return Err(EINVAL);
486485
}
487486
let _ = self.insert_or_update_handle(node_ref, true);
488487
return Ok(());
@@ -601,7 +600,7 @@ impl Process {
601600
let mut inner = self.inner.lock();
602601
match &inner.mapping {
603602
None => inner.mapping = Some(Mapping::new(vma.start(), size, ref_pages)?),
604-
Some(_) => return Err(Error::EBUSY),
603+
Some(_) => return Err(EBUSY),
605604
}
606605
Ok(())
607606
}
@@ -653,17 +652,17 @@ impl Process {
653652
|| out.reserved2 != 0
654653
|| out.reserved3 != 0
655654
{
656-
return Err(Error::EINVAL);
655+
return Err(EINVAL);
657656
}
658657

659658
// Only the context manager is allowed to use this ioctl.
660659
if !self.inner.lock().is_manager {
661-
return Err(Error::EPERM);
660+
return Err(EPERM);
662661
}
663662

664663
let node_ref = self
665664
.get_node_from_handle(out.handle, true)
666-
.or(Err(Error::EINVAL))?;
665+
.or(Err(EINVAL))?;
667666

668667
// Get the counts from the node.
669668
{
@@ -705,7 +704,7 @@ impl Process {
705704
})?;
706705

707706
let mut refs = self.node_refs.lock();
708-
let info = refs.by_handle.get_mut(&handle).ok_or(Error::EINVAL)?;
707+
let info = refs.by_handle.get_mut(&handle).ok_or(EINVAL)?;
709708

710709
// Nothing to do if there is already a death notification request for this handle.
711710
if info.death.is_some() {
@@ -741,12 +740,12 @@ impl Process {
741740
let cookie: usize = reader.read()?;
742741

743742
let mut refs = self.node_refs.lock();
744-
let info = refs.by_handle.get_mut(&handle).ok_or(Error::EINVAL)?;
743+
let info = refs.by_handle.get_mut(&handle).ok_or(EINVAL)?;
745744

746-
let death = info.death.take().ok_or(Error::EINVAL)?;
745+
let death = info.death.take().ok_or(EINVAL)?;
747746
if death.cookie != cookie {
748747
info.death = Some(death);
749-
return Err(Error::EINVAL);
748+
return Err(EINVAL);
750749
}
751750

752751
// Update state and determine if we need to queue a work item. We only need to do it when
@@ -782,7 +781,7 @@ impl IoctlHandler for Process {
782781
bindings::BINDER_SET_CONTEXT_MGR_EXT => {
783782
this.set_as_manager(Some(reader.read()?), &thread)?
784783
}
785-
_ => return Err(Error::EINVAL),
784+
_ => return Err(EINVAL),
786785
}
787786
Ok(0)
788787
}
@@ -799,13 +798,13 @@ impl IoctlHandler for Process {
799798
bindings::BINDER_GET_NODE_DEBUG_INFO => this.get_node_debug_info(data)?,
800799
bindings::BINDER_GET_NODE_INFO_FOR_REF => this.get_node_info_from_ref(data)?,
801800
bindings::BINDER_VERSION => this.version(data)?,
802-
_ => return Err(Error::EINVAL),
801+
_ => return Err(EINVAL),
803802
}
804803
Ok(0)
805804
}
806805
}
807806

808-
impl FileOperations for Process {
807+
impl file::Operations for Process {
809808
type Data = Ref<Self>;
810809
type OpenData = Ref<Context>;
811810

@@ -906,17 +905,17 @@ impl FileOperations for Process {
906905
fn mmap(this: RefBorrow<'_, Process>, _file: &File, vma: &mut mm::virt::Area) -> Result {
907906
// We don't allow mmap to be used in a different process.
908907
if !Task::current().group_leader().eq(&this.task) {
909-
return Err(Error::EINVAL);
908+
return Err(EINVAL);
910909
}
911910

912911
if vma.start() == 0 {
913-
return Err(Error::EINVAL);
912+
return Err(EINVAL);
914913
}
915914

916915
let mut flags = vma.flags();
917916
use mm::virt::flags::*;
918917
if flags & WRITE != 0 {
919-
return Err(Error::EPERM);
918+
return Err(EPERM);
920919
}
921920

922921
flags |= DONTCOPY | MIXEDMAP;

drivers/android/range_alloc.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<T> RangeAllocator<T> {
4949

5050
pub(crate) fn reserve_new(&mut self, size: usize) -> Result<usize> {
5151
let desc_ptr = match self.find_best_match(size) {
52-
None => return Err(Error::ENOMEM),
52+
None => return Err(ENOMEM),
5353
Some(found) => found,
5454
};
5555

@@ -70,11 +70,11 @@ impl<T> RangeAllocator<T> {
7070

7171
fn free_with_cursor(cursor: &mut CursorMut<'_, Box<Descriptor<T>>>) -> Result {
7272
let mut size = match cursor.current() {
73-
None => return Err(Error::EINVAL),
73+
None => return Err(EINVAL),
7474
Some(ref mut entry) => {
7575
match entry.state {
76-
DescriptorState::Free => return Err(Error::EINVAL),
77-
DescriptorState::Allocated => return Err(Error::EPERM),
76+
DescriptorState::Free => return Err(EINVAL),
77+
DescriptorState::Allocated => return Err(EPERM),
7878
DescriptorState::Reserved => {}
7979
}
8080
entry.state = DescriptorState::Free;
@@ -121,13 +121,13 @@ impl<T> RangeAllocator<T> {
121121

122122
pub(crate) fn reservation_abort(&mut self, offset: usize) -> Result {
123123
// TODO: The force case is currently O(n), but could be made O(1) with unsafe.
124-
let mut cursor = self.find_at_offset(offset).ok_or(Error::EINVAL)?;
124+
let mut cursor = self.find_at_offset(offset).ok_or(EINVAL)?;
125125
Self::free_with_cursor(&mut cursor)
126126
}
127127

128128
pub(crate) fn reservation_commit(&mut self, offset: usize, data: Option<T>) -> Result {
129129
// TODO: This is currently O(n), make it O(1).
130-
let mut cursor = self.find_at_offset(offset).ok_or(Error::ENOENT)?;
130+
let mut cursor = self.find_at_offset(offset).ok_or(ENOENT)?;
131131
let desc = cursor.current().unwrap();
132132
desc.state = DescriptorState::Allocated;
133133
desc.data = data;
@@ -140,10 +140,10 @@ impl<T> RangeAllocator<T> {
140140
/// Returns the size of the existing entry and the data associated with it.
141141
pub(crate) fn reserve_existing(&mut self, offset: usize) -> Result<(usize, Option<T>)> {
142142
// TODO: This is currently O(n), make it O(log n).
143-
let mut cursor = self.find_at_offset(offset).ok_or(Error::ENOENT)?;
143+
let mut cursor = self.find_at_offset(offset).ok_or(ENOENT)?;
144144
let desc = cursor.current().unwrap();
145145
if desc.state != DescriptorState::Allocated {
146-
return Err(Error::ENOENT);
146+
return Err(ENOENT);
147147
}
148148
desc.state = DescriptorState::Reserved;
149149
Ok((desc.size, desc.data.take()))

drivers/android/thread.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ use core::{
77
};
88
use kernel::{
99
bindings,
10-
file::File,
11-
file_operations::PollTable,
10+
file::{File, PollTable},
1211
io_buffer::{IoBufferReader, IoBufferWriter},
1312
linked_list::{GetLinks, Links, List},
1413
prelude::*,
@@ -163,11 +162,11 @@ impl InnerThread {
163162
/// (that it could respond to) but it has also issued a transaction, it must first wait for the
164163
/// previously-issued transaction to complete.
165164
fn pop_transaction_to_reply(&mut self, thread: &Thread) -> Result<Ref<Transaction>> {
166-
let transaction = self.current_transaction.take().ok_or(Error::EINVAL)?;
165+
let transaction = self.current_transaction.take().ok_or(EINVAL)?;
167166

168167
if core::ptr::eq(thread, transaction.from.as_ref()) {
169168
self.current_transaction = Some(transaction);
170-
return Err(Error::EINVAL);
169+
return Err(EINVAL);
171170
}
172171

173172
// Find a new current transaction for this thread.
@@ -278,7 +277,7 @@ impl Thread {
278277
fn get_work_local(self: &Ref<Self>, wait: bool) -> Result<Ref<dyn DeliverToRead>> {
279278
// Try once if the caller does not want to wait.
280279
if !wait {
281-
return self.inner.lock().pop_work().ok_or(Error::EAGAIN);
280+
return self.inner.lock().pop_work().ok_or(EAGAIN);
282281
}
283282

284283
// Loop waiting only on the local queue (i.e., not registering with the process queue).
@@ -293,7 +292,7 @@ impl Thread {
293292
inner.looper_flags &= !LOOPER_WAITING;
294293

295294
if signal_pending {
296-
return Err(Error::ERESTARTSYS);
295+
return Err(ERESTARTSYS);
297296
}
298297
}
299298
}
@@ -317,7 +316,7 @@ impl Thread {
317316
// We know nothing will have been queued directly to the thread queue because it is not in
318317
// a transaction and it is not in the process' ready list.
319318
if !wait {
320-
return self.process.get_work().ok_or(Error::EAGAIN);
319+
return self.process.get_work().ok_or(EAGAIN);
321320
}
322321

323322
// Get work from the process queue. If none is available, atomically register as ready.
@@ -343,7 +342,7 @@ impl Thread {
343342
// error).
344343
drop(inner);
345344
drop(reg);
346-
return self.inner.lock().pop_work().ok_or(Error::ERESTARTSYS);
345+
return self.inner.lock().pop_work().ok_or(ERESTARTSYS);
347346
}
348347
}
349348
}
@@ -473,7 +472,7 @@ impl Thread {
473472

474473
// This guarantees that at least `sizeof(usize)` bytes will be allocated.
475474
let len = core::cmp::max(
476-
adata_size.checked_add(aoffsets_size).ok_or(Error::ENOMEM)?,
475+
adata_size.checked_add(aoffsets_size).ok_or(ENOMEM)?,
477476
size_of::<usize>(),
478477
);
479478
let mut alloc = to_process.buffer_alloc(len)?;
@@ -607,7 +606,7 @@ impl Thread {
607606
let inner = self.inner.lock();
608607
Ok(if let Some(cur) = &inner.current_transaction {
609608
if core::ptr::eq(self, cur.from.as_ref()) {
610-
return Err(Error::EINVAL);
609+
return Err(EINVAL);
611610
}
612611
Some(cur.clone())
613612
} else {
@@ -691,7 +690,7 @@ impl Thread {
691690

692691
// TODO: Add support for BC_TRANSACTION_SG and BC_REPLY_SG.
693692
// BC_ATTEMPT_ACQUIRE and BC_ACQUIRE_RESULT are no longer supported.
694-
_ => return Err(Error::EINVAL),
693+
_ => return Err(EINVAL),
695694
}
696695

697696
// Update the number of write bytes consumed.

drivers/android/transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl Transaction {
173173

174174
// If the list is non-empty, prepare the buffer.
175175
if !file_list.is_empty() {
176-
let alloc = self.to.buffer_get(self.data_address).ok_or(Error::ESRCH)?;
176+
let alloc = self.to.buffer_get(self.data_address).ok_or(ESRCH)?;
177177
let cleanup = ScopeGuard::new(|| {
178178
self.free_allocation.store(false, Ordering::Relaxed);
179179
});

drivers/char/hw_random/bcm2835_rng_rust.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
//! Broadcom BCM2835 Random Number Generator support.
44
55
use kernel::{
6-
device, file::File, file_operations::FileOperations, io_buffer::IoBufferWriter, miscdev,
7-
module_platform_driver, of, platform, prelude::*, sync::Ref,
6+
device, file, file::File, io_buffer::IoBufferWriter, miscdev, module_platform_driver, of,
7+
platform, prelude::*, sync::Ref,
88
};
99

1010
module_platform_driver! {
@@ -17,7 +17,7 @@ module_platform_driver! {
1717

1818
struct RngDevice;
1919

20-
impl FileOperations for RngDevice {
20+
impl file::Operations for RngDevice {
2121
kernel::declare_file_operations!(read);
2222

2323
fn open(_open_data: &(), _file: &File) -> Result {
@@ -55,7 +55,7 @@ impl platform::Driver for RngDriver {
5555
)?;
5656

5757
data.registrations()
58-
.ok_or(Error::ENXIO)?
58+
.ok_or(ENXIO)?
5959
.as_pinned_mut()
6060
.register(fmt!("rust_hwrng"), ())?;
6161
Ok(data.into())

0 commit comments

Comments
 (0)