3 ms·
> That seems hard to believe. It would inefficient to look for a file for each e.g. write(). I guess a filesystem defines its own implementation-defined open-fi
by loeg 10y ago
> That seems hard to believe. It would inefficient to look for a file for each e.g. write(). I guess a filesystem defines its own implementation-defined open-file handle and the kernel translates that into an (integer) FD.
At least in BSD, the syscall layer calls into the kernel virtual filesystem, which translates file descriptors into file objects. File objects have fo_read/fo_write methods implemented by different kinds of file object.
Files backed by filesystems have an associated "vnode" (virtual inode object). Then the fo_read/fo_write layer invokes the VOP_READ or VOP_WRITE (for example) method on the vnode. VOP ("vnode operation") methods are implemented by individual filesystems. Multiple user-level file descriptors can refer to a single vnode.
The filesystem's inode object hangs off a pointer from the generic vnode. Vnodes are unique (1:1) per real file (inode).
So:
read(fd, ...) ->
sys_read -> kern_readv ->
fget() (translate fd number into struct file object;
check that the descriptor was opened read/write as appropriate for the operation)
dofileread() ->
fo_read() ->
vn_io_fault() (fo_read method for vnodes) -> vn_read ->
VOP_READ(vnode, uio, ioflags, ucred)
(vnode for the file, an io descriptor ("uio"), io flags, and user credentials)
So yeah, at the filesystem layet (VOP_READ), you no longer have the fd (or even the file object) in the BSD model.
Linux's model is similar, but not completely identical. They call "vnodes" "inodes" instead, and may not have the intermediary file layer.