Why is it impractical to check -ENODEV on debugfs api

debugfs api like debugfs_create_dir said

If debugfs is not included in the kernel, -ENODEV will be returned. It is impractical to check for this value, but rather check for NULL or! NULL instead, to eliminate the need for #ifdef in the calling code.

But why is it not wise? Could you give some examples about eliminate the need for #ifdef in the calling code

?

+3


source to share


3 answers


It is possible to interpret -ENODEV as a success, as long as dentry does not return something other than delete (debugfs_remove) and uses it as a directory for another file (debugfs_create_-family). All of these functions handle the case where CONFIG_DEBUG_FS is disabled correctly.

This is why it is only enough to check for NULL or! NULL.



In the rare case where the dentry returned by debugfs_create _ * () is passed explicitly (for example, to access private inode data), it is sufficient to check that only the first such call does not return -ENODEV.

+1


source


Yes. This advice doesn't make sense. If CONFIG_DEBUG_FS is not activated, the return value is -ENODEV (cast to pointer). It is clearly not NULL, but it is also not used as a valid pointer.



It would be better to use the return value instead of IS_ERR_OR_NULL (ptr). If CONFIG_DEBUG_FS is not defined, it will return -ENODEV. If CONFIG_DEBUG_FS is defined but the call fails, NULL is returned. This method handles every opportunity.

+1


source


You asked, "Could you provide any examples of eliminating the need for #ifdef in the calling code?"

Well one way to write your code:

#ifdef CONFIG_DEBUG_FS
  < ... do your debugfs stuff ... >
#endif

      

I would guess that this meant documentation.

Many kernel developers think that too many pairs #ifdef - #endif

in the code should be ugly ("hairy"). See this kernel document .

Relevant core code snippet Document code: "Prefer to compile entire functions rather than parts of functions or parts of expressions. Instead of putting an ifdef in an expression, output part or all of the expressions into a separate helper function and apply conditionally to that function."

Considering that following his advice, if you'd rather not use #ifdef, then pl goes with the already posted answers.

+1


source







All Articles