9p 15 KB

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