1.HTML "Lexical File Names in Plan 9 or Getting Dot-Dot Right 2.hw re-create 3.hw re-created 4.TL 5Lexical File Names in Plan 9 6.br 7or 8.br 9Getting Dot-Dot Right 10.AU 11Rob Pike 12.CW rob@plan9.bell-labs.com 13.AI 14.MH 15.AB 16.LP 17Symbolic links make the Unix file system non-hierarchical, resulting in 18multiple valid path names for a given file. 19This ambiguity is a source of confusion, especially since some shells 20work overtime to present a consistent view from programs such as 21.CW pwd , 22while other programs and 23the kernel itself do nothing about the problem. 24.LP 25Plan 9 has no symbolic links but it does have other mechanisms that produce the same difficulty. 26Moreover, Plan 9 is founded on the ability to control a program's environment 27by manipulating its name space. 28Ambiguous names muddle the result of operations such as copying a name space across 29the network. 30.LP 31To address these problems, 32the Plan 9 kernel has been modified to maintain an accurate path name for every active 33file (open file, working directory, mount table entry) in the system. 34The definition of `accurate' is that the path name for a file is guaranteed to be the rooted, 35absolute name 36the program used to acquire it. 37These names are maintained by an efficient method that combines lexical processing\(emsuch as 38evaluating 39.CW .. 40by just removing the last path name element of a directory\(emwith 41local operations within the file system to maintain a consistently, easily understood view 42of the name system. 43Ambiguous situations are resolved by examining the lexically maintained names themselves. 44.LP 45A new kernel call, 46.CW fd2path , 47returns the file name associated with an open file, 48permitting the use of reliable names to improve system 49services ranging from 50.CW pwd 51to debugging. 52Although this work was done in Plan 9, 53Unix systems could also benefit from the addition of 54a method to recover the accurate name of an 55open file or the current directory. 56.AE 57.SH 58Motivation 59.LP 60Consider the following unedited transcript of a session running the Bourne shell on a modern 61Unix system: 62.P1 63% echo $HOME 64/home/rob 65% cd $HOME 66% pwd 67/n/bopp/v7/rob 68% cd /home/rob 69% cd /home/ken 70% cd ../rob 71\&../rob: bad directory 72% 73.P2 74(The same output results from running 75.CW tcsh ; 76we'll discuss 77.CW ksh 78in a moment.) 79To a neophyte being schooled in the delights of a hierarchical file name space, 80this behavior must be baffling. 81It is, of course, the consequence of a series of symbolic links intended to give users 82the illusion they share a disk, when in fact their files are scattered over several devices: 83.P1 84.ps -1 85% ls -ld /home/rob /home/ken 86lrwxr-xr-x 1 root sys 14 Dec 26 1998 /home/ken -> /n/bopp/v6/ken 87lrwxr-xr-x 1 root sys 14 Dec 23 1998 /home/rob -> /n/bopp/v7/rob 88% 89.ps 90.P2 91The introduction of symbolic links has changed the Unix file system from a true 92hierarchy into a directed graph, rendering 93.CW .. 94ambiguous and sowing confusion. 95.LP 96Unix popularized hierarchical naming, but the introduction of symbolic links 97made its naming irregular. 98Worse, the 99.CW pwd 100command, through the underlying 101.CW getwd 102library routine, 103uses a tricky, expensive algorithm that often delivers the wrong answer. 104Starting from the current directory, 105.CW getwd 106opens the parent, 107.CW .. , 108and searches it for an entry whose i-number matches the current directory; 109the matching entry is the final path element of the ultimate result. 110Applying this process iteratively, 111.CW getwd 112works back towards the root. 113Since 114.CW getwd 115knows nothing about symbolic links, it will recover surprising names for 116directories reached by them, 117as illustrated by the example; 118the backward paths 119.CW getwd 120traverses will not backtrack across the links. 121.LP 122Partly for efficiency and partly to make 123.CW cd 124and 125.CW pwd 126more predictable, the Korn shell 127.CW ksh 128[Korn94] 129implements 130.CW pwd 131as a builtin. 132(The 133.CW cd 134command must be a builtin in any shell, since the current directory is unique to each process.) 135.CW Ksh 136maintains its own private view of the file system to try to disguise symbolic links; 137in particular, 138.CW cd 139and 140.CW pwd 141involve some lexical processing (somewhat like the 142.CW cleanname 143function discussed later 144in this paper), augmented by heuristics such as examining the environment 145for names like 146.CW $HOME 147and 148.CW $PWD 149to assist initialization of the state of the private view. [Korn00] 150.LP 151This transcript begins with a Bourne shell running: 152.P1 153% cd /home/rob 154% pwd 155/n/bopp/v7/rob 156% ksh 157$ pwd 158/home/rob 159$ 160.P2 161This result is encouraging. Another example, again starting from a Bourne shell: 162.P1 163% cd /home/rob 164% cd ../ken 165\&../ken: bad directory 166% ksh 167$ pwd 168/home/rob 169$ cd ../ken 170$ pwd 171/home/ken 172$ 173.P2 174By doing extra work, 175the Korn shell is providing more sensible behavior, 176but it is easy to defeat: 177.P1 178% cd /home/rob 179% pwd 180/n/bopp/v7/rob 181% cd bin 182% pwd 183/n/bopp/v7/rob/bin 184% ksh 185$ pwd 186/n/bopp/v7/rob/bin 187$ exit 188% cd /home/ken 189% pwd 190/n/bopp/v6/ken 191% ksh 192$ pwd 193/n/bopp/v6/ken 194$ 195.P2 196In these examples, 197.CW ksh 's 198built-in 199.CW pwd 200failed to produce the results 201.CW /home/rob/bin "" ( 202and 203.CW /home/ken ) 204that the previous example might have led us to expect. 205The Korn shell is hiding the problem, not solving it, and in fact is not even hiding it very well. 206.LP 207A deeper question is whether the shell should even be trying to make 208.CW pwd 209and 210.CW cd 211do a better job. 212If it does, then the 213.CW getwd 214library call and every program that uses it will behave differently from the shell, 215a situation that is sure to confuse. 216Moreover, the ability to change directory to 217.CW ../ken 218with the Korn shell's 219.CW cd 220command but not with the 221.CW chdir 222system call is a symptom of a diseased system, not a healthy shell. 223.LP 224The operating system should provide names that work and make sense. 225Symbolic links, though, are here to stay, so we need a way to provide 226sensible, unambiguous names in the face of a non-hierarchical name space. 227This paper shows how the challenge was met on Plan 9, an operating system 228with Unix-like naming. 229.SH 230Names in Plan 9 231.LP 232Except for some details involved with bootstrapping, file names in Plan 9 have the same syntax as in Unix. 233Plan 9 has no symbolic links, but its name space construction operators, 234.CW bind 235and 236.CW mount , 237make it possible to build the same sort of non-hierarchical structures created 238by symbolically linking directories on Unix. 239.LP 240Plan 9's 241.CW mount 242system call takes a file descriptor 243and attaches to the local name space the file system service it represents: 244.P1 245mount(fd, "/dir", flags) 246.P2 247Here 248.CW fd 249is a file descriptor to a communications port such as a pipe or network connection; 250at the other end of the port is a service, such as file server, that talks 9P, the Plan 9 file 251system protocol. 252After the call succeeds, the root directory of the service will be visible at the 253.I "mount point 254.CW /dir , 255much as with the 256.CW mount 257call of Unix. 258The 259.CW flag 260argument specifies the nature of the attachment: 261.CW MREPL 262says that the contents of the root directory (appear to) replace the current contents of 263.CW /dir ; 264.CW MAFTER 265says that the current contents of 266.CW dir 267remain visible, with the mounted directory's contents appearing 268.I after 269any existing files; 270and 271.CW MBEFORE 272says that the contents remain visible, with 273the mounted directory's contents appearing 274.I before 275any existing files. 276These multicomponent directories are called 277.I "union directories 278and are somewhat different from union directories in 4.4BSD-Lite [PeMc95], because 279only the top-level directory itself is unioned, not its descendents, recursively. 280(Plan 9's union directories are used differently from 4.4BSD-Lite's, as will become apparent.) 281.LP 282For example, to bootstrap a diskless computer the system builds a local name space containing 283only the root directory, 284.CW / , 285then uses the network to open a connection 286to the main file server. 287It then executes 288.P1 289mount(rootfd, "/", MREPL); 290.P2 291After this call, the entire file server's tree is visible, starting from the root of the local machine. 292.LP 293While 294.CW mount 295connects a new service to the local name space, 296.CW bind 297rearranges the existing name space: 298.P1 299bind("tofile", "fromfile", flags) 300.P2 301causes subsequent mention of the 302.CW fromfile 303(which may be a plain file or a directory) 304to behave as though 305.CW tofile 306had been mentioned instead, somewhat like a symbolic link. 307(Note, however, that the arguments are in the opposite order 308compared to 309.CW ln 310.CW -s ). 311The 312.CW flags 313argument is the same as with 314.CW mount . 315.LP 316As an example, a sequence something like the following is done at bootstrap time to 317assemble, under the single directory 318.CW /bin , 319all of the binaries suitable for this architecture, represented by (say) the string 320.CW sparc : 321.P1 322bind("/sparc/bin", "/bin", MREPL); 323bind("/usr/rob/sparc/bin", "/bin", MAFTER); 324.P2 325This sequence of 326.CW binds 327causes 328.CW /bin 329to contain first the standard binaries, then the contents of 330.CW rob 's 331private SPARC binaries. 332The ability to build such union directories 333obviates the need for a shell 334.CW $PATH 335variable 336while providing opportunities for managing heterogeneity. 337If the system were a Power PC, the same sequence would be run with 338.CW power 339textually substituted for 340.CW sparc 341to place the Power PC binaries in 342.CW /bin 343rather than the SPARC binaries. 344.LP 345Trouble is already brewing. After these bindings are set up, 346where does 347.P1 348% cd /bin 349% cd .. 350.P2 351set the current working directory, to 352.CW / 353or 354.CW /sparc 355or 356.CW /usr/rob/sparc ? 357We will return to this issue. 358.LP 359There are some important differences between 360.CW binds 361and symbolic links. 362First, 363symbolic links are a static part of the file system, while 364Plan 9 bindings are created at run time, are stored in the kernel, 365and endure only as long as the system maintains them; 366they are temporary. 367Since they are known to the kernel but not the file system, they must 368be set up each time the kernel boots or a user logs in; 369permanent bindings are created by editing system initialization scripts 370and user profiles rather than by building them in the file system itself. 371.LP 372The Plan 9 kernel records what bindings are active for a process, 373whereas symbolic links, being held on the Unix file server, may strike whenever the process evaluates 374a file name. 375Also, symbolic links apply to all processes that evaluate the affected file, whereas 376.CW bind 377has a local scope, applying only to the process that executes it and possibly some of its 378peers, as discussed in the next section. 379Symbolic links cannot construct the sort of 380.CW /bin 381directory built here; it is possible to have multiple directories point to 382.CW /bin 383but not the other way around. 384.LP 385Finally, 386symbolic links are symbolic, like macros: they evaluate the associated names each time 387they are accessed. 388Bindings, on the other hand, are evaluated only once, when the bind is executed; 389after the binding is set up, the kernel associates the underlying files, rather than their names. 390In fact, the kernel's representation of a bind is identical to its representation of a mount; 391in effect, a bind is a mount of the 392.CW tofile 393upon the 394.CW fromfile . 395The binds and mounts coexist in a single 396.I "mount table" , 397the subject of the next section. 398.SH 399The Mount Table 400.LP 401Unix has a single global mount table 402for all processes in the system, but Plan 9's mount tables are local to each process. 403By default it is inherited when a process forks, so mounts and binds made by one 404process affect the other, but a process may instead inherit a copy, 405so modifications it makes will be invisible to other processes. 406The convention is that related processes, such 407as processes running in a single window, share a mount table, while sets of processes 408in different windows have distinct mount tables. 409In practice, the name spaces of the two windows will appear largely the same, 410but the possibility for different processes to see different files (hence services) under 411the same name is fundamental to the system, 412affecting the design of key programs such as the 413window system [Pike91]. 414.LP 415The Plan 9 mount table is little more than an ordered list of pairs, mapping the 416.CW fromfiles 417to the 418.CW tofiles . 419For mounts, the 420.CW tofile 421will be an item called a 422.CW Channel , 423similar to a Unix 424.CW vnode , 425pointing to the root of the file service, 426while for a bind it will be the 427.CW Channel 428pointing to the 429.CW tofile 430mentioned in the 431.CW bind 432call. 433In both cases, the 434.CW fromfile 435entry in the table 436will be a 437.CW Channel 438pointing to the 439.CW fromfile 440itself. 441.LP 442The evaluation of a file name proceeds as follows. 443If the name begins with a slash, start with the 444.CW Channel 445for the root; otherwise start with the 446.CW Channel 447for the current directory of the process. 448For each path element in the name, 449such as 450.CW usr 451in 452.CW /usr/rob , 453try to `walk' the 454.CW Channel 455to that element [Pike93]. 456If the walk succeeds, look to see if the resulting 457.CW Channel 458is the same as any 459.CW fromfile 460in the mount table, and if so, replace it by the corresponding 461.CW tofile . 462Advance to the next element and continue. 463.LP 464There are a couple of nuances. If the directory being walked is a union directory, 465the walk is attempted in the elements of the union, in order, until a walk succeeds. 466If none succeed, the operation fails. 467Also, when the destination of a walk is a directory for a purpose such as the 468.CW chdir 469system call or the 470.CW fromfile 471in a 472.CW bind , 473once the final walk of the sequence has completed the operation stops; 474the final check through the mount table is not done. 475Among other things, this simplifies the management of union directories; 476for example, subsequent 477.CW bind 478calls will append to the union associated with the underlying 479.CW fromfile 480instead of what is bound upon it. 481.SH 482A Definition of Dot-Dot 483.LP 484The ability to construct union directories and other intricate naming structures 485introduces some thorny problems: as with symbolic links, 486the name space is no longer hierarchical, files and directories can have multiple 487names, and the meaning of 488.CW .. , 489the parent directory, can be ambiguous. 490.LP 491The meaning of 492.CW .. 493is straightforward if the directory is in a locally hierarchical part of the name space, 494but if we ask what 495.CW .. 496should identify when the current directory is a mount point or union directory or 497multiply symlinked spot (which we will henceforth call just a mount point, for brevity), 498there is no obvious answer. 499Name spaces have been part of Plan 9 from the beginning, but the definition of 500.CW .. 501has changed several times as we grappled with this issue. 502In fact, several attempts to clarify the meaning of 503.CW .. 504by clever coding 505resulted in definitions that could charitably be summarized as `what the implementation gives.' 506.LP 507Frustrated by this situation, and eager to have better-defined names for some of the 508applications described later in this paper, we recently proposed the following definition 509for 510.CW .. : 511.IP 512The parent of a directory 513.I X , 514.I X\f(CW/..\f1, 515is the same directory that would obtain if 516we instead accessed the directory named by stripping away the last 517path name element of 518.I X . 519.LP 520For example, if we are in the directory 521.CW /a/b/c 522and 523.CW chdir 524to 525.CW .. , 526the result is 527.I exactly 528as if we had executed a 529.CW chdir 530to 531.CW /a/b . 532.LP 533This definition is easy to understand and seems natural. 534It is, however, a purely 535.I lexical 536definition that flatly ignores evaluated file names, mount tables, and 537other kernel-resident data structures. 538Our challenge is to implement it efficiently. 539One obvious (and correct) 540implementation is to rewrite path names lexically to fold out 541.CW .. , 542and then evaluate the file name forward from the root, 543but this is expensive and unappealing. 544We want to be able to use local operations to evaluate file names, 545but maintain the global, lexical definition of dot-dot. 546It isn't too hard. 547.SH 548The Implementation 549.LP 550To operate lexically on file names, we associate a name with each open file in the kernel, that 551is, with each 552.CW Channel 553data structure. 554The first step is therefore to store a 555.CW char* 556with each 557.CW Channel 558in the system, called its 559.CW Cname , 560that records the 561.I absolute 562rooted 563file name for the 564.CW Channel . 565.CW Cnames 566are stored as full text strings, shared copy-on-write for efficiency. 567The task is to maintain each 568.CW Cname 569as an accurate absolute name using only local operations. 570.LP 571When a file is opened, the file name argument in the 572.CW open 573(or 574.CW chdir 575or 576.CW bind 577or ...) call is recorded in the 578.CW Cname 579of the resulting 580.CW Channel . 581When the file name begins with a slash, the name is stored as is, 582subject to a cleanup pass described in the next section. 583Otherwise, it is a local name, and the file name must be made 584absolute by prefixing it with the 585.CW Cname 586of the current directory, followed by a slash. 587For example, if we are in 588.CW /home/rob 589and 590.CW chdir 591to 592.CW bin , 593the 594.CW Cname 595of the resulting 596.CW Channel 597will be the string 598.CW /home/rob/bin . 599.LP 600This assumes, of course, that the local file name contains no 601.CW .. 602elements. 603If it does, instead of storing for example 604.CW /home/rob/.. 605we delete the last element of the existing name and set the 606.CW Cname 607to 608.CW /home . 609To maintain the lexical naming property we must guarantee that the resulting 610.CW Cname , 611if it were to be evaluated, would yield the identical directory to the one 612we actually do get by the local 613.CW .. 614operation. 615.LP 616If the current directory is not a mount point, it is easy to maintain the lexical property. 617If it is a mount point, though, it is still possible to maintain it on Plan 9 618because the mount table, a kernel-resident data structure, contains all the 619information about the non-hierarchical connectivity of the name space. 620(On Unix, by contrast, symbolic links are stored on the file server rather than in the kernel.) 621Moreover, the presence of a full file name for each 622.CW Channel 623in the mount table provides the information necessary to resolve ambiguities. 624.LP 625The mount table is examined in the 626.CW from\f1\(->\fPto 627direction when evaluating a name, but 628.CW .. 629points backwards in the hierarchy, so to evaluate 630.CW .. 631the table must be examined in the 632.CW to\f1\(->\fPfrom 633direction. 634(``How did we get here?'') 635.LP 636The value of 637.CW .. 638is ambiguous when there are multiple bindings (mount points) that point to 639the directories involved in the evaluation of 640.CW .. . 641For example, return to our original script with 642.CW /n/bopp/v6 643(containing a home directory for 644.CW ken ) 645and 646.CW /n/bopp/v7 647(containing a home directory for 648.CW rob ) 649unioned into 650.CW /home . 651This is represented by two entries in the mount table, 652.CW from=/home , 653.CW to=/n/bopp/v6 654and 655.CW from=/home , 656.CW to=/n/bopp/v7 . 657If we have set our current directory to 658.CW /home/rob 659(which has landed us in the physical location 660.CW /n/bopp/v7/rob ) 661our current directory is not a mount point but its parent is. 662The value of 663.CW .. 664is ambiguous: it could be 665.CW /home , 666.CW /n/bopp/v7 , 667or maybe even 668.CW /n/bopp/v6 , 669and the ambiguity is caused by two 670.CW tofiles 671bound to the same 672.CW fromfile . 673By our definition, if we now evaluate 674.CW .. , 675we should acquire the directory 676.CW /home ; 677otherwise 678.CW ../ken 679could not possibly result in 680.CW ken 's 681home directory, which it should. 682On the other hand, if we had originally gone to 683.CW /n/bopp/v7/rob , 684the name 685.CW ../ken 686should 687.I not 688evaluate to 689.CW ken 's 690home directory because there is no directory 691.CW /n/bopp/v7/ken 692.CW ken 's ( 693home directory is on 694.CW v6 ). 695The problem is that by using local file operations, it is impossible 696to distinguish these cases: regardless of whether we got here using the name 697.CW /home/rob 698or 699.CW /n/bopp/v7/rob , 700the resulting directory is the same. 701Moreover, the mount table does not itself have enough information 702to disambiguate: when we do a local operation to evaluate 703.CW .. 704and land in 705.CW /n/bopp/v7 , 706we discover that the directory is a 707.CW tofile 708in the mount table; should we step back through the table to 709.CW /home 710or not? 711.LP 712The solution comes from the 713.CW Cnames 714themselves. 715Whether to step back through the mount point 716.CW from=/home , 717.CW to=/n/bopp/v7 718when evaluating 719.CW .. 720in 721.CW rob 's 722directory is trivially resolved by asking the question, 723Does the 724.CW Cname 725for the directory begin 726.CW /home ? 727If it does, then the path that was evaluated to get us to the current 728directory must have gone through this mount point, and we should 729back up through it to evaluate 730.CW .. ; 731if not, then this mount table entry is irrelevant. 732.LP 733More precisely, 734both 735.I before 736and 737.I after 738each 739.CW .. 740element in the path name is evaluated, 741if the directory is a 742.CW tofile 743in the mount table, the corresponding 744.CW fromfile 745is taken instead, provided the 746.CW Cname 747of the corresponding 748.CW fromfile 749is the prefix of the 750.CW Cname 751of the original directory. 752Since we always know the full name of the directory 753we are evaluating, we can always compare it against all the entries in the mount table that point 754to it, thereby resolving ambiguous situations 755and maintaining the 756lexical property of 757.CW .. . 758This check also guarantees we don't follow a misleading mount point, such as the entry pointing to 759.CW /home 760when we are really in 761.CW /n/bopp/v7/rob . 762Keeping the full names with the 763.CW Channels 764makes it easy to use the mount table to decide how we got here and, therefore, 765how to get back. 766.LP 767In summary, the algorithm is as follows. 768Use the usual file system operations to walk to 769.CW .. ; 770call the resulting directory 771.I d . 772Lexically remove 773the last element of the initial file name. 774Examine all entries in the mount table whose 775.CW tofile 776is 777.I d 778and whose 779.CW fromfile 780has a 781.CW Cname 782identical to the truncated name. 783If one exists, that 784.CW fromfile 785is the correct result; by construction, it also has the right 786.CW Cname . 787In our example, evaluating 788.CW .. 789in 790.CW /home/rob 791(really 792.CW /n/bopp/v7/rob ) 793will set 794.I d 795to 796.CW /n/bopp/v7 ; 797that is a 798.CW tofile 799whose 800.CW fromfile 801is 802.CW /home . 803Removing the 804.CW /rob 805from the original 806.CW Cname , 807we find the name 808.CW /home , 809which matches that of the 810.CW fromfile , 811so the result is the 812.CW fromfile , 813.CW /home . 814.LP 815Since this implementation uses only local operations to maintain its names, 816it is possible to confuse it by external changes to the file system. 817Deleting or renaming directories and files that are part of a 818.CW Cname , 819or modifying the mount table, can introduce errors. 820With more implementation work, such mistakes could probably be caught, 821but in a networked environment, with machines sharing a remote file server, renamings 822and deletions made by one machine may go unnoticed by others. 823These problems, however, are minor, uncommon and, most important, easy to understand. 824The method maintains the lexical property of file names unless an external 825agent changes the name surreptitiously; 826within a stable file system, it is always maintained and 827.CW pwd 828is always right. 829.LP 830To recapitulate, maintaining the 831.CW Channel 's 832absolute file names lexically and using the names to disambiguate the 833mount table entries when evaluating 834.CW .. 835at a mount point 836combine to maintain the lexical definition of 837.CW .. 838efficiently. 839.SH 840Cleaning names 841.LP 842The lexical processing can generate names that are messy or redundant, 843ones with extra slashes or embedded 844.CW ../ 845or 846.CW ./ 847elements and other extraneous artifacts. 848As part of the kernel's implementation, we wrote a procedure, 849.CW cleanname , 850that rewrites a name in place to canonicalize its appearance. 851The procedure is useful enough that it is now part of the Plan 9 C 852library and is employed by many programs to make sure they always 853present clean file names. 854.LP 855.CW Cleanname 856is analogous to the URL-cleaning rules defined in RFC 1808 [Field95], although 857the rules are slightly different. 858.CW Cleanname 859iteratively does the following until no further processing can be done: 860.IP 8611. Reduce multiple slashes to a single slash. 862.IP 8632. Eliminate 864.CW . 865path name elements 866(the current directory). 867.IP 8683. Eliminate 869.CW .. 870path name elements (the parent directory) and the 871.CW . "" non- 872.CW .., "" non- 873element that precedes them. 874.IP 8754. Eliminate 876.CW .. 877elements that begin a rooted path, that is, replace 878.CW /.. 879by 880.CW / 881at the beginning of a path. 882.IP 8835. Leave intact 884.CW .. 885elements that begin a non-rooted path. 886.LP 887If the result of this process is a null string, 888.CW cleanname 889returns the string 890.CW \&"." , 891representing the current directory. 892.SH 893The fd2path system call 894.LP 895Plan 9 has a new system call, 896.CW fd2path , 897to enable programs to extract the 898.CW Cname 899associated with an open file descriptor. 900It takes three arguments: a file descriptor, a buffer, and the size of the buffer: 901.P1 902int fd2path(int fd, char *buf, int nbuf) 903.P2 904It returns an error if the file descriptor is invalid; otherwise it fills the buffer with the name 905associated with 906.CW fd . 907(If the name is too long, it is truncated; perhaps this condition should also draw an error.) 908The 909.CW fd2path 910system call is very cheap, since all it does is copy the 911.CW Cname 912string to user space. 913.LP 914The Plan 9 implementation of 915.CW getwd 916uses 917.CW fd2path 918rather than the tricky algorithm necessary in Unix: 919.P1 920char* 921getwd(char *buf, int nbuf) 922{ 923 int n, fd; 924 925 fd = open(".", OREAD); 926 if(fd < 0) 927 return NULL; 928 n = fd2path(fd, buf, nbuf); 929 close(fd); 930 if(n < 0) 931 return NULL; 932 return buf; 933} 934.P2 935(The Unix specification of 936.CW getwd 937does not include a count argument.) 938This version of 939.CW getwd 940is not only straightforward, it is very efficient, reducing the performance 941advantage of a built-in 942.CW pwd 943command while guaranteeing that all commands, not just 944.CW pwd , 945see sensible directory names. 946.LP 947Here is a routine that prints the file name associated 948with each of its open file descriptors; it is useful for tracking down file descriptors 949left open by network listeners, text editors that spawn commands, and the like: 950.P1 951void 952openfiles(void) 953{ 954 int i; 955 char buf[256]; 956 957 for(i=0; i<NFD; i++) 958 if(fd2path(i, buf, sizeof buf) >= 0) 959 print("%d: %s\en", i, buf); 960} 961.P2 962.SH 963Uses of good names 964.LP 965Although 966.CW pwd 967was the motivation for getting names right, good file names are useful in many contexts 968and have become a key part of the Plan 9 programming environment. 969The compilers record in the symbol table the full name of the source file, which makes 970it easy to track down the source of buggy, old software and also permits the 971implementation of a program, 972.CW src , 973to automate tracking it down. 974Given the name of a program, 975.CW src 976reads its symbol table, extracts the file information, 977and triggers the editor to open a window on the program's 978source for its 979.CW main 980routine. 981No guesswork, no heuristics. 982.LP 983The 984.CW openfiles 985routine was the inspiration for a new file in the 986.CW /proc 987file system [Kill84]. 988For process 989.I n , 990the file 991.CW /proc/\f2n\fP/fd 992is a list of all its open files, including its working directory, 993with associated information including its open status, 994I/O offset, unique id (analogous to i-number) 995and file name. 996Here is the contents of the 997.CW fd 998file for a process in the window system on the machine being used to write this paper: 999.P1 1000% cat /proc/125099/fd 1001/usr/rob 1002 0 r M 5141 00000001.00000000 0 /mnt/term/dev/cons 1003 1 w M 5141 00000001.00000000 51 /mnt/term/dev/cons 1004 2 w M 5141 00000001.00000000 51 /mnt/term/dev/cons 1005 3 r M 5141 0000000b.00000000 1166 /dev/snarf 1006 4 rw M 5141 0ffffffc.00000000 288 /dev/draw/new 1007 5 rw M 5141 00000036.00000000 4266337 /dev/draw/3/data 1008 6 r M 5141 00000037.00000000 0 /dev/draw/3/refresh 1009 7 r c 0 00000004.00000000 6199848 /dev/bintime 1010% 1011.P2 1012(The Linux implementation of 1013.CW /proc 1014provides a related service by giving a directory in which each file-descriptor-numbered file is 1015a symbolic link to the file itself.) 1016When debugging errant systems software, such information can be valuable. 1017.LP 1018Another motivation for getting names right was the need to extract from the system 1019an accurate description of the mount table, so that a process's name space could be 1020recreated on another machine, in order to move (or simulate) a computing environment 1021across the network. 1022One program that does this is Plan 9's 1023.CW cpu 1024command, which recreates the local name space on a remote machine, typically a large 1025fast multiprocessor. 1026Without accurate names, it was impossible to do the job right; now 1027.CW /proc 1028provides a description of the name space of each process, 1029.CW /proc/\f2n\fP/ns : 1030.P1 1031% cat /proc/125099/ns 1032bind / / 1033mount -aC #s/boot / 1034bind #c /dev 1035bind #d /fd 1036bind -c #e /env 1037bind #p /proc 1038bind -c #s /srv 1039bind /386/bin /bin 1040bind -a /rc/bin /bin 1041bind /net /net 1042bind -a #l /net 1043mount -a #s/cs /net 1044mount -a #s/dns /net 1045bind -a #D /net 1046mount -c #s/boot /n/emelie 1047bind -c /n/emelie/mail /mail 1048mount -c /net/il/134/data /mnt/term 1049bind -a /usr/rob/bin/rc /bin 1050bind -a /usr/rob/bin/386 /bin 1051mount #s/boot /n/emelieother other 1052bind -c /n/emelieother/rob /tmp 1053mount #s/boot /n/dump dump 1054bind /mnt/term/dev/cons /dev/cons 1055\&... 1056cd /usr/rob 1057% 1058.P2 1059(The 1060.CW # 1061notation identifies raw device drivers so they may be attached to the name space.) 1062The last line of the file gives the working directory of the process. 1063The format of this file is that used by a library routine, 1064.CW newns , 1065which reads a textual description like this and reconstructs a name space. 1066Except for the need to quote 1067.CW # 1068characters, the output is also a shell script that invokes the user-level commands 1069.CW bind 1070and 1071.CW mount , 1072which are just interfaces to the underlying system calls. 1073However, 1074files like 1075.CW /net/il/134/data 1076represent network connections; to find out where they point, so that the corresponding 1077calls can be reestablished for another process, 1078they must be examined in more detail using the network device files [PrWi93]. Another program, 1079.CW ns , 1080does this; it reads the 1081.CW /proc/\f2n\fP/ns 1082file, decodes the information, and interprets it, translating the network 1083addresses and quoting the names when required: 1084.P1 1085\&... 1086mount -a '#s/dns' /net 1087\&... 1088mount -c il!135.104.3.100!12884 /mnt/term 1089\&... 1090.P2 1091These tools make it possible to capture an accurate description of a process's 1092name space and recreate it elsewhere. 1093And like the open file descriptor table, 1094they are a boon to debugging; it is always helpful to know 1095exactly what resources a program is using. 1096.SH 1097Adapting to Unix 1098.LP 1099This work was done for the Plan 9 operating system, which has the advantage that 1100the non-hierarchical aspects of the name space are all known to the kernel. 1101It should be possible, though, to adapt it to a Unix system. 1102The problem is that Unix has nothing corresponding precisely to a 1103.CW Channel , 1104which in Plan 9 represents the unique result of evaluating a name. 1105The 1106.CW vnode 1107structure is a shared structure that may represent a file 1108known by several names, while the 1109.CW file 1110structure refers only to open files, but for example the current working 1111directory of a process is not open. 1112Possibilities to address this discrepancy include 1113introducing a 1114.CW Channel -like 1115structure that connects a name and a 1116.CW vnode , 1117or maintaining a separate per-process table that maps names to 1118.CW vnodes , 1119disambiguating using the techniques described here. 1120If it could be done 1121the result would be an implementation of 1122.CW .. 1123that reduces the need for a built-in 1124.CW pwd 1125in the shell and offers a consistent, sensible interpretation of the `parent directory'. 1126.LP 1127We have not done this adaptation, but we recommend that the Unix community try it. 1128.SH 1129Conclusions 1130.LP 1131It should be easy to discover a well-defined, absolute path name for every open file and 1132directory in the system, even in the face of symbolic links and other non-hierarchical 1133elements of the file name space. 1134In earlier versions of Plan 9, and all current versions of Unix, 1135names can instead be inconsistent and confusing. 1136.LP 1137The Plan 9 operating system now maintains an accurate name for each file, 1138using inexpensive lexical operations coupled with local file system actions. 1139Ambiguities are resolved by examining the names themselves; 1140since they reflect the path that was used to reach the file, they also reflect the path back, 1141permitting a dependable answer to be recovered even when stepping backwards through 1142a multiply-named directory. 1143.LP 1144Names make sense again: they are sensible and consistent. 1145Now that dependable names are available, system services can depend on them, 1146and recent work in Plan 9 is doing just that. 1147We\(emthe community of Unix and Unix-like systems\(emshould have done this work a long time ago. 1148.SH 1149Acknowledgements 1150.LP 1151Phil Winterbottom devised the 1152.CW ns 1153command and the 1154.CW fd 1155and 1156.CW ns 1157files in 1158.CW /proc , 1159based on an earlier implementation of path name management that 1160the work in this paper replaces. 1161Russ Cox wrote the final version of 1162.CW cleanname 1163and helped debug the code for reversing the mount table. 1164Ken Thompson, Dave Presotto, and Jim McKie offered encouragement and consultation. 1165.SH 1166References 1167.LP 1168[Field95] 1169R. Fielding, 1170``Relative Uniform Resource Locators'', 1171.I "Network Working Group Request for Comments: 1808" , 1172June, 1995. 1173.LP 1174[Kill84] 1175T. J. Killian, 1176``Processes as Files'', 1177.I "Proceedings of the Summer 1984 USENIX Conference" , 1178Salt Lake City, 1984, pp. 203-207. 1179.LP 1180[Korn94] 1181David G. Korn, 1182``ksh: An Extensible High Level Language'', 1183.I "Proceedings of the USENIX Very High Level Languages Symposium" , 1184Santa Fe, 1994, pp. 129-146. 1185.LP 1186[Korn00] 1187David G. Korn, 1188personal communication. 1189.LP 1190[PeMc95] 1191Jan-Simon Pendry and Marshall Kirk McKusick, 1192``Union Mounts in 4.4BSD-Lite'', 1193.I "Proceedings of the 1995 USENIX Conference" , 1194New Orleans, 1995. 1195.LP 1196[Pike91] 1197Rob Pike, 1198``8½, the Plan 9 Window System'', 1199.I "Proceedings of the Summer 1991 USENIX Conference" , 1200Nashville, 1991, pp. 257-265. 1201.LP 1202[Pike93] 1203Rob Pike, Dave Presotto, Ken Thompson, Howard Trickey, and Phil Winterbottom, 1204``The Use of Name Spaces in Plan 9'', 1205.I "Operating Systems Review" , 1206.B 27 , 12072, April 1993, pp. 72-76. 1208.LP 1209[PrWi93] 1210Dave Presotto and Phil Winterbottom, 1211``The Organization of Networks in Plan 9'', 1212.I "Proceedings of the Winter 1993 USENIX Conference" , 1213San Diego, 1993, pp. 43-50. 1214