blob: 397486f8301af713654d294e531df2063e571810 [file] [log] [blame]
rsc058b0112005-01-03 06:40:20 +00001.TH INTRO 9P
2.SH NAME
3intro \- introduction to the Plan 9 File Protocol, 9P
4.SH SYNOPSIS
5.B #include <fcall.h>
6.SH DESCRIPTION
7A Plan 9
8.I server
9is an agent that provides one or more hierarchical file systems
10\(em file trees \(em
11that may be accessed by Plan 9 processes.
12A server responds to requests by
13.I clients
14to navigate the hierarchy,
15and to create, remove, read, and write files.
16The prototypical server is a separate machine that stores
17large numbers of user files on permanent media;
18such a machine is called, somewhat confusingly, a
19.I file
20.IR server .
21Another possibility for a server is to synthesize
22files on demand, perhaps based on information on data structures
23maintained in memory; the
24.IR plumber (4)
25server is an example of such a server.
26.PP
27A
28.I connection
29to a server is a bidirectional communication path from the client to the server.
30There may be a single client or
31multiple clients sharing the same connection.
32.PP
33The
34.IR "Plan 9 File Protocol" ,
359P, is used for messages between
36.I clients
37and
38.IR servers .
39A client transmits
40.I requests
41.RI ( T-messages )
42to a server, which
43subsequently returns
44.I replies
45.RI ( R-messages )
46to the client.
47The combined acts of transmitting (receiving) a request of a particular type,
48and receiving (transmitting)
49its reply is called a
50.I transaction
51of that type.
52.PP
53Each message consists of a sequence of bytes.
54Two-, four-, and eight-byte fields hold unsigned
55integers represented in little-endian order
56(least significant byte first).
57Data items of larger or variable lengths are represented
58by a two-byte field specifying a count,
59.IR n ,
60followed by
61.I n
62bytes of data.
63Text strings are represented this way,
64with the text itself stored as a UTF-8
65encoded sequence of Unicode characters (see
66.IR utf (7)).
67Text strings in 9P messages are not
68.SM NUL\c
69-terminated:
70.I n
71counts the bytes of UTF-8 data, which include no final zero byte.
72The
73.SM NUL
74character is illegal in all text strings in 9P, and is therefore
75excluded from file names, user names, and so on.
76.PP
77Each 9P message begins with a four-byte size field
78specifying the length in bytes of the complete message including
79the four bytes of the size field itself.
80The next byte is the message type, one of the constants
81in the enumeration in the include file
82.BR <fcall.h> .
83The next two bytes are an identifying
84.IR tag ,
85described below.
86The remaining bytes are parameters of different sizes.
87In the message descriptions, the number of bytes in a field
88is given in brackets after the field name.
89The notation
90.IR parameter [ n ]
91where
92.I n
93is not a constant represents a variable-length parameter:
94.IR n [2]
95followed by
96.I n
97bytes of data forming the
98.IR parameter .
99The notation
100.IR string [ s ]
101(using a literal
102.I s
103character)
104is shorthand for
105.IR s [2]
106followed by
107.I s
108bytes of UTF-8 text.
109(Systems may choose to reduce the set of legal characters
110to reduce syntactic problems,
111for example to remove slashes from name components,
112but the protocol has no such restriction.
113Plan 9 names may contain any printable character (that is, any character
114outside hexadecimal 00-1F and 80-9F)
115except slash.)
116Messages are transported in byte form to allow for machine independence;
117.IR fcall (3)
118describes routines that convert to and from this form into a machine-dependent
119C structure.
120.SH MESSAGES
121.ta \w'\fLTsession 'u
122.IP
123.ne 2v
124.IR size [4]
125.B Tversion
126.IR tag [2]
127.IR msize [4]
128.IR version [ s ]
129.br
130.IR size [4]
131.B Rversion
132.IR tag [2]
133.IR msize [4]
134.IR version [ s ]
135.IP
136.ne 2v
137.IR size [4]
138.B Tauth
139.IR tag [2]
140.IR afid [4]
141.IR uname [ s ]
142.IR aname [ s ]
143.br
144.br
145.IR size [4]
146.B Rauth
147.IR tag [2]
148.IR aqid [13]
149.IP
150.ne 2v
151.IR size [4]
152.B Rerror
153.IR tag [2]
154.IR ename [ s ]
155.IP
156.ne 2v
157.IR size [4]
158.B Tflush
159.IR tag [2]
160.IR oldtag [2]
161.br
162.IR size [4]
163.B Rflush
164.IR tag [2]
165.IP
166.ne 2v
167.IR size [4]
168.B Tattach
169.IR tag [2]
170.IR fid [4]
171.IR afid [4]
172.IR uname [ s ]
173.IR aname [ s ]
174.br
175.IR size [4]
176.B Rattach
177.IR tag [2]
178.IR qid [13]
179.IP
180.ne 2v
181.IR size [4]
182.B Twalk
183.IR tag [2]
184.IR fid [4]
185.IR newfid [4]
186.IR nwname [2]
187.IR nwname *( wname [ s ])
188.br
189.IR size [4]
190.B Rwalk
191.IR tag [2]
192.IR nwqid [2]
193.IR nwqid *( wqid [13])
194.IP
195.ne 2v
196.IR size [4]
197.B Topen
198.IR tag [2]
199.IR fid [4]
200.IR mode [1]
201.br
202.IR size [4]
203.B Ropen
204.IR tag [2]
205.IR qid [13]
206.IR iounit [4]
207.IP
208.ne 2v
209.IR size [4]
210.B Topenfd
211.IR tag [2]
212.IR fid [4]
213.IR mode [1]
214.br
215.IR size [4]
216.B Ropenfd
217.IR tag [2]
218.IR qid [13]
219.IR iounit [4]
220.IR unixfd [4]
221.IP
222.ne 2v
223.IR size [4]
224.B Tcreate
225.IR tag [2]
226.IR fid [4]
227.IR name [ s ]
228.IR perm [4]
229.IR mode [1]
230.br
231.IR size [4]
232.B Rcreate
233.IR tag [2]
234.IR qid [13]
235.IR iounit [4]
236.IP
237.ne 2v
238.IR size [4]
239.B Tread
240.IR tag [2]
241.IR fid [4]
242.IR offset [8]
243.IR count [4]
244.br
245.IR size [4]
246.B Rread
247.IR tag [2]
248.IR count [4]
249.IR data [ count ]
250.IP
251.ne 2v
252.IR size [4]
253.B Twrite
254.IR tag [2]
255.IR fid [4]
256.IR offset [8]
257.IR count [4]
258.IR data [ count ]
259.br
260.IR size [4]
261.B Rwrite
262.IR tag [2]
263.IR count [4]
264.IP
265.ne 2v
266.IR size [4]
267.B Tclunk
268.IR tag [2]
269.IR fid [4]
270.br
271.IR size [4]
272.B Rclunk
273.IR tag [2]
274.IP
275.ne 2v
276.IR size [4]
277.B Tremove
278.IR tag [2]
279.IR fid [4]
280.br
281.IR size [4]
282.B Rremove
283.IR tag [2]
284.IP
285.ne 2v
286.IR size [4]
287.B Tstat
288.IR tag [2]
289.IR fid [4]
290.br
291.IR size [4]
292.B Rstat
293.IR tag [2]
294.IR stat [ n ]
295.IP
296.ne 2v
297.IR size [4]
298.B Twstat
299.IR tag [2]
300.IR fid [4]
301.IR stat [ n ]
302.br
303.IR size [4]
304.B Rwstat
305.IR tag [2]
306.PP
307Each T-message has a
308.I tag
309field, chosen and used by the client to identify the message.
310The reply to the message will have the same tag.
311Clients must arrange that no two outstanding messages
312on the same connection have the same tag.
313An exception is the tag
314.BR NOTAG ,
315defined as
316.B (ushort)~0
317in
318.BR <fcall.h> :
319the client can use it, when establishing a connection,
320to
321override tag matching in
322.B version
323messages.
324.PP
325The type of an R-message will either be one greater than the type
326of the corresponding T-message or
327.BR Rerror ,
328indicating that the request failed.
329In the latter case, the
330.I ename
331field contains a string describing the reason for failure.
332.PP
333The
334.B version
335message identifies the version of the protocol and indicates
336the maximum message size the system is prepared to handle.
337It also initializes the connection and
338aborts all outstanding I/O on the connection.
339The set of messages between
340.B version
341requests is called a
342.IR session .
343.PP
344Most T-messages contain a
345.IR fid ,
346a 32-bit unsigned integer that the client uses to identify
347a ``current file'' on the server.
348Fids are somewhat like file descriptors in a user process,
349but they are not restricted to files open for I/O:
350directories being examined, files being accessed by
351.IR stat (3)
352calls, and so on \(em all files being manipulated by the operating
353system \(em are identified by fids.
354Fids are chosen by the client.
355All requests on a connection share the same fid space;
356when several clients share a connection,
357the agent managing the sharing must arrange
358that no two clients choose the same fid.
359.PP
360The fid supplied in an
361.B attach
362message
363will be taken by the server to refer to the root of the served file tree.
364The
365.B attach
366identifies the user
367to the server and may specify a particular file tree served
368by the server (for those that supply more than one).
369.PP
370Permission to attach to the service is proven by providing a special fid, called
371.BR afid ,
372in the
373.B attach
374message. This
375.B afid
376is established by exchanging
377.B auth
378messages and subsequently manipulated using
379.B read
380and
381.B write
382messages to exchange authentication information not defined explicitly by 9P.
383Once the authentication protocol is complete, the
384.B afid
385is presented in the
386.B attach
387to permit the user to access the service.
388.PP
389A
390.B walk
391message causes the server to change the current file associated
392with a fid to be a file in the directory that is the old current file, or one of
393its subdirectories.
394.B Walk
395returns a new fid that refers to the resulting file.
396Usually, a client maintains a fid for the root,
397and navigates by
398.B walks
399from the root fid.
400.PP
401A client can send multiple T-messages without waiting for the corresponding
402R-messages, but all outstanding T-messages must specify different tags.
403The server may delay the response to a request
404and respond to later ones;
405this is sometimes necessary, for example when the client reads
406from a file that the server synthesizes from external events
407such as keyboard characters.
408.PP
409Replies (R-messages) to
410.BR auth ,
411.BR attach ,
412.BR walk ,
413.BR open ,
414and
415.B create
416requests convey a
417.I qid
418field back to the client.
419The qid represents the server's unique identification for the
420file being accessed:
421two files on the same server hierarchy are the same if and only if their qids
422are the same.
423(The client may have multiple fids pointing to a single file on a server
424and hence having a single qid.)
425The thirteen-byte qid fields hold a one-byte type,
426specifying whether the file is a directory, append-only file, etc.,
427and two unsigned integers:
428first the four-byte qid
429.IR version ,
430then the eight-byte qid
431.IR path .
432The path is an integer unique among all files in the hierarchy.
433If a file is deleted and recreated with the
434same name in the same directory, the old and new path components of the qids
435should be different.
436The version is a version number for a file;
437typically, it is incremented every time the file is modified.
438.PP
439An existing file can be
440.BR opened ,
441or a new file may be
442.B created
443in the current (directory) file.
444I/O of a given number of bytes
445at a given offset
446on an open file is done by
447.B read
448and
449.BR write .
450.PP
451A client should
452.B clunk
453any fid that is no longer needed.
454The
455.B remove
456transaction deletes files.
457.PP
458.B Openfd
459is an extension used by Unix utilities to allow traditional Unix programs
460to have their input or output attached to fids on 9P servers.
461See
462.IR openfd (9p)
463and
464.IR 9pclient (3)
465for details.
466.PP
467The
468.B stat
469transaction retrieves information about the file.
470The
471.I stat
472field in the reply includes the file's
473name,
474access permissions (read, write and execute for owner, group and public),
475access and modification times, and
476owner and group identifications
477(see
478.IR stat (3)).
479The owner and group identifications are textual names.
480The
481.B wstat
482transaction allows some of a file's properties
483to be changed.
484.PP
485A request can be aborted with a
486flush
487request.
488When a server receives a
489.BR Tflush ,
490it should not reply to the message with tag
491.I oldtag
492(unless it has already replied),
493and it should immediately send an
494.BR Rflush .
495The client must wait
496until it gets the
497.B Rflush
498(even if the reply to the original message arrives in the interim),
499at which point
500.I oldtag
501may be reused.
502.PP
503Because the message size is negotiable and some elements of the
504protocol are variable length, it is possible (although unlikely) to have
505a situation where a valid message is too large to fit within the negotiated size.
506For example, a very long file name may cause a
507.B Rstat
508of the file or
509.B Rread
510of its directory entry to be too large to send.
511In most such cases, the server should generate an error rather than
512modify the data to fit, such as by truncating the file name.
513The exception is that a long error string in an
514.B Rerror
515message should be truncated if necessary, since the string is only
516advisory and in some sense arbitrary.
517.PP
518Most programs do not see the 9P protocol directly;
519on Plan 9, calls to library
520routines that access files are
521translated by the kernel's mount driver
522into 9P messages.
523.SS Unix
524On Unix, 9P services are posted as Unix domain sockets in a
525well-known directory (see
526.IR getns (3)
527and
528.IR 9pserve (4)).
529Clients connect to these servers using a 9P client library
530(see
531.IR 9pclient (3)).
532.SH DIRECTORIES
533Directories are created by
534.B create
535with
536.B DMDIR
537set in the permissions argument (see
538.IR stat (9P)).
539The members of a directory can be found with
540.IR read (9P).
541All directories must support
542.B walks
543to the directory
544.B ..
545(dot-dot)
546meaning parent directory, although by convention directories
547contain no explicit entry for
548.B ..
549or
550.B .
551(dot).
552The parent of the root directory of a server's tree is itself.
553.SH "ACCESS PERMISSIONS"
554This section describes the access permission conventions
555implemented by most Plan 9 file servers. These conventions
556are not enforced by the protocol and may differ between servers,
557especially servers built on top of foreign operating systems.
558.PP
559Each file server maintains a set of user and group names.
560Each user can be a member of any number of groups.
561Each group has a
562.I group leader
563who has special privileges (see
564.IR stat (9P)
565and
566Plan 9's \fIusers\fR(6)).
567Every file request has an implicit user id (copied from the original
568.BR attach )
569and an implicit set of groups (every group of which the user is a member).
570.PP
571Each file has an associated
572.I owner
573and
574.I group
575id and
576three sets of permissions:
577those of the owner, those of the group, and those of ``other'' users.
578When the owner attempts to do something to a file, the owner, group,
579and other permissions are consulted, and if any of them grant
580the requested permission, the operation is allowed.
581For someone who is not the owner, but is a member of the file's group,
582the group and other permissions are consulted.
583For everyone else, the other permissions are used.
584Each set of permissions says whether reading is allowed,
585whether writing is allowed, and whether executing is allowed.
586A
587.B walk
588in a directory is regarded as executing the directory,
589not reading it.
590Permissions are kept in the low-order bits of the file
591.IR mode :
592owner read/write/execute permission represented as 1 in bits 8, 7, and 6
593respectively (using 0 to number the low order).
594The group permissions are in bits 5, 4, and 3,
595and the other permissions are in bits 2, 1, and 0.
596.PP
597The file
598.I mode
599contains some additional attributes besides the permissions.
600If bit 31
601.RB ( DMDIR )
602is set, the file is a directory;
603if bit 30
604.RB ( DMAPPEND )
605is set, the file is append-only (offset is ignored in writes);
606if bit 29
607.RB ( DMEXCL )
608is set, the file is exclusive-use (only one client may have it
609open at a time);
610if bit 27
611.RB ( DMAUTH )
612is set, the file is an authentication file established by
613.B auth
614messages;
615if bit 26
616.RB ( DMTMP )
617is set, the contents of the file (or directory) are not included in nightly archives.
618(Bit 28 is skipped for historical reasons.)
619These bits are reproduced, from the top bit down, in the type byte of the Qid:
620.BR QTDIR ,
621.BR QTAPPEND ,
622.BR QTEXCL ,
623(skipping one bit)
624.BR QTAUTH ,
625and
626.BR QTTMP .
627The name
628.BR QTFILE ,
629defined to be zero,
630identifies the value of the type for a plain file.