~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/Documentation/filesystems/path-lookup.rst

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /Documentation/filesystems/path-lookup.rst (Version linux-6.11.5) and /Documentation/filesystems/path-lookup.rst (Version linux-4.18.20)


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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php