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