Blob


1 .TH 9P 3
2 .SH NAME
3 Srv,
4 dirread9p,
5 emalloc9p,
6 erealloc9p,
7 estrdup9p,
8 postfd,
9 postmountsrv,
10 readbuf,
11 readstr,
12 respond,
13 threadpostmountsrv,
14 srv \- 9P file service
15 .SH SYNOPSIS
16 .ft L
17 .nf
18 #include <u.h>
19 #include <libc.h>
20 #include <fcall.h>
21 #include <thread.h>
22 #include <9p.h>
23 .fi
24 .PP
25 .ft L
26 .nf
27 .ta \w'\fL1234'u +\w'\fLTree* 'u
28 typedef struct Srv {
29 Tree* tree;
31 void (*attach)(Req *r);
32 void (*auth)(Req *r);
33 void (*open)(Req *r);
34 void (*create)(Req *r);
35 void (*read)(Req *r);
36 void (*write)(Req *r);
37 void (*remove)(Req *r);
38 void (*flush)(Req *r);
39 void (*stat)(Req *r);
40 void (*wstat)(Req *r);
41 void (*walk)(Req *r);
43 char* (*walk1)(Fid *fid, char *name, Qid *qid);
44 char* (*clone)(Fid *oldfid, Fid *newfid);
46 void (*destroyfid)(Fid *fid);
47 void (*destroyreq)(Req *r);
48 void (*end)(Srv *s);
49 void* aux;
51 int infd;
52 int outfd;
53 int srvfd;
54 int nopipe;
55 } Srv;
56 .fi
57 .PP
58 .nf
59 .ft L
60 .ta \w'\fLvoid* 'u
61 int srv(Srv *s)
62 void postmountsrv(Srv *s, char *name, char *mtpt, int flag)
63 void threadpostmountsrv(Srv *s, char *name, char *mtpt, int flag)
64 int postfd(char *srvname, int fd)
65 void respond(Req *r, char *error)
66 ulong readstr(Req *r, char *src)
67 ulong readbuf(Req *r, void *src, ulong nsrc)
68 typedef int Dirgen(int n, Dir *dir, void *aux)
69 void dirread9p(Req *r, Dirgen *gen, void *aux)
70 void walkandclone(Req *r, char *(*walk1)(Fid *old, char *name, void *v),
71 char *(*clone)(Fid *old, Fid *new, void *v), void *v)
72 .fi
73 .PP
74 .nf
75 .ft L
76 .ta \w'\fLvoid* 'u
77 void* emalloc9p(ulong n)
78 void* erealloc9p(void *v, ulong n)
79 char* estrdup9p(char *s)
80 .fi
81 .PP
82 .nf
83 .ft L
84 extern int chatty9p;
85 .fi
86 .SH DESCRIPTION
87 The function
88 .I srv
89 serves a 9P session by reading requests from
90 .BR s->infd ,
91 dispatching them to the function pointers kept in
92 .BR Srv ,
93 and
94 writing the responses to
95 .BR s->outfd .
96 (Typically,
97 .I postmountsrv
98 or
99 .I threadpostmountsrv
100 initializes the
101 .B infd
102 and
103 .B outfd
104 structure members. See the description below.)
105 .PP
106 .B Req
107 and
108 .B Fid
109 structures are allocated one-to-one with uncompleted
110 requests and active fids, and are described in
111 .IR 9pfid (3).
112 .PP
113 The behavior of
114 .I srv
115 depends on whether there is a file tree
116 (see
117 .IR 9pfile (3))
118 associated with the server, that is,
119 whether the
120 .B tree
121 element is nonzero.
122 The differences are made explicit in the
123 discussion of the service loop below.
124 The
125 .B aux
126 element is the client's, to do with as it pleases.
127 .PP
128 .I Srv
129 does not return until the 9P conversation is finished.
130 Since it is usually run in a separate process so that
131 the caller can exit, the service loop has little chance
132 to return gracefully on out of memory errors.
133 It calls
134 .IR emalloc9p ,
135 .IR erealloc9p ,
136 and
137 .I estrdup9p
138 to obtain its memory.
139 The default implementations of these functions
140 act as
141 .IR malloc ,
142 .IR realloc ,
143 and
144 .I strdup
145 but abort the program if they run out of memory.
146 If alternate behavior is desired, clients can link against
147 alternate implementations of these functions.
148 .PP
149 .I Postmountsrv
150 and
151 .I threadpostmountsrv
152 are wrappers that create a separate process in which to run
153 .IR srv .
154 They do the following:
155 .IP
156 If
157 .IB s -> nopipe
158 is zero (the common case),
159 initialize
160 .IB s -> infd
161 and
162 .IB s -> outfd
163 to be one end of a freshly allocated pipe,
164 with
165 .IB s -> srvfd
166 initialized as the other end.
167 .IP
168 If
169 .B name
170 is non-nil, call
171 .BI postfd( s -> srvfd ,
172 .IB name )
173 to post
174 .IB s -> srvfd
175 as
176 .BI /srv/ name .
177 .IP
178 Fork a child process via
179 .I rfork
180 (see
181 .IR fork (3))
182 or
183 .I procrfork
184 (see
185 .IR thread (3)),
186 using the
187 .BR RFFDG ,
188 .RR RFNOTEG ,
189 .BR RFNAMEG ,
190 and
191 .BR RFMEM
192 flags.
193 The child process
194 calls
195 .IB close( s -> srvfd )
196 and then
197 .IB srv( s ) \fR;
198 it will exit once
199 .I srv
200 returns.
201 .IP
202 If
203 .I mtpt
204 is non-nil,
205 call
206 .BI amount( s -> srvfd,
207 .IB mtpt ,
208 .IB flag ,
209 \fB"")\fR;
210 otherwise, close
211 .IB s -> srvfd \fR.
212 .IP
213 The parent returns to the caller.
214 .LP
215 If any error occurs during
216 this process, the entire process is terminated by calling
217 .I sysfatal
218 (see
219 .IR perror (3)).
220 .SS Service functions
221 The functions in a
222 .B Srv
223 structure named after 9P transactions
224 are called to satisfy requests as they arrive.
225 If a function is provided, it
226 .I must
227 arrange for
228 .I respond
229 to be called when the request is satisfied.
230 The only parameter of each service function
231 is a
232 .B Req*
233 parameter (say
234 .IR r ).
235 The incoming request parameters are stored in
236 .IB r -> ifcall \fR;
237 .IB r -> fid
238 and
239 .IB r -> newfid
240 are pointers to
241 .B Fid
242 structures corresponding to the
243 numeric fids in
244 .IB r -> ifcall \fR;
245 similarly,
246 .IB r -> oldreq
247 is the
248 .B Req
249 structure corresponding to
250 .IB r -> ifcall.oldtag \fR.
251 The outgoing response data should be stored in
252 .IB r -> ofcall \fR.
253 The one exception to this rule is that
254 .I stat
255 should fill in
256 .IB r -> d
257 rather than
258 .IB r -> ofcall.stat \fR:
259 the library will convert the structure into the machine-independent
260 wire representation.
261 Similarly,
262 .I wstat
263 may consult
264 .IB r -> d
265 rather than decoding
266 .IB r -> ifcall . stat
267 itself.
268 When a request has been handled,
269 .I respond
270 should be called with
271 .I r
272 and an error string.
273 If the request was satisfied successfully, the error
274 string should be a nil pointer.
275 Note that it is permissible for a function to return
276 without itself calling
277 .IR respond ,
278 as long as it has arranged for
279 .I respond
280 to be called at some point in the future
281 by another proc sharing its address space,
282 but see the discussion of
283 .I flush
284 below.
285 Once
286 .I respond
287 has been called, the
288 .B Req*
289 as well as any pointers it once contained must
290 be considered freed and not referenced.
291 .PP
292 If the service loop detects an error in a request
293 (e.g., an attempt to reuse an extant fid, an open of
294 an already open fid, a read from a fid opened for write, etc.)
295 it will reply with an error without consulting
296 the service functions.
297 .PP
298 The service loop provided by
299 .I srv
300 (and indirectly by
301 .I postmountsrv
302 and
303 .IR threadpostmountsrv )
304 is single-threaded.
305 If it is expected that some requests might
306 block, arranging for alternate processes
307 to handle them is suggested.
308 .PP
309 The constraints on the service functions are as follows.
310 These constraints are checked while the server executes.
311 If a service function fails to do something it ought to have,
312 .I srv
313 will call
314 .I endsrv
315 and then abort.
316 .TP
317 .I Auth
318 If authentication is desired,
319 the
320 .I auth
321 function should record that
322 .I afid
323 is the new authentication fid and
324 set
325 .I afid->qid
326 and
327 .IR ofcall.qid .
328 .I Auth
329 may be nil, in which case it will be treated as having
330 responded with the error
331 .RI `` "argv0: authentication not required" ,''
332 where
333 .I argv0
334 is the program name variable as set by
335 .I ARGBEGIN
336 (see
337 .IR arg (3)).
338 .TP
339 .I Attach
340 The
341 .I attach
342 function should check the authentication state of
343 .I afid
344 if desired,
345 and set
346 .IB r -> fid -> qid
347 and
348 .I ofcall.qid
349 to the qid of the file system root.
350 .I Attach
351 may be nil only if file trees are in use;
352 in this case, the qid will be filled from the root
353 of the tree, and no authentication will be done.
354 .TP
355 .I Walk
356 If file trees are in use,
357 .I walk
358 is handled internally, and
359 .IB srv -> walk
360 is never called.
361 .IP
362 If file trees are not in use,
363 .I walk
364 should consult
365 .IB r -> ifcall . wname
366 and
367 .IB r -> ifcall . nwname \fR,
368 filling in
369 .IB ofcall . qid
370 and
371 .IB ofcall . nqid \fR,
372 and also copying any necessary
373 .I aux
374 state from
375 .IB r -> fid
376 to
377 .IB r -> newfid
378 when the two are different.
379 As long as
380 .I walk
381 sets
382 .IB ofcall . nqid
383 appropriately, it can
384 .I respond
385 with a nil error string even when 9P
386 demands an error
387 .RI ( e.g. ,
388 in the case of a short walk);
389 the library detects error conditions and handles them appropriately.
390 .IP
391 Because implementing the full walk message is intricate and
392 prone to error, the helper routine
393 .I walkandclone
394 will handle the request given pointers to two functions
395 .I walk1
396 and (optionally)
397 .I clone .
398 .IR Clone ,
399 if non-nil, is called to signal the creation of
400 .I newfid
401 from
402 .IR oldfid .
403 Typically a
404 .I clone
405 routine will copy or increment a reference count in
406 .IR oldfid 's
407 .I aux
408 element.
409 .I Walk1
410 should walk
411 .I fid
412 to
413 .IR name ,
414 initializing
415 .IB fid -> qid
416 to the new path's qid.
417 Both should return nil
418 on success or an error message on error.
419 .I Walkandclone
420 will call
421 .I respond
422 after handling the request.
423 .TP
424 .I Walk1\fR, \fPClone
425 If the client provides functions
426 .IB srv -> walk1
427 and (optionally)
428 .IB srv -> clone \fR,
429 the 9P service loop will call
430 .I walkandclone
431 with these functions to handle the request.
432 Unlike the
433 .I walk1
434 above,
435 .IB srv -> walk1
436 must fill in both
437 .IB fid -> qid
438 and
439 .BI * qid
440 with the new qid on a successful walk.
441 .TP
442 .I Open
443 If file trees are in use, the file
444 metadata will be consulted on open, create, remove, and wstat
445 to see if the requester has the appropriate permissions.
446 If not, an error will be sent back without consulting a service function.
447 .PP
448 If not using file trees or the user has the appropriate permissions,
449 .I open
450 is called with
451 .IB r -> ofcall . qid
452 already initialized to the one stored in the
453 .B Fid
454 structure (that is, the one returned in the previous walk).
455 If the qid changes, both should be updated.
456 .TP
457 .I Create
458 The
459 .I create
460 function must fill in
461 both
462 .IB r -> fid -> qid
463 and
464 .IB r -> ofcall . qid
465 on success.
466 When using file trees,
467 .I create
468 should allocate a new
469 .B File
470 with
471 .IR createfile ;
472 note that
473 .I createfile
474 may return nil (because, say, the file already exists).
475 If the
476 .I create
477 function is nil,
478 .I srv
479 behaves as though it were a function that always responded
480 with the error ``create prohibited''.
481 .TP
482 .I Remove
483 .I Remove
484 should mark the file as removed, whether
485 by calling
486 .I removefile
487 when using file trees, or by updating an internal data structure.
488 In general it is not a good idea to clean up the
489 .I aux
490 information associated with the corresponding
491 .B File
492 at this time, to avoid memory errors if other
493 fids have references to that file.
494 Instead, it is suggested that
495 .I remove
496 simply mark the file as removed (so that further
497 operations on it know to fail) and wait until the
498 file tree's destroy function is called to reclaim the
499 .I aux
500 pointer.
501 If not using file trees, it is prudent to take the
502 analogous measures.
503 If
504 .I remove
505 is not provided, all remove requests will draw
506 ``remove prohibited'' errors.
507 .TP
508 .I Read
509 The
510 .I read
511 function must be provided; it fills
512 .IB r -> ofcall . data
513 with at most
514 .IB r -> ifcall . count
515 bytes of data from offset
516 .IB r -> ifcall . offset
517 of the file.
518 It also sets
519 .IB r -> ofcall . count
520 to the number of bytes being returned.
521 If using file trees,
522 .I srv
523 will handle reads of directories internally, only
524 calling
525 .I read
526 for requests on files.
527 .I Readstr
528 and
529 .I readbuf
530 are useful for satisfying read requests on a string or buffer.
531 Consulting the request in
532 .IB r -> ifcall \fR,
533 they fill
534 .IB r -> ofcall . data
535 and set
536 .IB r -> ofcall . count \fR;
537 they do not call
538 .IB respond .
539 Similarly,
540 .I dirread9p
541 can be used to handle directory reads in servers
542 not using file trees.
543 The passed
544 .I gen
545 function will be called as necessary to
546 fill
547 .I dir
548 with information for the
549 .IR n th
550 entry in the directory.
551 The string pointers placed in
552 .I dir
553 should be fresh copies
554 made with
555 .IR estrdup9p ;
556 they will be freed by
557 .I dirread9p
558 after each successful call to
559 .IR gen .
560 .I Gen
561 should return zero if it successfully filled
562 .IR dir ,
563 minus one on end of directory.
564 .TP
565 .I Write
566 The
567 .I write
568 function is similar but need not be provided.
569 If it is not, all writes will draw
570 ``write prohibited'' errors.
571 Otherwise,
572 .I write
573 should attempt to write the
574 .IB r -> ifcall . count
575 bytes of
576 .IB r -> ifcall . data
577 to offset
578 .IB r -> ifcall . offset
579 of the file, setting
580 .IB r -> ofcall . count
581 to the number of bytes actually written.
582 Most programs consider it an error to
583 write less than the requested amount.
584 .TP
585 .I Stat
586 .I Stat
587 should fill
588 .IB r -> d
589 with the stat information for
590 .IB r -> fid \fR.
591 If using file trees,
592 .IB r -> d
593 will have been initialized with the stat info from
594 the tree, and
595 .I stat
596 itself may be nil.
597 .TP
598 .I Wstat
599 The
600 .I wstat
601 consults
602 .IB r -> d
603 in changing the metadata for
604 .IB r -> fid
605 as described in
606 .IR stat (5).
607 When using file trees,
608 .I srv
609 will take care to check that the request satisfies
610 the permissions outlined in
611 .IR stat (5).
612 Otherwise
613 .I wstat
614 should take care to enforce permissions
615 where appropriate.
616 .TP
617 .I Flush
618 Single-threaded servers, which always call
619 .I respond
620 before returning from the service functions,
621 need not provide a
622 .I flush
623 implementation:
624 .I flush
625 is only necessary in multithreaded programs,
626 which arrange for
627 .I respond
628 to be called asynchronously.
629 .I Flush
630 should cause the request
631 .IB r -> oldreq
632 to be cancelled or hurried along.
633 If
634 .I oldreq
635 is cancelled, this should be signalled by calling
636 .I respond
637 on
638 .I oldreq
639 with error string
640 .RB ` interrupted '.
641 .I Flush
642 must respond to
643 .I r
644 with a nil error string.
645 .I Flush
646 may respond to
647 .I r
648 before forcing a response to
649 .IB r -> oldreq \fR.
650 In this case, the library will delay sending
651 the
652 .I Rflush
653 message until the response to
654 .IB r -> oldreq
655 has been sent.
656 .PD
657 .PP
658 .IR Destroyfid ,
659 .IR destroyreq ,
660 and
661 .I end
662 are auxiliary functions, not called in direct response to 9P requests.
663 .TP
664 .I Destroyfid
665 When a
666 .BR Fid 's
667 reference count drops to zero
668 .RI ( i.e.,
669 it has been clunked and there are no outstanding
670 requests referring to it),
671 .I destroyfid
672 is called to allow the program to dispose
673 of the
674 .IB fid -> aux
675 pointer.
676 .TP
677 .I Destroyreq
678 Similarly, when a
679 .BR Req 's
680 reference count drops to zero
681 .RI ( i.e. ,
682 it has been handled via
683 .I respond
684 and other outstanding pointers to it have been closed),
685 .I destroyreq
686 is called to allow the program to dispose of the
687 .IB r -> aux
688 pointer.
689 .TP
690 .I End
691 Once the 9P service loop has finished
692 (end of file been reached on the service pipe
693 or a bad message has been read),
694 .I end
695 is called (if provided) to allow any final cleanup.
696 For example, it was used by the Palm Pilot synchronization
697 file system (never finished) to gracefully terminate the serial conversation once
698 the file system had been unmounted.
699 After calling
700 .IR end ,
701 the service loop (which runs in a separate process
702 from its caller) terminates using
703 .I _exits
704 (see
705 .IR exits (3)).
706 .PD
707 .PP
708 If the
709 .B chatty9p
710 flag is at least one,
711 a transcript of the 9P session is printed
712 on standard error.
713 If the
714 .B chatty9p
715 flag is greater than one,
716 additional unspecified debugging output is generated.
717 By convention, servers written using this library
718 accept the
719 .B -D
720 option to increment
721 .BR chatty9p .
722 .SH EXAMPLES
723 .IR Archfs (4),
724 .IR cdfs (4),
725 .IR nntpfs (4),
726 .IR snap (4),
727 and
728 .B /sys/src/lib9p/ramfs.c
729 are good examples of simple single-threaded file servers.
730 .IR Webfs (4)
731 and
732 .I sshnet
733 (see
734 .IR ssh (1))
735 are good examples of multithreaded file servers.
736 .PP
737 In general, the
738 .B File
739 interface is appropriate for maintaining arbitrary file trees (as in
740 .IR ramfs ).
741 The
742 .B File
743 interface is best avoided when the
744 tree structure is easily generated as necessary;
745 this is true when the tree is highly structured (as in
746 .I cdfs
747 and
748 .IR nntpfs )
749 or is maintained elsewhere.
750 .SH SOURCE
751 .B /sys/src/lib9p
752 .SH SEE ALSO
753 .IR 9pfid (3),
754 .IR 9pfile (3),
755 .IR srv (3),
756 .IR intro (5)
757 .SH BUGS
758 The switch to 9P2000 was taken as an opportunity to tidy
759 much of the interface; we promise to avoid such gratuitous change
760 in the future.