VOP_READDIR(9)

πŸ’… VOP_READDIR β€” like, reading a directory, bestie

okay so this is a page from the Kernel Developer's Manual (section 9, kernel-adjacent things, iykyk) and it's all about VOP_READDIR, which is literally the vnode operation that reads what's inside a directory. yes. that. let's go πŸ¦‹


✨ NAME ✨

VOP_READDIR β€” read contents of a directory πŸ“‚


🌸 SYNOPSIS 🌸

#include <sys/param.h>
#include <sys/dirent.h>
#include <sys/vnode.h>

int
VOP_READDIR(struct vnode *vp, struct uio *uio, struct ucred *cred,
    int *eofflag, int *ncookies, u_long **cookies);

like... look at that signature. it's giving many arguments. we love her for it tho 🫢


πŸ’– DESCRIPTION πŸ’–

Read directory entries. that's the whole vibe. but let's break down every single one of these lil parameters because each one matters, no cap:

  • vp β€” the vnode of the directory. the actual folder. the main character πŸ‘‘
  • uio β€” where to read the directory contents. this is where the good stuff goes.
  • cred β€” the caller's credentials. who even are you, prove it πŸ’…
  • eofflag β€” return end of file status (NULL if not wanted).
  • ncookies β€” number of directory cookies generated for NFS (NULL if not wanted).
  • cookies β€” directory seek cookies generated for NFS (NULL if not wanted).

The directory contents are read into struct dirent structures. and if the on-disc data structures differ from this, then bestie, they should be translated. periodt. πŸ”₯


πŸ”’ LOCKS πŸ”’

The directory should be locked on entry and will still be locked on exit. so like, she goes in locked, she comes out locked. no unlocked entries in this house. not on my watch. πŸ™ˆ


πŸ’€ RETURN VALUES πŸ’€

Zero is returned on success, otherwise an error code is returned. classic. zero equals good, anything else equals uh oh 😭

If this is called from the NFS server, the extra arguments eofflag, ncookies and cookies are given. The value of *eofflag should be set to TRUE if the end of the directory is reached while reading. The directory seek cookies are returned to the NFS client and may be used later to restart a directory read part way through the directory. There should be one cookie returned per directory entry. The value of the cookie should be the offset within the directory where the on-disc version of the appropriate directory entry starts.

Memory for the cookies should be allocated using:

...;
*ncookies = number of entries read;
*cookies = malloc(*ncookies * sizeof(u_long), M_TEMP, M_WAITOK);

like, that's the exact allocation ritual, do not improvise ✨


🚨 ERRORS 🚨

  • [EINVAL] β€” An attempt was made to read from an illegal offset in the directory. rude. illegal even. we don't do that here.
  • [EIO] β€” A read error occurred while reading the directory. the directory said "i'm not okay rn" 😭
  • [EINTEGRITY] β€” Corrupted data was detected while reading the directory. the data got caught lacking. integrity check said absolutely not πŸ’€

πŸ‘€ SEE ALSO πŸ‘€

go read that one too, it's the prequel, iykyk 🌈


πŸŽ€ AUTHORS πŸŽ€

This manual page was written by Doug Rabson. thank you Doug, we stan 🫢


a little note from me to you: keep the directory locked, return those cookies correctly, and always check your error codes. slay responsibly πŸ’œ

okay that's the whole page!! this was written from the Debian version dated March 30, 2020 β€” mwah, bye bestie, go read some vnodes πŸŒΈβœ¨πŸ’…

πŸ“¦ where this page comes from