1
0

lexnames.ms 33 KB

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