rust: Add functions to create and modify an Inode
PATCH 1/1 rust: Add functions to create and modify an Inode
Add try_new_inode function associated to SuperBlock which allocates a new inode and implement a few setter functions for INode.
--- rust/kernel/fs.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+)
rust/kernel/fs.rs +41 -0
@@ -707,6 +707,18 @@ pub struct SuperBlock<T: Type + ?Sized>( PhantomData<T>, ); +impl <T :Type + ?Sized >SuperBlock <T > { +/// Create a new inode +pub fn try_new_inode (&self) ->Result <INode > { +let inode =unsafe { bindings::new_inode (self.0 .get ()) }; +if inode.is_null () { +return Err (ENOMEM ); + } +// SAFETY: We've just allocated a new inode and checked that it's not null +Ok (INode (UnsafeCell ::new (unsafe { *inode }))) + } +} +/// Wraps the kernel's `struct inode`. /// /// # Invariants
@@ -716,6 +728,35 @@ pub struct SuperBlock<T: Type + ?Sized>(#[repr(transparent)] pub struct INode (pub (crate )UnsafeCell <bindings::inode >); +impl INode { +/// set the field i_mode of the inode +pub fn set_mode (&mut self, mode:u16 ) { + self.0 .get_mut ().i_mode = mode; + } +/// set the field i_ino of the inode +pub fn set_ino (&mut self, ino:u64 ) { + self.0 .get_mut ().i_ino = ino; + } + +/// set the current time in the atime, ctime and mtime fields of the inode +pub fn set_current_time (&mut self) { +let time =unsafe { bindings::current_time (self.0 .get ()) }; + self.0 .get_mut ().i_atime = time; + self.0 .get_mut ().i_ctime = time; + self.0 .get_mut ().i_mtime = time; + } + +/// set the field i_fop of the inode +pub fn set_fop (&mut self, fop: *const bindings::file_operations ) { + self.0 .get_mut ().__bindgen_anon_3.i_fop = fop; + } + +/// set the field i_op of the inode +pub fn set_op (&mut self, fop: *const bindings::inode_operations ) { + self.0 .get_mut ().i_op = fop; + } +} +// SAFETY: The type invariants guarantee that `INode` is always ref-counted. unsafe impl AlwaysRefCounted for INode {fn inc_ref (&self) {
Signature · 1 line
2.40.0
Hi Ariel,
Quoted 3 lines · Ariel Miculas
Add
try_new_inodefunction associated to SuperBlock which allocates a new inode and implement a few setter functions for INode.
Thanks for the patch!
This seems to be based on the rust branch: the filesystem abstractions we have there are not yet in mainline, so we can't take this now since fs.rs does not exist yet. Please see
https://rust-for-linux.com/branches.
Also, I haven't reviewed the patch, but some quick notes below that you may find useful.
Quoted 1 line
+ let inode = unsafe { bindings::new_inode(self.0.get()) };
Please follow the coding conventions / guidelines of the rest of other code. For instance, we require // SAFETY comments on all unsafe blocks. Please see https://docs.kernel.org/rust/coding-guidelines.html
for some details.
Quoted 1 line
+ /// set the field
i_modeof the inode
Ditto, for docs.
Quoted 1 line
+ pub fn
set_fop(&mut self, fop: *const bindings::file_operations){
If this is intended to be used by modules (as it appears to be since it is public in a public module), then you will likely need to avoid referring to bindings:: so that modules do not need to deal directly with the C types.
Moreover, the fact that a safe function takes a raw pointer raises some questions -- what happens if somebody passes a dangling pointer, for instance?
Cheers,
Miguel
Thanks Miguel, you've clarified some questions I had about the branches and the development process.
Regarding the rust branch, it says:
Quoted 1 line
However, please feel free to submit new PRs for this branch.
Does it make sense to open PRs against the rust branch in github and ask for feedback that way or should the existing fs.rs from the rust branch be merged into rust-next instead (and then I could submit patches)?
For context, I'm trying to add the necessary fs abstractions in order to be able to rewrite lwnfs (https://lwn.net/Articles/57369/) in rust. I want to ask for feedback early because I'm not sure the abstractions I come up with are the right ones.
Regards,
Ariel
Quoted 42 lines · Miguel Ojeda
Hi Ariel,
Quoted 3 lines · Ariel Miculas
Add
try_new_inodefunction associated to SuperBlock which allocates a new inode and implement a few setter functions for INode.Thanks for the patch!
This seems to be based on the
rustbranch: the filesystem abstractions we have there are not yet in mainline, so we can't take this now sincefs.rsdoes not exist yet. Please see
https://rust-for-linux.com/branches.Also, I haven't reviewed the patch, but some quick notes below that you may find useful.
Quoted 1 line
+ let inode = unsafe { bindings::new_inode(self.0.get()) };
Please follow the coding conventions / guidelines of the rest of other code. For instance, we require
// SAFETYcomments on allunsafeblocks. Please see https://docs.kernel.org/rust/coding-guidelines.html
for some details.Quoted 1 line
+ /// set the field
i_modeof the inodeDitto, for docs.
Quoted 1 line
+ pub fn
set_fop(&mut self, fop: *const bindings::file_operations){If this is intended to be used by modules (as it appears to be since it is public in a public module), then you will likely need to avoid referring to
bindings::so that modules do not need to deal directly with the C types.Moreover, the fact that a safe function takes a raw pointer raises some questions -- what happens if somebody passes a dangling pointer, for instance?
Cheers,
Miguel
Quoted 6 lines · Ariel Miculas
Thanks Miguel, you've clarified some questions I had about the branches and the development process.
Regarding the rust branch, it says:Quoted 1 line
However, please feel free to submit new PRs for this branch.
Quoted 8 lines
Does it make sense to open PRs against the rust branch in github and ask for feedback that way or should the existing
fs.rsfrom the rust branch be merged into rust-next instead (and then I could submit patches)?
For context, I'm trying to add the necessary fs abstractions in order to be able to rewrite lwnfs (https://lwn.net/Articles/57369/) in rust. I want to ask for feedback early because I'm not sure the abstractions I come up with are the right ones.
Please checkout https://github.com/wedsonaf/linux/commits/fs
This is the continuation of the code currently in the rust branch. It allows the creation of a read-only in-memory file system as you can see in the sample code.
I was working on it with the intent of merging it to rust but then we decided to cut down what was upstreamed to a bare minimum, so I put this on the back burner while we push other pieces (on which this depends) upstream first. We also heard from Matthew Wilcox (the page cache maintainer) on LPC that he has ideas on what the interface should (or shouldn't) look like. So we'll get him involved [if he has the time] once we have the dependencies upstreamed.
Some of the code we know will change for the better now that we have pin-init.
Anyway, you're welcome to join us if you're interested.
Cheers,
-Wedson
Quoted 48 lines
Regards,
ArielQuoted 42 lines · Miguel Ojeda
Hi Ariel,
Quoted 3 lines · Ariel Miculas
Add
try_new_inodefunction associated to SuperBlock which allocates a new inode and implement a few setter functions for INode.Thanks for the patch!
This seems to be based on the
rustbranch: the filesystem abstractions we have there are not yet in mainline, so we can't take this now sincefs.rsdoes not exist yet. Please see
https://rust-for-linux.com/branches.Also, I haven't reviewed the patch, but some quick notes below that you may find useful.
Quoted 1 line
+ let inode = unsafe { bindings::new_inode(self.0.get()) };
Please follow the coding conventions / guidelines of the rest of other code. For instance, we require
// SAFETYcomments on allunsafeblocks. Please see https://docs.kernel.org/rust/coding-guidelines.html
for some details.Quoted 1 line
+ /// set the field
i_modeof the inodeDitto, for docs.
Quoted 1 line
+ pub fn
set_fop(&mut self, fop: *const bindings::file_operations){If this is intended to be used by modules (as it appears to be since it is public in a public module), then you will likely need to avoid referring to
bindings::so that modules do not need to deal directly with the C types.Moreover, the fact that a safe function takes a raw pointer raises some questions -- what happens if somebody passes a dangling pointer, for instance?
Cheers,
Miguel
Thanks, Wedson, I wasn't aware of your branch. I'm definitely interested in helping out, let me know what I can do. I'm new to rust-for-linux, though, so don't expect too much.
Regards,
Ariel
Quoted 86 lines · Wedson Almeida Filho
Quoted 6 lines · Ariel Miculas
Thanks Miguel, you've clarified some questions I had about the branches and the development process.
Regarding the rust branch, it says:Quoted 1 line
However, please feel free to submit new PRs for this branch.
Quoted 8 lines
Does it make sense to open PRs against the rust branch in github and ask for feedback that way or should the existing
fs.rsfrom the rust branch be merged into rust-next instead (and then I could submit patches)?
For context, I'm trying to add the necessary fs abstractions in order to be able to rewrite lwnfs (https://lwn.net/Articles/57369/) in rust. I want to ask for feedback early because I'm not sure the abstractions I come up with are the right ones.Please checkout https://github.com/wedsonaf/linux/commits/fs
This is the continuation of the code currently in the
rustbranch. It allows the creation of a read-only in-memory file system as you can see in the sample code.I was working on it with the intent of merging it to
rustbut then we decided to cut down what was upstreamed to a bare minimum, so I put this on the back burner while we push other pieces (on which this depends) upstream first. We also heard from Matthew Wilcox (the page cache maintainer) on LPC that he has ideas on what the interface should (or shouldn't) look like. So we'll get him involved [if he has the time] once we have the dependencies upstreamed.Some of the code we know will change for the better now that we have pin-init.
Anyway, you're welcome to join us if you're interested.
Cheers,
-WedsonQuoted 48 lines
Regards,
ArielQuoted 42 lines · Miguel Ojeda
Hi Ariel,
Quoted 3 lines · Ariel Miculas
Add
try_new_inodefunction associated to SuperBlock which allocates a new inode and implement a few setter functions for INode.Thanks for the patch!
This seems to be based on the
rustbranch: the filesystem abstractions we have there are not yet in mainline, so we can't take this now sincefs.rsdoes not exist yet. Please see
https://rust-for-linux.com/branches.Also, I haven't reviewed the patch, but some quick notes below that you may find useful.
Quoted 1 line
+ let inode = unsafe { bindings::new_inode(self.0.get()) };
Please follow the coding conventions / guidelines of the rest of other code. For instance, we require
// SAFETYcomments on allunsafeblocks. Please see https://docs.kernel.org/rust/coding-guidelines.html
for some details.Quoted 1 line
+ /// set the field
i_modeof the inodeDitto, for docs.
Quoted 1 line
+ pub fn
set_fop(&mut self, fop: *const bindings::file_operations){If this is intended to be used by modules (as it appears to be since it is public in a public module), then you will likely need to avoid referring to
bindings::so that modules do not need to deal directly with the C types.Moreover, the fact that a safe function takes a raw pointer raises some questions -- what happens if somebody passes a dangling pointer, for instance?
Cheers,
Miguel
A message that is not in the archive (sent off-list or not yet received): CAPDJoNt_jCPfkmS7pRVA=1-U6csPEMuYsjTdwYm3HpUyZBVEUQ@mail.gmail.com
Quoted 2 lines · Ariel Miculas
Thanks Miguel, you've clarified some questions I had about the branches and the development process.
My pleasure!
Quoted 5 lines
Regarding the rust branch, it says:
Quoted 1 line
However, please feel free to submit new PRs for this branch.
Does it make sense to open PRs against the rust branch in github and ask for feedback that way or should the existing
fs.rsfrom the rust branch be merged into rust-next instead (and then I could submit patches)? For context, I'm trying to add the necessary fs abstractions in order to be able to rewrite lwnfs (https://lwn.net/Articles/57369/) in rust. I want to ask for feedback early because I'm not sure the abstractions I come up with are the right ones.
Yes, in general, please feel free to submit PRs there. The PRs will not be merged, because the branch is effectively frozen and will eventually go away / get archived, but you may still find it useful to get early feedback. The mailing list is meant for mainline development instead.
As for mainline, anyone can also send patches, of course, as long as they are ready to be applied (again, in general, e.g. for filesystem APIs, please wait until things get a bit more settled down as Wedson mentioned -- when in doubt, you can always ask in Zulip).
Cheers,
Miguel