^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) ===============
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) Pathname lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) ===============
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) This write-up is based on three articles published at lwn.net:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) - <https://lwn.net/Articles/649115/> Pathname lookup in Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) - <https://lwn.net/Articles/649729/> RCU-walk: faster pathname lookup in Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) - <https://lwn.net/Articles/650786/> A walk among the symlinks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) Written by Neil Brown with help from Al Viro and Jon Corbet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) It has subsequently been updated to reflect changes in the kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) including:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) - per-directory parallel name lookup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) - ``openat2()`` resolution restriction flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) Introduction to pathname lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) ===============================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) The most obvious aspect of pathname lookup, which very little
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) exploration is needed to discover, is that it is complex. There are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) many rules, special cases, and implementation alternatives that all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) combine to confuse the unwary reader. Computer science has long been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) acquainted with such complexity and has tools to help manage it. One
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) tool that we will make extensive use of is "divide and conquer". For
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) the early parts of the analysis we will divide off symlinks - leaving
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) them until the final part. Well before we get to symlinks we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) another major division based on the VFS's approach to locking which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) will allow us to review "REF-walk" and "RCU-walk" separately. But we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) are getting ahead of ourselves. There are some important low level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) distinctions we need to clarify first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) There are two sorts of ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) --------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) .. _openat: http://man7.org/linux/man-pages/man2/openat.2.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) Pathnames (sometimes "file names"), used to identify objects in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) filesystem, will be familiar to most readers. They contain two sorts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) of elements: "slashes" that are sequences of one or more "``/``"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) characters, and "components" that are sequences of one or more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) non-"``/``" characters. These form two kinds of paths. Those that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) start with slashes are "absolute" and start from the filesystem root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) The others are "relative" and start from the current directory, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) from some other location specified by a file descriptor given to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) "``*at()``" system calls such as `openat() <openat_>`_.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) .. _execveat: http://man7.org/linux/man-pages/man2/execveat.2.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) It is tempting to describe the second kind as starting with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) component, but that isn't always accurate: a pathname can lack both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) slashes and components, it can be empty, in other words. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) generally forbidden in POSIX, but some of those "``*at()``" system calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) in Linux permit it when the ``AT_EMPTY_PATH`` flag is given. For
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) example, if you have an open file descriptor on an executable file you
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) can execute it by calling `execveat() <execveat_>`_ passing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) the file descriptor, an empty path, and the ``AT_EMPTY_PATH`` flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) These paths can be divided into two sections: the final component and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) everything else. The "everything else" is the easy bit. In all cases
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) it must identify a directory that already exists, otherwise an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) such as ``ENOENT`` or ``ENOTDIR`` will be reported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) The final component is not so simple. Not only do different system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) calls interpret it quite differently (e.g. some create it, some do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) not), but it might not even exist: neither the empty pathname nor the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) pathname that is just slashes have a final component. If it does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) exist, it could be "``.``" or "``..``" which are handled quite differently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) from other components.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) .. _POSIX: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) If a pathname ends with a slash, such as "``/tmp/foo/``" it might be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) tempting to consider that to have an empty final component. In many
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) ways that would lead to correct results, but not always. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) particular, ``mkdir()`` and ``rmdir()`` each create or remove a directory named
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) by the final component, and they are required to work with pathnames
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) ending in "``/``". According to POSIX_:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) A pathname that contains at least one non-<slash> character and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) that ends with one or more trailing <slash> characters shall not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) be resolved successfully unless the last pathname component before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) the trailing <slash> characters names an existing directory or a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) directory entry that is to be created for a directory immediately
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) after the pathname is resolved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) The Linux pathname walking code (mostly in ``fs/namei.c``) deals with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) all of these issues: breaking the path into components, handling the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) "everything else" quite separately from the final component, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) checking that the trailing slash is not used where it isn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) permitted. It also addresses the important issue of concurrent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) access.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) While one process is looking up a pathname, another might be making
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) changes that affect that lookup. One fairly extreme case is that if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) "a/b" were renamed to "a/c/b" while another process were looking up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) "a/b/..", that process might successfully resolve on "a/c".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) Most races are much more subtle, and a big part of the task of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) pathname lookup is to prevent them from having damaging effects. Many
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) of the possible races are seen most clearly in the context of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) "dcache" and an understanding of that is central to understanding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) pathname lookup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) More than just a cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) ----------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) The "dcache" caches information about names in each filesystem to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) make them quickly available for lookup. Each entry (known as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) "dentry") contains three significant fields: a component name, a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) pointer to a parent dentry, and a pointer to the "inode" which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) contains further information about the object in that parent with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) the given name. The inode pointer can be ``NULL`` indicating that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) name doesn't exist in the parent. While there can be linkage in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) dentry of a directory to the dentries of the children, that linkage is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) not used for pathname lookup, and so will not be considered here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) The dcache has a number of uses apart from accelerating lookup. One
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) that will be particularly relevant is that it is closely integrated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) with the mount table that records which filesystem is mounted where.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) What the mount table actually stores is which dentry is mounted on top
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) of which other dentry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) When considering the dcache, we have another of our "two types"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) distinctions: there are two types of filesystems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) Some filesystems ensure that the information in the dcache is always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) completely accurate (though not necessarily complete). This can allow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) the VFS to determine if a particular file does or doesn't exist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) without checking with the filesystem, and means that the VFS can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) protect the filesystem against certain races and other problems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) These are typically "local" filesystems such as ext3, XFS, and Btrfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) Other filesystems don't provide that guarantee because they cannot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) These are typically filesystems that are shared across a network,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) whether remote filesystems like NFS and 9P, or cluster filesystems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) like ocfs2 or cephfs. These filesystems allow the VFS to revalidate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) cached information, and must provide their own protection against
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) awkward races. The VFS can detect these filesystems by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) ``DCACHE_OP_REVALIDATE`` flag being set in the dentry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) REF-walk: simple concurrency management with refcounts and spinlocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) --------------------------------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) With all of those divisions carefully classified, we can now start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) looking at the actual process of walking along a path. In particular
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) we will start with the handling of the "everything else" part of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) pathname, and focus on the "REF-walk" approach to concurrency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) management. This code is found in the ``link_path_walk()`` function, if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) you ignore all the places that only run when "``LOOKUP_RCU``"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) (indicating the use of RCU-walk) is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) .. _Meet the Lockers: https://lwn.net/Articles/453685/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) REF-walk is fairly heavy-handed with locks and reference counts. Not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) as heavy-handed as in the old "big kernel lock" days, but certainly not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) afraid of taking a lock when one is needed. It uses a variety of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) different concurrency controls. A background understanding of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) various primitives is assumed, or can be gleaned from elsewhere such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) as in `Meet the Lockers`_.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) The locking mechanisms used by REF-walk include:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) dentry->d_lockref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) This uses the lockref primitive to provide both a spinlock and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) reference count. The special-sauce of this primitive is that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) conceptual sequence "lock; inc_ref; unlock;" can often be performed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) with a single atomic memory operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) Holding a reference on a dentry ensures that the dentry won't suddenly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) be freed and used for something else, so the values in various fields
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) will behave as expected. It also protects the ``->d_inode`` reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) to the inode to some extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) The association between a dentry and its inode is fairly permanent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) For example, when a file is renamed, the dentry and inode move
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) together to the new location. When a file is created the dentry will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) initially be negative (i.e. ``d_inode`` is ``NULL``), and will be assigned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) to the new inode as part of the act of creation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) When a file is deleted, this can be reflected in the cache either by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) setting ``d_inode`` to ``NULL``, or by removing it from the hash table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) (described shortly) used to look up the name in the parent directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) If the dentry is still in use the second option is used as it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) perfectly legal to keep using an open file after it has been deleted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) and having the dentry around helps. If the dentry is not otherwise in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) use (i.e. if the refcount in ``d_lockref`` is one), only then will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) ``d_inode`` be set to ``NULL``. Doing it this way is more efficient for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) very common case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) So as long as a counted reference is held to a dentry, a non-``NULL`` ``->d_inode``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) value will never be changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) dentry->d_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) ~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) ``d_lock`` is a synonym for the spinlock that is part of ``d_lockref`` above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) For our purposes, holding this lock protects against the dentry being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) renamed or unlinked. In particular, its parent (``d_parent``), and its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) name (``d_name``) cannot be changed, and it cannot be removed from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) dentry hash table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) When looking for a name in a directory, REF-walk takes ``d_lock`` on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) each candidate dentry that it finds in the hash table and then checks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) that the parent and name are correct. So it doesn't lock the parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) while searching in the cache; it only locks children.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) When looking for the parent for a given name (to handle "``..``"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) REF-walk can take ``d_lock`` to get a stable reference to ``d_parent``,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) but it first tries a more lightweight approach. As seen in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) ``dget_parent()``, if a reference can be claimed on the parent, and if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) subsequently ``d_parent`` can be seen to have not changed, then there is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) no need to actually take the lock on the child.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) rename_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) ~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) Looking up a given name in a given directory involves computing a hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) from the two values (the name and the dentry of the directory),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) accessing that slot in a hash table, and searching the linked list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) that is found there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) When a dentry is renamed, the name and the parent dentry can both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) change so the hash will almost certainly change too. This would move the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) dentry to a different chain in the hash table. If a filename search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) happened to be looking at a dentry that was moved in this way,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) it might end up continuing the search down the wrong chain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) and so miss out on part of the correct chain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) The name-lookup process (``d_lookup()``) does *not* try to prevent this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) from happening, but only to detect when it happens.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) ``rename_lock`` is a seqlock that is updated whenever any dentry is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) renamed. If ``d_lookup`` finds that a rename happened while it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) unsuccessfully scanned a chain in the hash table, it simply tries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) ``rename_lock`` is also used to detect and defend against potential attacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) against ``LOOKUP_BENEATH`` and ``LOOKUP_IN_ROOT`` when resolving ".." (where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) the parent directory is moved outside the root, bypassing the ``path_equal()``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) check). If ``rename_lock`` is updated during the lookup and the path encounters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) a "..", a potential attack occurred and ``handle_dots()`` will bail out with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) ``-EAGAIN``.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) inode->i_rwsem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) ~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) ``i_rwsem`` is a read/write semaphore that serializes all changes to a particular
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) directory. This ensures that, for example, an ``unlink()`` and a ``rename()``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) cannot both happen at the same time. It also keeps the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) stable while the filesystem is asked to look up a name that is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) currently in the dcache or, optionally, when the list of entries in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) directory is being retrieved with ``readdir()``.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) This has a complementary role to that of ``d_lock``: ``i_rwsem`` on a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) directory protects all of the names in that directory, while ``d_lock``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) on a name protects just one name in a directory. Most changes to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) dcache hold ``i_rwsem`` on the relevant directory inode and briefly take
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) ``d_lock`` on one or more the dentries while the change happens. One
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) exception is when idle dentries are removed from the dcache due to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) memory pressure. This uses ``d_lock``, but ``i_rwsem`` plays no role.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) The semaphore affects pathname lookup in two distinct ways. Firstly it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) prevents changes during lookup of a name in a directory. ``walk_component()`` uses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) ``lookup_fast()`` first which, in turn, checks to see if the name is in the cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) using only ``d_lock`` locking. If the name isn't found, then ``walk_component()``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) falls back to ``lookup_slow()`` which takes a shared lock on ``i_rwsem``, checks again that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) the name isn't in the cache, and then calls in to the filesystem to get a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) definitive answer. A new dentry will be added to the cache regardless of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) the result.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) Secondly, when pathname lookup reaches the final component, it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) sometimes need to take an exclusive lock on ``i_rwsem`` before performing the last lookup so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) that the required exclusion can be achieved. How path lookup chooses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) to take, or not take, ``i_rwsem`` is one of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) issues addressed in a subsequent section.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) If two threads attempt to look up the same name at the same time - a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) name that is not yet in the dcache - the shared lock on ``i_rwsem`` will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) not prevent them both adding new dentries with the same name. As this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) would result in confusion an extra level of interlocking is used,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) based around a secondary hash table (``in_lookup_hashtable``) and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) per-dentry flag bit (``DCACHE_PAR_LOOKUP``).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) To add a new dentry to the cache while only holding a shared lock on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) ``i_rwsem``, a thread must call ``d_alloc_parallel()``. This allocates a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) dentry, stores the required name and parent in it, checks if there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) is already a matching dentry in the primary or secondary hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) tables, and if not, stores the newly allocated dentry in the secondary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) hash table, with ``DCACHE_PAR_LOOKUP`` set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) If a matching dentry was found in the primary hash table then that is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) returned and the caller can know that it lost a race with some other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) thread adding the entry. If no matching dentry is found in either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) cache, the newly allocated dentry is returned and the caller can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) detect this from the presence of ``DCACHE_PAR_LOOKUP``. In this case it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) knows that it has won any race and now is responsible for asking the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) filesystem to perform the lookup and find the matching inode. When
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) the lookup is complete, it must call ``d_lookup_done()`` which clears
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) the flag and does some other house keeping, including removing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) dentry from the secondary hash table - it will normally have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) added to the primary hash table already. Note that a ``struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) waitqueue_head`` is passed to ``d_alloc_parallel()``, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) ``d_lookup_done()`` must be called while this ``waitqueue_head`` is still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) in scope.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) If a matching dentry is found in the secondary hash table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) ``d_alloc_parallel()`` has a little more work to do. It first waits for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) ``DCACHE_PAR_LOOKUP`` to be cleared, using a wait_queue that was passed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) to the instance of ``d_alloc_parallel()`` that won the race and that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) will be woken by the call to ``d_lookup_done()``. It then checks to see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if the dentry has now been added to the primary hash table. If it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) has, the dentry is returned and the caller just sees that it lost any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) race. If it hasn't been added to the primary hash table, the most
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) likely explanation is that some other dentry was added instead using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) ``d_splice_alias()``. In any case, ``d_alloc_parallel()`` repeats all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) look ups from the start and will normally return something from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) primary hash table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) mnt->mnt_count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) ~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) ``mnt_count`` is a per-CPU reference counter on "``mount``" structures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) Per-CPU here means that incrementing the count is cheap as it only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) uses CPU-local memory, but checking if the count is zero is expensive as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) it needs to check with every CPU. Taking a ``mnt_count`` reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) prevents the mount structure from disappearing as the result of regular
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) unmount operations, but does not prevent a "lazy" unmount. So holding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) ``mnt_count`` doesn't ensure that the mount remains in the namespace and,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) in particular, doesn't stabilize the link to the mounted-on dentry. It
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) does, however, ensure that the ``mount`` data structure remains coherent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) and it provides a reference to the root dentry of the mounted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) filesystem. So a reference through ``->mnt_count`` provides a stable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) reference to the mounted dentry, but not the mounted-on dentry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) mount_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) ~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) ``mount_lock`` is a global seqlock, a bit like ``rename_lock``. It can be used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) check if any change has been made to any mount points.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) While walking down the tree (away from the root) this lock is used when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) crossing a mount point to check that the crossing was safe. That is,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) the value in the seqlock is read, then the code finds the mount that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) is mounted on the current directory, if there is one, and increments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) the ``mnt_count``. Finally the value in ``mount_lock`` is checked against
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) the old value. If there is no change, then the crossing was safe. If there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) was a change, the ``mnt_count`` is decremented and the whole process is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) retried.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) When walking up the tree (towards the root) by following a ".." link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) a little more care is needed. In this case the seqlock (which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) contains both a counter and a spinlock) is fully locked to prevent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) any changes to any mount points while stepping up. This locking is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) needed to stabilize the link to the mounted-on dentry, which the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) refcount on the mount itself doesn't ensure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) ``mount_lock`` is also used to detect and defend against potential attacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) against ``LOOKUP_BENEATH`` and ``LOOKUP_IN_ROOT`` when resolving ".." (where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) the parent directory is moved outside the root, bypassing the ``path_equal()``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) check). If ``mount_lock`` is updated during the lookup and the path encounters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) a "..", a potential attack occurred and ``handle_dots()`` will bail out with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) ``-EAGAIN``.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) RCU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) ~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) Finally the global (but extremely lightweight) RCU read lock is held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) from time to time to ensure certain data structures don't get freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) unexpectedly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) In particular it is held while scanning chains in the dcache hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) table, and the mount point hash table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) Bringing it together with ``struct nameidata``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) ----------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) .. _First edition Unix: https://minnie.tuhs.org/cgi-bin/utree.pl?file=V1/u2.s
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) Throughout the process of walking a path, the current status is stored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) in a ``struct nameidata``, "namei" being the traditional name - dating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) all the way back to `First Edition Unix`_ - of the function that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) converts a "name" to an "inode". ``struct nameidata`` contains (among
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) other fields):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) ``struct path path``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) ~~~~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) A ``path`` contains a ``struct vfsmount`` (which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) embedded in a ``struct mount``) and a ``struct dentry``. Together these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) record the current status of the walk. They start out referring to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) starting point (the current working directory, the root directory, or some other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) directory identified by a file descriptor), and are updated on each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) step. A reference through ``d_lockref`` and ``mnt_count`` is always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) ``struct qstr last``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) ~~~~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) This is a string together with a length (i.e. *not* ``nul`` terminated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) that is the "next" component in the pathname.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) ``int last_type``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) ~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) This is one of ``LAST_NORM``, ``LAST_ROOT``, ``LAST_DOT`` or ``LAST_DOTDOT``.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) The ``last`` field is only valid if the type is ``LAST_NORM``.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) ``struct path root``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) ~~~~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) This is used to hold a reference to the effective root of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) filesystem. Often that reference won't be needed, so this field is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) only assigned the first time it is used, or when a non-standard root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) is requested. Keeping a reference in the ``nameidata`` ensures that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) only one root is in effect for the entire path walk, even if it races
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) with a ``chroot()`` system call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) It should be noted that in the case of ``LOOKUP_IN_ROOT`` or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) ``LOOKUP_BENEATH``, the effective root becomes the directory file descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) passed to ``openat2()`` (which exposes these ``LOOKUP_`` flags).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) The root is needed when either of two conditions holds: (1) either the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) pathname or a symbolic link starts with a "'/'", or (2) a "``..``"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) component is being handled, since "``..``" from the root must always stay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) at the root. The value used is usually the current root directory of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) the calling process. An alternate root can be provided as when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) ``sysctl()`` calls ``file_open_root()``, and when NFSv4 or Btrfs call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) ``mount_subtree()``. In each case a pathname is being looked up in a very
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) specific part of the filesystem, and the lookup must not be allowed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) escape that subtree. It works a bit like a local ``chroot()``.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) Ignoring the handling of symbolic links, we can now describe the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) "``link_path_walk()``" function, which handles the lookup of everything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) except the final component as:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) Given a path (``name``) and a nameidata structure (``nd``), check that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) current directory has execute permission and then advance ``name``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) over one component while updating ``last_type`` and ``last``. If that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) was the final component, then return, otherwise call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) ``walk_component()`` and repeat from the top.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) ``walk_component()`` is even easier. If the component is ``LAST_DOTS``,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) it calls ``handle_dots()`` which does the necessary locking as already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) described. If it finds a ``LAST_NORM`` component it first calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) "``lookup_fast()``" which only looks in the dcache, but will ask the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) filesystem to revalidate the result if it is that sort of filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) If that doesn't get a good result, it calls "``lookup_slow()``" which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) takes ``i_rwsem``, rechecks the cache, and then asks the filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) to find a definitive answer. Each of these will call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) ``follow_managed()`` (as described below) to handle any mount points.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) In the absence of symbolic links, ``walk_component()`` creates a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) ``struct path`` containing a counted reference to the new dentry and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) reference to the new ``vfsmount`` which is only counted if it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) different from the previous ``vfsmount``. It then calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) ``path_to_nameidata()`` to install the new ``struct path`` in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) ``struct nameidata`` and drop the unneeded references.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) This "hand-over-hand" sequencing of getting a reference to the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) dentry before dropping the reference to the previous dentry may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) seem obvious, but is worth pointing out so that we will recognize its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) analogue in the "RCU-walk" version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) Handling the final component
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) ----------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) ``link_path_walk()`` only walks as far as setting ``nd->last`` and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) ``nd->last_type`` to refer to the final component of the path. It does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) not call ``walk_component()`` that last time. Handling that final
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) component remains for the caller to sort out. Those callers are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) ``path_lookupat()``, ``path_parentat()``, ``path_mountpoint()`` and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) ``path_openat()`` each of which handles the differing requirements of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) different system calls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) ``path_parentat()`` is clearly the simplest - it just wraps a little bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) of housekeeping around ``link_path_walk()`` and returns the parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) directory and final component to the caller. The caller will be either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) aiming to create a name (via ``filename_create()``) or remove or rename
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) a name (in which case ``user_path_parent()`` is used). They will use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) ``i_rwsem`` to exclude other changes while they validate and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) perform their operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) ``path_lookupat()`` is nearly as simple - it is used when an existing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) object is wanted such as by ``stat()`` or ``chmod()``. It essentially just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) calls ``walk_component()`` on the final component through a call to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) ``lookup_last()``. ``path_lookupat()`` returns just the final dentry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) ``path_mountpoint()`` handles the special case of unmounting which must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) not try to revalidate the mounted filesystem. It effectively
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) contains, through a call to ``mountpoint_last()``, an alternate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) implementation of ``lookup_slow()`` which skips that step. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) important when unmounting a filesystem that is inaccessible, such as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) one provided by a dead NFS server.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) Finally ``path_openat()`` is used for the ``open()`` system call; it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) contains, in support functions starting with "``do_last()``", all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) complexity needed to handle the different subtleties of O_CREAT (with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) or without O_EXCL), final "``/``" characters, and trailing symbolic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) links. We will revisit this in the final part of this series, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) focuses on those symbolic links. "``do_last()``" will sometimes, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) not always, take ``i_rwsem``, depending on what it finds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) Each of these, or the functions which call them, need to be alert to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) the possibility that the final component is not ``LAST_NORM``. If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) goal of the lookup is to create something, then any value for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) ``last_type`` other than ``LAST_NORM`` will result in an error. For
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) example if ``path_parentat()`` reports ``LAST_DOTDOT``, then the caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) won't try to create that name. They also check for trailing slashes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) by testing ``last.name[last.len]``. If there is any character beyond
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) the final component, it must be a trailing slash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) Revalidation and automounts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) ---------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) Apart from symbolic links, there are only two parts of the "REF-walk"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) process not yet covered. One is the handling of stale cache entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) and the other is automounts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) On filesystems that require it, the lookup routines will call the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) ``->d_revalidate()`` dentry method to ensure that the cached information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) is current. This will often confirm validity or update a few details
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) from a server. In some cases it may find that there has been change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) further up the path and that something that was thought to be valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) previously isn't really. When this happens the lookup of the whole
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) path is aborted and retried with the "``LOOKUP_REVAL``" flag set. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) forces revalidation to be more thorough. We will see more details of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) this retry process in the next article.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) Automount points are locations in the filesystem where an attempt to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) lookup a name can trigger changes to how that lookup should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) handled, in particular by mounting a filesystem there. These are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) covered in greater detail in autofs.txt in the Linux documentation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) tree, but a few notes specifically related to path lookup are in order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) The Linux VFS has a concept of "managed" dentries which is reflected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) in function names such as "``follow_managed()``". There are three
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) potentially interesting things about these dentries corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) to three different flags that might be set in ``dentry->d_flags``:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) ``DCACHE_MANAGE_TRANSIT``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) ~~~~~~~~~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) If this flag has been set, then the filesystem has requested that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) ``d_manage()`` dentry operation be called before handling any possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) mount point. This can perform two particular services:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) It can block to avoid races. If an automount point is being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) unmounted, the ``d_manage()`` function will usually wait for that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) process to complete before letting the new lookup proceed and possibly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) trigger a new automount.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) It can selectively allow only some processes to transit through a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) mount point. When a server process is managing automounts, it may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) need to access a directory without triggering normal automount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) processing. That server process can identify itself to the ``autofs``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) filesystem, which will then give it a special pass through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) ``d_manage()`` by returning ``-EISDIR``.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) ``DCACHE_MOUNTED``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) ~~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) This flag is set on every dentry that is mounted on. As Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) supports multiple filesystem namespaces, it is possible that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) dentry may not be mounted on in *this* namespace, just in some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) other. So this flag is seen as a hint, not a promise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) If this flag is set, and ``d_manage()`` didn't return ``-EISDIR``,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) ``lookup_mnt()`` is called to examine the mount hash table (honoring the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) ``mount_lock`` described earlier) and possibly return a new ``vfsmount``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) and a new ``dentry`` (both with counted references).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) ``DCACHE_NEED_AUTOMOUNT``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) ~~~~~~~~~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) If ``d_manage()`` allowed us to get this far, and ``lookup_mnt()`` didn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) find a mount point, then this flag causes the ``d_automount()`` dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) operation to be called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) The ``d_automount()`` operation can be arbitrarily complex and may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) communicate with server processes etc. but it should ultimately either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) report that there was an error, that there was nothing to mount, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) should provide an updated ``struct path`` with new ``dentry`` and ``vfsmount``.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) In the latter case, ``finish_automount()`` will be called to safely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) install the new mount point into the mount table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) There is no new locking of import here and it is important that no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) locks (only counted references) are held over this processing due to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) the very real possibility of extended delays.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) This will become more important next time when we examine RCU-walk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) which is particularly sensitive to delays.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) RCU-walk - faster pathname lookup in Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) ==========================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) RCU-walk is another algorithm for performing pathname lookup in Linux.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) It is in many ways similar to REF-walk and the two share quite a bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) of code. The significant difference in RCU-walk is how it allows for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) the possibility of concurrent access.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) We noted that REF-walk is complex because there are numerous details
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) and special cases. RCU-walk reduces this complexity by simply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) refusing to handle a number of cases -- it instead falls back to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) REF-walk. The difficulty with RCU-walk comes from a different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) direction: unfamiliarity. The locking rules when depending on RCU are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) quite different from traditional locking, so we will spend a little extra
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) time when we come to those.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) Clear demarcation of roles
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) --------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) The easiest way to manage concurrency is to forcibly stop any other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) thread from changing the data structures that a given thread is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) looking at. In cases where no other thread would even think of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) changing the data and lots of different threads want to read at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) same time, this can be very costly. Even when using locks that permit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) multiple concurrent readers, the simple act of updating the count of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) the number of current readers can impose an unwanted cost. So the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) goal when reading a shared data structure that no other process is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) changing is to avoid writing anything to memory at all. Take no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) locks, increment no counts, leave no footprints.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) The REF-walk mechanism already described certainly doesn't follow this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) principle, but then it is really designed to work when there may well
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) be other threads modifying the data. RCU-walk, in contrast, is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) designed for the common situation where there are lots of frequent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) readers and only occasional writers. This may not be common in all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) parts of the filesystem tree, but in many parts it will be. For the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) other parts it is important that RCU-walk can quickly fall back to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) using REF-walk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) Pathname lookup always starts in RCU-walk mode but only remains there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) as long as what it is looking for is in the cache and is stable. It
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) dances lightly down the cached filesystem image, leaving no footprints
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) and carefully watching where it is, to be sure it doesn't trip. If it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) notices that something has changed or is changing, or if something
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) isn't in the cache, then it tries to stop gracefully and switch to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) REF-walk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) This stopping requires getting a counted reference on the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) ``vfsmount`` and ``dentry``, and ensuring that these are still valid -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) that a path walk with REF-walk would have found the same entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) This is an invariant that RCU-walk must guarantee. It can only make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) decisions, such as selecting the next step, that are decisions which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) REF-walk could also have made if it were walking down the tree at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) same time. If the graceful stop succeeds, the rest of the path is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) processed with the reliable, if slightly sluggish, REF-walk. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) RCU-walk finds it cannot stop gracefully, it simply gives up and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) restarts from the top with REF-walk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) This pattern of "try RCU-walk, if that fails try REF-walk" can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) clearly seen in functions like ``filename_lookup()``,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) ``filename_parentat()``, ``filename_mountpoint()``,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) ``do_filp_open()``, and ``do_file_open_root()``. These five
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) correspond roughly to the four ``path_*()`` functions we met earlier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) each of which calls ``link_path_walk()``. The ``path_*()`` functions are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) called using different mode flags until a mode is found which works.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) They are first called with ``LOOKUP_RCU`` set to request "RCU-walk". If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) that fails with the error ``ECHILD`` they are called again with no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) special flag to request "REF-walk". If either of those report the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) error ``ESTALE`` a final attempt is made with ``LOOKUP_REVAL`` set (and no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) ``LOOKUP_RCU``) to ensure that entries found in the cache are forcibly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) revalidated - normally entries are only revalidated if the filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) determines that they are too old to trust.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) The ``LOOKUP_RCU`` attempt may drop that flag internally and switch to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) REF-walk, but will never then try to switch back to RCU-walk. Places
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) that trip up RCU-walk are much more likely to be near the leaves and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) so it is very unlikely that there will be much, if any, benefit from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) switching back.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) RCU and seqlocks: fast and light
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) --------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) RCU is, unsurprisingly, critical to RCU-walk mode. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) ``rcu_read_lock()`` is held for the entire time that RCU-walk is walking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) down a path. The particular guarantee it provides is that the key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) data structures - dentries, inodes, super_blocks, and mounts - will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) not be freed while the lock is held. They might be unlinked or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) invalidated in one way or another, but the memory will not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) repurposed so values in various fields will still be meaningful. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) is the only guarantee that RCU provides; everything else is done using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) seqlocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) As we saw above, REF-walk holds a counted reference to the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) dentry and the current vfsmount, and does not release those references
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) before taking references to the "next" dentry or vfsmount. It also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) sometimes takes the ``d_lock`` spinlock. These references and locks are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) taken to prevent certain changes from happening. RCU-walk must not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) take those references or locks and so cannot prevent such changes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) Instead, it checks to see if a change has been made, and aborts or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) retries if it has.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) To preserve the invariant mentioned above (that RCU-walk may only make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) decisions that REF-walk could have made), it must make the checks at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) or near the same places that REF-walk holds the references. So, when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) REF-walk increments a reference count or takes a spinlock, RCU-walk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) samples the status of a seqlock using ``read_seqcount_begin()`` or a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) similar function. When REF-walk decrements the count or drops the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) lock, RCU-walk checks if the sampled status is still valid using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) ``read_seqcount_retry()`` or similar.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) However, there is a little bit more to seqlocks than that. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) RCU-walk accesses two different fields in a seqlock-protected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) structure, or accesses the same field twice, there is no a priori
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) guarantee of any consistency between those accesses. When consistency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) is needed - which it usually is - RCU-walk must take a copy and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) use ``read_seqcount_retry()`` to validate that copy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) ``read_seqcount_retry()`` not only checks the sequence number, but also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) imposes a memory barrier so that no memory-read instruction from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) *before* the call can be delayed until *after* the call, either by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) CPU or by the compiler. A simple example of this can be seen in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) ``slow_dentry_cmp()`` which, for filesystems which do not use simple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) byte-wise name equality, calls into the filesystem to compare a name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) against a dentry. The length and name pointer are copied into local
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) variables, then ``read_seqcount_retry()`` is called to confirm the two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) are consistent, and only then is ``->d_compare()`` called. When
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) standard filename comparison is used, ``dentry_cmp()`` is called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) instead. Notably it does *not* use ``read_seqcount_retry()``, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) instead has a large comment explaining why the consistency guarantee
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) isn't necessary. A subsequent ``read_seqcount_retry()`` will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) sufficient to catch any problem that could occur at this point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) With that little refresher on seqlocks out of the way we can look at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) the bigger picture of how RCU-walk uses seqlocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) ``mount_lock`` and ``nd->m_seq``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) We already met the ``mount_lock`` seqlock when REF-walk used it to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) ensure that crossing a mount point is performed safely. RCU-walk uses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) it for that too, but for quite a bit more.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) Instead of taking a counted reference to each ``vfsmount`` as it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) descends the tree, RCU-walk samples the state of ``mount_lock`` at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) start of the walk and stores this initial sequence number in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) ``struct nameidata`` in the ``m_seq`` field. This one lock and one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) sequence number are used to validate all accesses to all ``vfsmounts``,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) and all mount point crossings. As changes to the mount table are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) relatively rare, it is reasonable to fall back on REF-walk any time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) that any "mount" or "unmount" happens.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) ``m_seq`` is checked (using ``read_seqretry()``) at the end of an RCU-walk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) sequence, whether switching to REF-walk for the rest of the path or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) when the end of the path is reached. It is also checked when stepping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) down over a mount point (in ``__follow_mount_rcu()``) or up (in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) ``follow_dotdot_rcu()``). If it is ever found to have changed, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) whole RCU-walk sequence is aborted and the path is processed again by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) REF-walk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) If RCU-walk finds that ``mount_lock`` hasn't changed then it can be sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) that, had REF-walk taken counted references on each vfsmount, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) results would have been the same. This ensures the invariant holds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) at least for vfsmount structures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) ``dentry->d_seq`` and ``nd->seq``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) In place of taking a count or lock on ``d_reflock``, RCU-walk samples
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) the per-dentry ``d_seq`` seqlock, and stores the sequence number in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) ``seq`` field of the nameidata structure, so ``nd->seq`` should always be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) the current sequence number of ``nd->dentry``. This number needs to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) revalidated after copying, and before using, the name, parent, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) inode of the dentry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) The handling of the name we have already looked at, and the parent is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) only accessed in ``follow_dotdot_rcu()`` which fairly trivially follows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) the required pattern, though it does so for three different cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) When not at a mount point, ``d_parent`` is followed and its ``d_seq`` is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) collected. When we are at a mount point, we instead follow the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) ``mnt->mnt_mountpoint`` link to get a new dentry and collect its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) ``d_seq``. Then, after finally finding a ``d_parent`` to follow, we must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) check if we have landed on a mount point and, if so, must find that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) mount point and follow the ``mnt->mnt_root`` link. This would imply a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) somewhat unusual, but certainly possible, circumstance where the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) starting point of the path lookup was in part of the filesystem that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) was mounted on, and so not visible from the root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) The inode pointer, stored in ``->d_inode``, is a little more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) interesting. The inode will always need to be accessed at least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) twice, once to determine if it is NULL and once to verify access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) permissions. Symlink handling requires a validated inode pointer too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) Rather than revalidating on each access, a copy is made on the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) access and it is stored in the ``inode`` field of ``nameidata`` from where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) it can be safely accessed without further validation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) ``lookup_fast()`` is the only lookup routine that is used in RCU-mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) ``lookup_slow()`` being too slow and requiring locks. It is in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) ``lookup_fast()`` that we find the important "hand over hand" tracking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) of the current dentry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) The current ``dentry`` and current ``seq`` number are passed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) ``__d_lookup_rcu()`` which, on success, returns a new ``dentry`` and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) new ``seq`` number. ``lookup_fast()`` then copies the inode pointer and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) revalidates the new ``seq`` number. It then validates the old ``dentry``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) with the old ``seq`` number one last time and only then continues. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) process of getting the ``seq`` number of the new dentry and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) checking the ``seq`` number of the old exactly mirrors the process of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) getting a counted reference to the new dentry before dropping that for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) the old dentry which we saw in REF-walk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) No ``inode->i_rwsem`` or even ``rename_lock``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) A semaphore is a fairly heavyweight lock that can only be taken when it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) permissible to sleep. As ``rcu_read_lock()`` forbids sleeping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) ``inode->i_rwsem`` plays no role in RCU-walk. If some other thread does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) take ``i_rwsem`` and modifies the directory in a way that RCU-walk needs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) to notice, the result will be either that RCU-walk fails to find the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) dentry that it is looking for, or it will find a dentry which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) ``read_seqretry()`` won't validate. In either case it will drop down to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) REF-walk mode which can take whatever locks are needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) Though ``rename_lock`` could be used by RCU-walk as it doesn't require
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) any sleeping, RCU-walk doesn't bother. REF-walk uses ``rename_lock`` to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) protect against the possibility of hash chains in the dcache changing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) while they are being searched. This can result in failing to find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) something that actually is there. When RCU-walk fails to find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) something in the dentry cache, whether it is really there or not, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) already drops down to REF-walk and tries again with appropriate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) locking. This neatly handles all cases, so adding extra checks on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) rename_lock would bring no significant value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) ``unlazy walk()`` and ``complete_walk()``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) -----------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) That "dropping down to REF-walk" typically involves a call to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) ``unlazy_walk()``, so named because "RCU-walk" is also sometimes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) referred to as "lazy walk". ``unlazy_walk()`` is called when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) following the path down to the current vfsmount/dentry pair seems to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) have proceeded successfully, but the next step is problematic. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) can happen if the next name cannot be found in the dcache, if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) permission checking or name revalidation couldn't be achieved while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) the ``rcu_read_lock()`` is held (which forbids sleeping), if an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) automount point is found, or in a couple of cases involving symlinks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) It is also called from ``complete_walk()`` when the lookup has reached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) the final component, or the very end of the path, depending on which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) particular flavor of lookup is used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) Other reasons for dropping out of RCU-walk that do not trigger a call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) to ``unlazy_walk()`` are when some inconsistency is found that cannot be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) handled immediately, such as ``mount_lock`` or one of the ``d_seq``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) seqlocks reporting a change. In these cases the relevant function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) will return ``-ECHILD`` which will percolate up until it triggers a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) attempt from the top using REF-walk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) For those cases where ``unlazy_walk()`` is an option, it essentially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) takes a reference on each of the pointers that it holds (vfsmount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) dentry, and possibly some symbolic links) and then verifies that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) relevant seqlocks have not been changed. If there have been changes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) it, too, aborts with ``-ECHILD``, otherwise the transition to REF-walk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) has been a success and the lookup process continues.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) Taking a reference on those pointers is not quite as simple as just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) incrementing a counter. That works to take a second reference if you
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) already have one (often indirectly through another object), but it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) isn't sufficient if you don't actually have a counted reference at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) all. For ``dentry->d_lockref``, it is safe to increment the reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) counter to get a reference unless it has been explicitly marked as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) "dead" which involves setting the counter to ``-128``.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) ``lockref_get_not_dead()`` achieves this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) For ``mnt->mnt_count`` it is safe to take a reference as long as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) ``mount_lock`` is then used to validate the reference. If that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) validation fails, it may *not* be safe to just drop that reference in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) the standard way of calling ``mnt_put()`` - an unmount may have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) progressed too far. So the code in ``legitimize_mnt()``, when it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) finds that the reference it got might not be safe, checks the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) ``MNT_SYNC_UMOUNT`` flag to determine if a simple ``mnt_put()`` is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) correct, or if it should just decrement the count and pretend none of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) this ever happened.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) Taking care in filesystems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) --------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) RCU-walk depends almost entirely on cached information and often will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) not call into the filesystem at all. However there are two places,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) besides the already-mentioned component-name comparison, where the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) file system might be included in RCU-walk, and it must know to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) careful.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) If the filesystem has non-standard permission-checking requirements -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) such as a networked filesystem which may need to check with the server
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) - the ``i_op->permission`` interface might be called during RCU-walk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) In this case an extra "``MAY_NOT_BLOCK``" flag is passed so that it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) knows not to sleep, but to return ``-ECHILD`` if it cannot complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) promptly. ``i_op->permission`` is given the inode pointer, not the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) dentry, so it doesn't need to worry about further consistency checks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) However if it accesses any other filesystem data structures, it must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) ensure they are safe to be accessed with only the ``rcu_read_lock()``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) held. This typically means they must be freed using ``kfree_rcu()`` or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) similar.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) .. _READ_ONCE: https://lwn.net/Articles/624126/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) If the filesystem may need to revalidate dcache entries, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) ``d_op->d_revalidate`` may be called in RCU-walk too. This interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) *is* passed the dentry but does not have access to the ``inode`` or the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) ``seq`` number from the ``nameidata``, so it needs to be extra careful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) when accessing fields in the dentry. This "extra care" typically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) involves using `READ_ONCE() <READ_ONCE_>`_ to access fields, and verifying the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) result is not NULL before using it. This pattern can be seen in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) ``nfs_lookup_revalidate()``.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) A pair of patterns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) ------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) In various places in the details of REF-walk and RCU-walk, and also in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) the big picture, there are a couple of related patterns that are worth
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) being aware of.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) The first is "try quickly and check, if that fails try slowly". We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) can see that in the high-level approach of first trying RCU-walk and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) then trying REF-walk, and in places where ``unlazy_walk()`` is used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) switch to REF-walk for the rest of the path. We also saw it earlier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) in ``dget_parent()`` when following a "``..``" link. It tries a quick way
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) to get a reference, then falls back to taking locks if needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) The second pattern is "try quickly and check, if that fails try
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) again - repeatedly". This is seen with the use of ``rename_lock`` and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) ``mount_lock`` in REF-walk. RCU-walk doesn't make use of this pattern -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) if anything goes wrong it is much safer to just abort and try a more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) sedate approach.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) The emphasis here is "try quickly and check". It should probably be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) "try quickly *and carefully*, then check". The fact that checking is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) needed is a reminder that the system is dynamic and only a limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) number of things are safe at all. The most likely cause of errors in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) this whole process is assuming something is safe when in reality it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) isn't. Careful consideration of what exactly guarantees the safety of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) each access is sometimes necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) A walk among the symlinks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) =========================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) There are several basic issues that we will examine to understand the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) handling of symbolic links: the symlink stack, together with cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) lifetimes, will help us understand the overall recursive handling of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) symlinks and lead to the special care needed for the final component.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) Then a consideration of access-time updates and summary of the various
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) flags controlling lookup will finish the story.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) The symlink stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) -----------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) There are only two sorts of filesystem objects that can usefully
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) appear in a path prior to the final component: directories and symlinks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) Handling directories is quite straightforward: the new directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) simply becomes the starting point at which to interpret the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) component on the path. Handling symbolic links requires a bit more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) Conceptually, symbolic links could be handled by editing the path. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) a component name refers to a symbolic link, then that component is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) replaced by the body of the link and, if that body starts with a '/',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) then all preceding parts of the path are discarded. This is what the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) "``readlink -f``" command does, though it also edits out "``.``" and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) "``..``" components.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) Directly editing the path string is not really necessary when looking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) up a path, and discarding early components is pointless as they aren't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) looked at anyway. Keeping track of all remaining components is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) important, but they can of course be kept separately; there is no need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) to concatenate them. As one symlink may easily refer to another,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) which in turn can refer to a third, we may need to keep the remaining
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) components of several paths, each to be processed when the preceding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) ones are completed. These path remnants are kept on a stack of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) limited size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) There are two reasons for placing limits on how many symlinks can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) occur in a single path lookup. The most obvious is to avoid loops.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) If a symlink referred to itself either directly or through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) intermediaries, then following the symlink can never complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) successfully - the error ``ELOOP`` must be returned. Loops can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) detected without imposing limits, but limits are the simplest solution
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) and, given the second reason for restriction, quite sufficient.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) .. _outlined recently: http://thread.gmane.org/gmane.linux.kernel/1934390/focus=1934550
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) The second reason was `outlined recently`_ by Linus:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) Because it's a latency and DoS issue too. We need to react well to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) true loops, but also to "very deep" non-loops. It's not about memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) use, it's about users triggering unreasonable CPU resources.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) Linux imposes a limit on the length of any pathname: ``PATH_MAX``, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) is 4096. There are a number of reasons for this limit; not letting the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) kernel spend too much time on just one path is one of them. With
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) symbolic links you can effectively generate much longer paths so some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) sort of limit is needed for the same reason. Linux imposes a limit of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) at most 40 symlinks in any one path lookup. It previously imposed a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) further limit of eight on the maximum depth of recursion, but that was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) raised to 40 when a separate stack was implemented, so there is now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) just the one limit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) The ``nameidata`` structure that we met in an earlier article contains a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) small stack that can be used to store the remaining part of up to two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) symlinks. In many cases this will be sufficient. If it isn't, a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) separate stack is allocated with room for 40 symlinks. Pathname
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) lookup will never exceed that stack as, once the 40th symlink is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) detected, an error is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) It might seem that the name remnants are all that needs to be stored on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) this stack, but we need a bit more. To see that, we need to move on to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) cache lifetimes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) Storage and lifetime of cached symlinks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) ---------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) Like other filesystem resources, such as inodes and directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) entries, symlinks are cached by Linux to avoid repeated costly access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) to external storage. It is particularly important for RCU-walk to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) able to find and temporarily hold onto these cached entries, so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) it doesn't need to drop down into REF-walk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) .. _object-oriented design pattern: https://lwn.net/Articles/446317/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) While each filesystem is free to make its own choice, symlinks are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) typically stored in one of two places. Short symlinks are often
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) stored directly in the inode. When a filesystem allocates a ``struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) inode`` it typically allocates extra space to store private data (a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) common `object-oriented design pattern`_ in the kernel). This will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) sometimes include space for a symlink. The other common location is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) in the page cache, which normally stores the content of files. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) pathname in a symlink can be seen as the content of that symlink and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) can easily be stored in the page cache just like file content.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) When neither of these is suitable, the next most likely scenario is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) that the filesystem will allocate some temporary memory and copy or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) construct the symlink content into that memory whenever it is needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) When the symlink is stored in the inode, it has the same lifetime as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) the inode which, itself, is protected by RCU or by a counted reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) on the dentry. This means that the mechanisms that pathname lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) uses to access the dcache and icache (inode cache) safely are quite
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) sufficient for accessing some cached symlinks safely. In these cases,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) the ``i_link`` pointer in the inode is set to point to wherever the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) symlink is stored and it can be accessed directly whenever needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) When the symlink is stored in the page cache or elsewhere, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) situation is not so straightforward. A reference on a dentry or even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) on an inode does not imply any reference on cached pages of that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) inode, and even an ``rcu_read_lock()`` is not sufficient to ensure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) a page will not disappear. So for these symlinks the pathname lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) code needs to ask the filesystem to provide a stable reference and,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) significantly, needs to release that reference when it is finished
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) with it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) Taking a reference to a cache page is often possible even in RCU-walk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) mode. It does require making changes to memory, which is best avoided,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) but that isn't necessarily a big cost and it is better than dropping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) out of RCU-walk mode completely. Even filesystems that allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) space to copy the symlink into can use ``GFP_ATOMIC`` to often successfully
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) allocate memory without the need to drop out of RCU-walk. If a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) filesystem cannot successfully get a reference in RCU-walk mode, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) must return ``-ECHILD`` and ``unlazy_walk()`` will be called to return to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) REF-walk mode in which the filesystem is allowed to sleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) The place for all this to happen is the ``i_op->follow_link()`` inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) method. In the present mainline code this is never actually called in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) RCU-walk mode as the rewrite is not quite complete. It is likely that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) in a future release this method will be passed an ``inode`` pointer when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) called in RCU-walk mode so it both (1) knows to be careful, and (2) has the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) validated pointer. Much like the ``i_op->permission()`` method we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) looked at previously, ``->follow_link()`` would need to be careful that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) all the data structures it references are safe to be accessed while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) holding no counted reference, only the RCU lock. Though getting a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) reference with ``->follow_link()`` is not yet done in RCU-walk mode, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) code is ready to release the reference when that does happen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) This need to drop the reference to a symlink adds significant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) complexity. It requires a reference to the inode so that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) ``i_op->put_link()`` inode operation can be called. In REF-walk, that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) reference is kept implicitly through a reference to the dentry, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) keeping the ``struct path`` of the symlink is easiest. For RCU-walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) the pointer to the inode is kept separately. To allow switching from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) RCU-walk back to REF-walk in the middle of processing nested symlinks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) we also need the seq number for the dentry so we can confirm that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) switching back was safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) Finally, when providing a reference to a symlink, the filesystem also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) provides an opaque "cookie" that must be passed to ``->put_link()`` so that it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) knows what to free. This might be the allocated memory area, or a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) pointer to the ``struct page`` in the page cache, or something else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) completely. Only the filesystem knows what it is.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) In order for the reference to each symlink to be dropped when the walk completes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) whether in RCU-walk or REF-walk, the symlink stack needs to contain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) along with the path remnants:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) - the ``struct path`` to provide a reference to the inode in REF-walk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) - the ``struct inode *`` to provide a reference to the inode in RCU-walk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) - the ``seq`` to allow the path to be safely switched from RCU-walk to REF-walk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) - the ``cookie`` that tells ``->put_path()`` what to put.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) This means that each entry in the symlink stack needs to hold five
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) pointers and an integer instead of just one pointer (the path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) remnant). On a 64-bit system, this is about 40 bytes per entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) with 40 entries it adds up to 1600 bytes total, which is less than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) half a page. So it might seem like a lot, but is by no means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) excessive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) Note that, in a given stack frame, the path remnant (``name``) is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) part of the symlink that the other fields refer to. It is the remnant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) to be followed once that symlink has been fully parsed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) Following the symlink
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) ---------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) The main loop in ``link_path_walk()`` iterates seamlessly over all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) components in the path and all of the non-final symlinks. As symlinks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) are processed, the ``name`` pointer is adjusted to point to a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) symlink, or is restored from the stack, so that much of the loop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) doesn't need to notice. Getting this ``name`` variable on and off the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) stack is very straightforward; pushing and popping the references is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) a little more complex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) When a symlink is found, ``walk_component()`` returns the value ``1``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) (``0`` is returned for any other sort of success, and a negative number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) is, as usual, an error indicator). This causes ``get_link()`` to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) called; it then gets the link from the filesystem. Providing that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) operation is successful, the old path ``name`` is placed on the stack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) and the new value is used as the ``name`` for a while. When the end of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) the path is found (i.e. ``*name`` is ``'\0'``) the old ``name`` is restored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) off the stack and path walking continues.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) Pushing and popping the reference pointers (inode, cookie, etc.) is more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) complex in part because of the desire to handle tail recursion. When
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) the last component of a symlink itself points to a symlink, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) want to pop the symlink-just-completed off the stack before pushing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) the symlink-just-found to avoid leaving empty path remnants that would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) just get in the way.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) It is most convenient to push the new symlink references onto the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) stack in ``walk_component()`` immediately when the symlink is found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) ``walk_component()`` is also the last piece of code that needs to look at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) old symlink as it walks that last component. So it is quite
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) convenient for ``walk_component()`` to release the old symlink and pop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) the references just before pushing the reference information for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) new symlink. It is guided in this by two flags; ``WALK_GET``, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) gives it permission to follow a symlink if it finds one, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) ``WALK_PUT``, which tells it to release the current symlink after it has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) followed. ``WALK_PUT`` is tested first, leading to a call to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) ``put_link()``. ``WALK_GET`` is tested subsequently (by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) ``should_follow_link()``) leading to a call to ``pick_link()`` which sets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) up the stack frame.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) Symlinks with no final component
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) A pair of special-case symlinks deserve a little further explanation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) Both result in a new ``struct path`` (with mount and dentry) being set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) up in the ``nameidata``, and result in ``get_link()`` returning ``NULL``.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) The more obvious case is a symlink to "``/``". All symlinks starting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) with "``/``" are detected in ``get_link()`` which resets the ``nameidata``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) to point to the effective filesystem root. If the symlink only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) contains "``/``" then there is nothing more to do, no components at all,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) so ``NULL`` is returned to indicate that the symlink can be released and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) the stack frame discarded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) The other case involves things in ``/proc`` that look like symlinks but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) aren't really (and are therefore commonly referred to as "magic-links")::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) $ ls -l /proc/self/fd/1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) lrwx------ 1 neilb neilb 64 Jun 13 10:19 /proc/self/fd/1 -> /dev/pts/4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) Every open file descriptor in any process is represented in ``/proc`` by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) something that looks like a symlink. It is really a reference to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) target file, not just the name of it. When you ``readlink`` these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) objects you get a name that might refer to the same file - unless it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) has been unlinked or mounted over. When ``walk_component()`` follows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) one of these, the ``->follow_link()`` method in "procfs" doesn't return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) a string name, but instead calls ``nd_jump_link()`` which updates the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) ``nameidata`` in place to point to that target. ``->follow_link()`` then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) returns ``NULL``. Again there is no final component and ``get_link()``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) reports this by leaving the ``last_type`` field of ``nameidata`` as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) ``LAST_BIND``.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) Following the symlink in the final component
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) --------------------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) All this leads to ``link_path_walk()`` walking down every component, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) following all symbolic links it finds, until it reaches the final
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) component. This is just returned in the ``last`` field of ``nameidata``.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) For some callers, this is all they need; they want to create that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) ``last`` name if it doesn't exist or give an error if it does. Other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) callers will want to follow a symlink if one is found, and possibly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) apply special handling to the last component of that symlink, rather
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) than just the last component of the original file name. These callers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) potentially need to call ``link_path_walk()`` again and again on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) successive symlinks until one is found that doesn't point to another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) symlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) This case is handled by the relevant caller of ``link_path_walk()``, such as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) ``path_lookupat()`` using a loop that calls ``link_path_walk()``, and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) handles the final component. If the final component is a symlink
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) that needs to be followed, then ``trailing_symlink()`` is called to set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) things up properly and the loop repeats, calling ``link_path_walk()``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) again. This could loop as many as 40 times if the last component of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) each symlink is another symlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) The various functions that examine the final component and possibly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) report that it is a symlink are ``lookup_last()``, ``mountpoint_last()``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) and ``do_last()``, each of which use the same convention as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) ``walk_component()`` of returning ``1`` if a symlink was found that needs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) to be followed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) Of these, ``do_last()`` is the most interesting as it is used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) opening a file. Part of ``do_last()`` runs with ``i_rwsem`` held and this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) part is in a separate function: ``lookup_open()``.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) Explaining ``do_last()`` completely is beyond the scope of this article,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) but a few highlights should help those interested in exploring the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 1. Rather than just finding the target file, ``do_last()`` needs to open
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) it. If the file was found in the dcache, then ``vfs_open()`` is used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) this. If not, then ``lookup_open()`` will either call ``atomic_open()`` (if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) the filesystem provides it) to combine the final lookup with the open, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) will perform the separate ``lookup_real()`` and ``vfs_create()`` steps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) directly. In the later case the actual "open" of this newly found or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) created file will be performed by ``vfs_open()``, just as if the name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) were found in the dcache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 2. ``vfs_open()`` can fail with ``-EOPENSTALE`` if the cached information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) wasn't quite current enough. Rather than restarting the lookup from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) the top with ``LOOKUP_REVAL`` set, ``lookup_open()`` is called instead,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) giving the filesystem a chance to resolve small inconsistencies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) If that doesn't work, only then is the lookup restarted from the top.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 3. An open with O_CREAT **does** follow a symlink in the final component,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) unlike other creation system calls (like ``mkdir``). So the sequence::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) ln -s bar /tmp/foo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) echo hello > /tmp/foo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) will create a file called ``/tmp/bar``. This is not permitted if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) ``O_EXCL`` is set but otherwise is handled for an O_CREAT open much
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) like for a non-creating open: ``should_follow_link()`` returns ``1``, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) so does ``do_last()`` so that ``trailing_symlink()`` gets called and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) open process continues on the symlink that was found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) Updating the access time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) ------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) We previously said of RCU-walk that it would "take no locks, increment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) no counts, leave no footprints." We have since seen that some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) "footprints" can be needed when handling symlinks as a counted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) reference (or even a memory allocation) may be needed. But these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) footprints are best kept to a minimum.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) One other place where walking down a symlink can involve leaving
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) footprints in a way that doesn't affect directories is in updating access times.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) In Unix (and Linux) every filesystem object has a "last accessed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) time", or "``atime``". Passing through a directory to access a file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) within is not considered to be an access for the purposes of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) ``atime``; only listing the contents of a directory can update its ``atime``.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) Symlinks are different it seems. Both reading a symlink (with ``readlink()``)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) and looking up a symlink on the way to some other destination can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) update the atime on that symlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) .. _clearest statement: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) It is not clear why this is the case; POSIX has little to say on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) subject. The `clearest statement`_ is that, if a particular implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) updates a timestamp in a place not specified by POSIX, this must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) documented "except that any changes caused by pathname resolution need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) not be documented". This seems to imply that POSIX doesn't really
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) care about access-time updates during pathname lookup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) .. _Linux 1.3.87: https://git.kernel.org/cgit/linux/kernel/git/history/history.git/diff/fs/ext2/symlink.c?id=f806c6db77b8eaa6e00dcfb6b567706feae8dbb8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) An examination of history shows that prior to `Linux 1.3.87`_, the ext2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) filesystem, at least, didn't update atime when following a link.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) Unfortunately we have no record of why that behavior was changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) In any case, access time must now be updated and that operation can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) quite complex. Trying to stay in RCU-walk while doing it is best
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) avoided. Fortunately it is often permitted to skip the ``atime``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) update. Because ``atime`` updates cause performance problems in various
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) areas, Linux supports the ``relatime`` mount option, which generally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) limits the updates of ``atime`` to once per day on files that aren't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) being changed (and symlinks never change once created). Even without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) ``relatime``, many filesystems record ``atime`` with a one-second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) granularity, so only one update per second is required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) It is easy to test if an ``atime`` update is needed while in RCU-walk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) mode and, if it isn't, the update can be skipped and RCU-walk mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) continues. Only when an ``atime`` update is actually required does the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) path walk drop down to REF-walk. All of this is handled in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) ``get_link()`` function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) A few flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) -----------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) A suitable way to wrap up this tour of pathname walking is to list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) the various flags that can be stored in the ``nameidata`` to guide the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) lookup process. Many of these are only meaningful on the final
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) component, others reflect the current state of the pathname lookup, and some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) apply restrictions to all path components encountered in the path lookup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) And then there is ``LOOKUP_EMPTY``, which doesn't fit conceptually with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) the others. If this is not set, an empty pathname causes an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) very early on. If it is set, empty pathnames are not considered to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) Global state flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) ~~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) We have already met two global state flags: ``LOOKUP_RCU`` and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) ``LOOKUP_REVAL``. These select between one of three overall approaches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) to lookup: RCU-walk, REF-walk, and REF-walk with forced revalidation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) ``LOOKUP_PARENT`` indicates that the final component hasn't been reached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) yet. This is primarily used to tell the audit subsystem the full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) context of a particular access being audited.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) ``LOOKUP_ROOT`` indicates that the ``root`` field in the ``nameidata`` was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) provided by the caller, so it shouldn't be released when it is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) longer needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) ``LOOKUP_JUMPED`` means that the current dentry was chosen not because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) it had the right name but for some other reason. This happens when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) following "``..``", following a symlink to ``/``, crossing a mount point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) or accessing a "``/proc/$PID/fd/$FD``" symlink (also known as a "magic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) link"). In this case the filesystem has not been asked to revalidate the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) name (with ``d_revalidate()``). In such cases the inode may still need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) to be revalidated, so ``d_op->d_weak_revalidate()`` is called if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) ``LOOKUP_JUMPED`` is set when the look completes - which may be at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) final component or, when creating, unlinking, or renaming, at the penultimate component.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) Resolution-restriction flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) In order to allow userspace to protect itself against certain race conditions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) and attack scenarios involving changing path components, a series of flags are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) available which apply restrictions to all path components encountered during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) path lookup. These flags are exposed through ``openat2()``'s ``resolve`` field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) ``LOOKUP_NO_SYMLINKS`` blocks all symlink traversals (including magic-links).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) This is distinctly different from ``LOOKUP_FOLLOW``, because the latter only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) relates to restricting the following of trailing symlinks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) ``LOOKUP_NO_MAGICLINKS`` blocks all magic-link traversals. Filesystems must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) ensure that they return errors from ``nd_jump_link()``, because that is how
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) ``LOOKUP_NO_MAGICLINKS`` and other magic-link restrictions are implemented.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) ``LOOKUP_NO_XDEV`` blocks all ``vfsmount`` traversals (this includes both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) bind-mounts and ordinary mounts). Note that the ``vfsmount`` which contains the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) lookup is determined by the first mountpoint the path lookup reaches --
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) absolute paths start with the ``vfsmount`` of ``/``, and relative paths start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) with the ``dfd``'s ``vfsmount``. Magic-links are only permitted if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) ``vfsmount`` of the path is unchanged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) ``LOOKUP_BENEATH`` blocks any path components which resolve outside the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) starting point of the resolution. This is done by blocking ``nd_jump_root()``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) as well as blocking ".." if it would jump outside the starting point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) ``rename_lock`` and ``mount_lock`` are used to detect attacks against the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) resolution of "..". Magic-links are also blocked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) ``LOOKUP_IN_ROOT`` resolves all path components as though the starting point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) were the filesystem root. ``nd_jump_root()`` brings the resolution back to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) the starting point, and ".." at the starting point will act as a no-op. As with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) ``LOOKUP_BENEATH``, ``rename_lock`` and ``mount_lock`` are used to detect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) attacks against ".." resolution. Magic-links are also blocked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) Final-component flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) ~~~~~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) Some of these flags are only set when the final component is being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) considered. Others are only checked for when considering that final
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) component.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) ``LOOKUP_AUTOMOUNT`` ensures that, if the final component is an automount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) point, then the mount is triggered. Some operations would trigger it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) anyway, but operations like ``stat()`` deliberately don't. ``statfs()``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) needs to trigger the mount but otherwise behaves a lot like ``stat()``, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) it sets ``LOOKUP_AUTOMOUNT``, as does "``quotactl()``" and the handling of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) "``mount --bind``".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) ``LOOKUP_FOLLOW`` has a similar function to ``LOOKUP_AUTOMOUNT`` but for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) symlinks. Some system calls set or clear it implicitly, while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) others have API flags such as ``AT_SYMLINK_FOLLOW`` and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) ``UMOUNT_NOFOLLOW`` to control it. Its effect is similar to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) ``WALK_GET`` that we already met, but it is used in a different way.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) ``LOOKUP_DIRECTORY`` insists that the final component is a directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) Various callers set this and it is also set when the final component
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) is found to be followed by a slash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) Finally ``LOOKUP_OPEN``, ``LOOKUP_CREATE``, ``LOOKUP_EXCL``, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) ``LOOKUP_RENAME_TARGET`` are not used directly by the VFS but are made
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) available to the filesystem and particularly the ``->d_revalidate()``
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) method. A filesystem can choose not to bother revalidating too hard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) if it knows that it will be asked to open or create the file soon.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) These flags were previously useful for ``->lookup()`` too but with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) introduction of ``->atomic_open()`` they are less relevant there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) End of the road
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) ---------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) Despite its complexity, all this pathname lookup code appears to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) in good shape - various parts are certainly easier to understand now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) than even a couple of releases ago. But that doesn't mean it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) "finished". As already mentioned, RCU-walk currently only follows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) symlinks that are stored in the inode so, while it handles many ext4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) symlinks, it doesn't help with NFS, XFS, or Btrfs. That support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) is not likely to be long delayed.