Pathname lookup and filesystem I/O¶
Setting up lookup¶
The lookup interface carries its state in nameidata and
componentname, defined in sys/sys/namei.h. The caller supplies
the operation and flags, the pathname, and the starting context through
the appropriate initialization interfaces.
Flags can request a locked leaf, a parent vnode, or particular traversal behavior. Relative paths, directory file descriptors, root constraints, and capability restrictions affect where traversal is allowed to begin and continue. Do not describe lookup as simply splitting a string at slashes and searching from the global root.
namei() is the main entry point. In this checkout it attempts
cache_fplookup() before continuing through the ordinary lookup path.
The fast path’s completion status determines whether the request is
finished, partially resolved, or needs fallback/restart handling.
Namecache and filesystem lookup¶
vfs_cache.c contains positive and negative namecache entries, lookup
validation, and coordination with rename and reclamation. A positive
entry associates a name in a directory with a vnode; a negative entry can
remember absence. Neither replaces all filesystem access or permission
checks in every lookup mode.
vfs_lookup() walks components through the ordinary path and
coordinates directory vnode operations, mount crossings, and symlinks.
VOP_LOOKUP connects a component lookup to the filesystem implementation.
The namecache fast path uses additional lifetime and sequence-validation
mechanisms to avoid taking the same locks as the ordinary path in cases
it can safely complete.
Changes such as rename, unmount, and vnode reclamation can race with lookup. Restart and fallback paths are part of the design, not evidence that a cached vnode pointer may be used without revalidation.
Results and cleanup¶
On return, inspect the result flags and requested lookup mode to determine which vnode references and locks the caller owns. A request involving a parent and a leaf can have special cases where the identities overlap. Failure paths can leave a different set of resources from success paths.
Pathname-buffer ownership is separate from vnode ownership. The cleanup
interfaces in namei.h, including NDFREE_PNBUF, do not stand in for
releasing vnode references or unlocking vnodes. Read the source’s actual
cleanup sequence for the operation being implemented.
From a pathname to an open file¶
vn_open() supplies common vnode-open handling. The syscall-facing
code in vfs_syscalls.c and the file-descriptor layer turn that result
into an open file description and a process-visible descriptor.
A descriptor number is a process’s handle; the open file description carries file-operation state; the vnode identifies the filesystem object. Closing a descriptor, releasing an open file description, and reclaiming a vnode are therefore distinct events.
vfs_vnops.c supplies vnode-backed file operations through its
vnops vector. vn_rdwr() is also a common helper for vnode I/O.
The filesystem’s vnode operations ultimately implement the requested
access, with credentials, offsets, and locking supplied by the call path.
VM pages and the buffer cache¶
File-backed mappings use the vnode pager in sys/vm/vnode_pager.c.
A vm_fault() on such a mapping can obtain file data through
vm_pager_get_pages() and filesystem pager operations. The pager
must coordinate file size and the VM object’s view of the file.
vfs_bio.c and vfs_cluster.c implement buffer-cache and clustered-I/O
work. A filesystem can use these interfaces together with VM-backed data
pages; not every read is the same buffer-cache path. vfs_vnops.c also
contains a page-cache read path, so a read can sometimes obtain resident
data without entering the full filesystem read operation.
Distinguish dirty VM pages, dirty buffers, and filesystem metadata when tracing writeback. They have different bookkeeping and synchronization points. Completion of one layer’s operation does not by itself establish all of another layer’s persistence or invalidation guarantees.
Following an operation¶
For a lookup problem, record the starting context, flags, component being resolved, and whether the fast or ordinary path handled it. For an I/O problem, establish whether the access came through a descriptor, a common vnode helper, or a file-backed page fault before following the filesystem operation and backing I/O.