Page MenuHomeFreeBSD

getdirentries: Return ENOTDIR if not a directory.
ClosedPublic

Authored by des on Tue, Jul 8, 6:47 PM.
Tags
None
Referenced Files
Unknown Object (File)
Thu, Jul 10, 10:30 AM
Unknown Object (File)
Wed, Jul 9, 8:36 PM
Subscribers

Details

Summary

This is both more logical and more useful than EINVAL.

MFC after: never
Sponsored by: Klara, Inc.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Skipped
Unit
Tests Skipped
Build Status
Buildable 65308
Build 62191: arc lint + arc unit

Event Timeline

des requested review of this revision.Tue, Jul 8, 6:47 PM
kib added inline comments.
lib/libsys/getdirentries.2
173

I do not object against the change.
Just a note that with it, sometimes this condition would be reported as ENOTDIR now.
Specifically, when e.g. the mount where the corresponding vnode is located, is forcibly unmounted.

This revision is now accepted and ready to land.Tue, Jul 8, 7:23 PM
This revision now requires review to proceed.Tue, Jul 8, 7:42 PM
lib/libsys/getdirentries.2
173

Sorry, I don't understand what this means, or how it relates to the line it's attached to.

lib/libsys/getdirentries.2
173

The EBADF condition that is described in the enumeration item I commented, after the patch might some time (most of the time in fact) result in ENOTDIR. But some times it is still EBADF.

des marked 2 inline comments as done.
des added inline comments.
lib/libsys/getdirentries.2
173

It would previously have returned EINVAL, never EBADF, but I've fixed it now.

des marked an inline comment as done.Wed, Jul 9, 5:15 PM
des edited the summary of this revision. (Show Details)

return EBADF for VBAD.

lib/libsys/getdirentries.2
173

Sometimes it was still EBADF when falling to deadfs VOP.

sys/kern/vfs_syscalls.c
4317

This is racy, the vnode type can change from V* to VBAD any time when the vnode lock is not owned.

At least, move the check after the vn_lock. And please add a comment explaining why we check the vnode type at all, since usually we either not do it at all, or do vn_lock() without LK_RETRY and check vn_lock() failures.

sys/kern/vfs_syscalls.c
4317

In that case we may as well drop the check and let the call through to the VOP, which will return either EBADF or ENOTDIR anyway, no?

sys/kern/vfs_syscalls.c
4317

Look at ufs_readdir(): it does not tread directories specially, it reads the data pages and interpret the content as dirents. In other words, I think that removing the check cannot be done with the current code, and would require passing over all fs ufs_readdir() to ensure ENOTDIR.

sys/kern/vfs_syscalls.c
4317

Is it safe to assume that VNON can't happen?

des marked 2 inline comments as done.Wed, Jul 9, 8:08 PM
des marked an inline comment as done.
sys/kern/vfs_syscalls.c
4317

Cannot happen in what sense?

If you have a directory vnode, and it is not locked, it can be reclaimed at any moment, at which time it is converted to VBAD. So if you checked the type of a vnode and it is not VDIR, the vnode would not magically become VDIR later (we do not expose half-constructed vnodes).

This revision is now accepted and ready to land.Wed, Jul 9, 8:15 PM
sys/kern/vfs_syscalls.c
4317

I'm asking if it's safe to assume that the vnode's type must either be valid or VBAD, and can never be VNON at this point, so I don't need to check for it. I think it's safe, but I'd still like you to confirm it.

des marked an inline comment as done.Wed, Jul 9, 8:19 PM
sys/kern/vfs_syscalls.c
4317

I think I answered it in the second paragraph of my previous response.

Also, I do not see why would you need to specifically check for VNON. If you have a non-directory type vnode, the only special case is the doomed vnode where it is better to fine-select EBADF.

des marked an inline comment as done.Wed, Jul 9, 8:27 PM
des added inline comments.
sys/kern/vfs_syscalls.c
4317

Because imo VNON would also be an EBADF case rather than ENOTDIR. But it can't happen, so it doesn't matter.

des marked an inline comment as done.Wed, Jul 9, 8:27 PM