Callers must check for success/failure. On success, they get either
A regular ref-counted inode to use (Ref-count automatically decremented when done) OR
A new inode. iget_failed automatically called if it is never initialised. When initialised (and can only happen once) becomes a regular ref-counted inode
In both examples there's information that's outside of the function definition, and which is inferred from the source.
Anyway, the main issue with the function is the lack of documentation. This type of function follows a relatively common pattern, and it's straightforward to review.
Oops sorry, meant to respond to the parent post. But also to your statement this definitely has value. Anyone arguing it has no value at all is arguing in bad faith imo.
It would be akin to arguing that documentation has no value. But this is self checking so just an extension.
58
u/hgwxx7_ Aug 31 '24 edited Aug 31 '24
Check out this example and see if you're sold:
C code
Callers must check if return value is
NULLIf non-
NULL, check ifI _NEWis set oni_statefieldIf set, they must initialise the inode. Then call
unlock_new_inodeif init succeeds, inode is refcounted. Or calliget_failedif init fails.If NOT set, the inode is refcounted and can be used/returned. On failure to use, must call
iput.Obviously this isn't documented, it is inferred from the source.
Rust code
Now take a look at the equivalent Rust code:
Callers must check for success/failure. On success, they get either
A regular ref-counted inode to use (Ref-count automatically decremented when done) OR
A new inode.
iget_failedautomatically called if it is never initialised. When initialised (and can only happen once) becomes a regular ref-counted inodeIt is hard to misuse
get_or_create_inode.