VOP_OPEN(9)

πŸ’– VOP_OPEN β€” open or close a file, bestie edition πŸ’–

okay so we're in the kernel developer's manual, VOP_OPEN(9), and this is literally the page about how files get opened and closed at the vnode level. no cap, this is foundational. let's get into it 🫢


✨ NAME ✨

VOP_OPEN, VOP_CLOSE β€” open or close a file


🌸 SYNOPSIS 🌸

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

int
VOP_OPEN(struct vnode *vp, int mode, struct ucred *cred,
    struct thread *td, struct file *fp);

int
VOP_CLOSE(struct vnode *vp, int mode, struct ucred *cred,
    struct thread *td);

πŸ’… DESCRIPTION πŸ’…

so the whole vibe here is two entry points that bracket a file's life. VOP_OPEN() gets called before a file is accessed by a process, and VOP_CLOSE() gets called after a file is finished with by the process. opening, doing stuff, closing. iconic trio behavior honestly.

here are the arguments, bestie, one by one:

  • vp β€” the vnode of the file. the main character, lowkey.
  • mode β€” the access mode required by the calling process.
  • cred β€” the caller's credentials.
  • td β€” the thread which is accessing the file.
  • fp β€” the file being opened.

so like, that pointer to the file fp? it's genuinely useful for file systems which require such information, e.g. fdescfs(5). and here's the tea: use NULL as the fp argument to VOP_OPEN() for in-kernel opens. don't be out here passing random stuff, okay? 😭

oh and the access mode? it's a set of flags, including FREAD, FWRITE, O_NONBLOCK, O_APPEND. it's giving "bitmask of intentions."


πŸ”’ LOCKS πŸ”’

this is the part where you have to pay attention or you WILL have a bad time, fr fr.

VOP_OPEN() expects vp to be locked on entry and will leave it locked on return. so you hand it a locked vnode, you get a locked vnode back. consistency, we love her. πŸ‘‘

VOP_CLOSE() is more chill β€” it expects at least a reference to be associated with the vnode and does not care whether the vnode is locked or not. the lock and reference state is left unchanged on return. so whatever energy you brought in, that's the energy going out.

but heads up bestie: note that vn_close expects an unlocked, referenced vnode and will dereference the vnode prior to returning. so don't come at it with a locked vnode and then act surprised, iykyk πŸ’€

πŸŽ€ RETURN VALUES πŸŽ€

zero is returned on success, otherwise an error code is returned. that's it, that's the whole thing. zero = slay, nonzero = check the error code, babes.


πŸ¦‹ SEE ALSO πŸ¦‹


πŸ‘‘ AUTHORS πŸ‘‘

this manual page was written by Doug Rabson. king behavior, we thank him for his service 🫢


and that's the whole page, bestie!! go forth and open some files responsibly ✨

this lil writeup came from the Debian crew, dated December 2, 2009, filed under VOP_OPEN(9) in the Kernel Developer's Manual. mwah πŸ’–

πŸ“¦ where this page comes from