9p.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * linux/fs/9p/9p.c
  3. *
  4. * This file contains functions 9P2000 functions
  5. *
  6. * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
  7. * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to:
  21. * Free Software Foundation
  22. * 51 Franklin Street, Fifth Floor
  23. * Boston, MA 02111-1301 USA
  24. *
  25. */
  26. #include <linux/config.h>
  27. #include <linux/module.h>
  28. #include <linux/errno.h>
  29. #include <linux/fs.h>
  30. #include <linux/idr.h>
  31. #include "debug.h"
  32. #include "v9fs.h"
  33. #include "9p.h"
  34. #include "mux.h"
  35. /**
  36. * v9fs_t_version - negotiate protocol parameters with sever
  37. * @v9ses: 9P2000 session information
  38. * @msize: requested max size packet
  39. * @version: requested version.extension string
  40. * @fcall: pointer to response fcall pointer
  41. *
  42. */
  43. int
  44. v9fs_t_version(struct v9fs_session_info *v9ses, u32 msize,
  45. char *version, struct v9fs_fcall **fcall)
  46. {
  47. struct v9fs_fcall msg;
  48. dprintk(DEBUG_9P, "msize: %d version: %s\n", msize, version);
  49. msg.id = TVERSION;
  50. msg.params.tversion.msize = msize;
  51. msg.params.tversion.version = version;
  52. return v9fs_mux_rpc(v9ses, &msg, fcall);
  53. }
  54. /**
  55. * v9fs_t_attach - mount the server
  56. * @v9ses: 9P2000 session information
  57. * @uname: user name doing the attach
  58. * @aname: remote name being attached to
  59. * @fid: mount fid to attatch to root node
  60. * @afid: authentication fid (in this case result key)
  61. * @fcall: pointer to response fcall pointer
  62. *
  63. */
  64. int
  65. v9fs_t_attach(struct v9fs_session_info *v9ses, char *uname, char *aname,
  66. u32 fid, u32 afid, struct v9fs_fcall **fcall)
  67. {
  68. struct v9fs_fcall msg;
  69. dprintk(DEBUG_9P, "uname '%s' aname '%s' fid %d afid %d\n", uname,
  70. aname, fid, afid);
  71. msg.id = TATTACH;
  72. msg.params.tattach.fid = fid;
  73. msg.params.tattach.afid = afid;
  74. msg.params.tattach.uname = uname;
  75. msg.params.tattach.aname = aname;
  76. return v9fs_mux_rpc(v9ses, &msg, fcall);
  77. }
  78. /**
  79. * v9fs_t_clunk - release a fid (finish a transaction)
  80. * @v9ses: 9P2000 session information
  81. * @fid: fid to release
  82. * @fcall: pointer to response fcall pointer
  83. *
  84. */
  85. int
  86. v9fs_t_clunk(struct v9fs_session_info *v9ses, u32 fid,
  87. struct v9fs_fcall **fcall)
  88. {
  89. struct v9fs_fcall msg;
  90. dprintk(DEBUG_9P, "fid %d\n", fid);
  91. msg.id = TCLUNK;
  92. msg.params.tclunk.fid = fid;
  93. return v9fs_mux_rpc(v9ses, &msg, fcall);
  94. }
  95. /**
  96. * v9fs_v9fs_t_flush - flush a pending transaction
  97. * @v9ses: 9P2000 session information
  98. * @tag: tid to release
  99. *
  100. */
  101. int v9fs_t_flush(struct v9fs_session_info *v9ses, u16 tag)
  102. {
  103. struct v9fs_fcall msg;
  104. dprintk(DEBUG_9P, "oldtag %d\n", tag);
  105. msg.id = TFLUSH;
  106. msg.params.tflush.oldtag = tag;
  107. return v9fs_mux_rpc(v9ses, &msg, NULL);
  108. }
  109. /**
  110. * v9fs_t_stat - read a file's meta-data
  111. * @v9ses: 9P2000 session information
  112. * @fid: fid pointing to file or directory to get info about
  113. * @fcall: pointer to response fcall
  114. *
  115. */
  116. int
  117. v9fs_t_stat(struct v9fs_session_info *v9ses, u32 fid, struct v9fs_fcall **fcall)
  118. {
  119. struct v9fs_fcall msg;
  120. dprintk(DEBUG_9P, "fid %d\n", fid);
  121. if (fcall)
  122. *fcall = NULL;
  123. msg.id = TSTAT;
  124. msg.params.tstat.fid = fid;
  125. return v9fs_mux_rpc(v9ses, &msg, fcall);
  126. }
  127. /**
  128. * v9fs_t_wstat - write a file's meta-data
  129. * @v9ses: 9P2000 session information
  130. * @fid: fid pointing to file or directory to write info about
  131. * @stat: metadata
  132. * @fcall: pointer to response fcall
  133. *
  134. */
  135. int
  136. v9fs_t_wstat(struct v9fs_session_info *v9ses, u32 fid,
  137. struct v9fs_stat *stat, struct v9fs_fcall **fcall)
  138. {
  139. struct v9fs_fcall msg;
  140. dprintk(DEBUG_9P, "fid %d length %d\n", fid, (int)stat->length);
  141. msg.id = TWSTAT;
  142. msg.params.twstat.fid = fid;
  143. msg.params.twstat.stat = stat;
  144. return v9fs_mux_rpc(v9ses, &msg, fcall);
  145. }
  146. /**
  147. * v9fs_t_walk - walk a fid to a new file or directory
  148. * @v9ses: 9P2000 session information
  149. * @fid: fid to walk
  150. * @newfid: new fid (for clone operations)
  151. * @name: path to walk fid to
  152. * @fcall: pointer to response fcall
  153. *
  154. */
  155. /* TODO: support multiple walk */
  156. int
  157. v9fs_t_walk(struct v9fs_session_info *v9ses, u32 fid, u32 newfid,
  158. char *name, struct v9fs_fcall **fcall)
  159. {
  160. struct v9fs_fcall msg;
  161. dprintk(DEBUG_9P, "fid %d newfid %d wname '%s'\n", fid, newfid, name);
  162. msg.id = TWALK;
  163. msg.params.twalk.fid = fid;
  164. msg.params.twalk.newfid = newfid;
  165. if (name) {
  166. msg.params.twalk.nwname = 1;
  167. msg.params.twalk.wnames = &name;
  168. } else {
  169. msg.params.twalk.nwname = 0;
  170. }
  171. return v9fs_mux_rpc(v9ses, &msg, fcall);
  172. }
  173. /**
  174. * v9fs_t_open - open a file
  175. *
  176. * @v9ses - 9P2000 session information
  177. * @fid - fid to open
  178. * @mode - mode to open file (R, RW, etc)
  179. * @fcall - pointer to response fcall
  180. *
  181. */
  182. int
  183. v9fs_t_open(struct v9fs_session_info *v9ses, u32 fid, u8 mode,
  184. struct v9fs_fcall **fcall)
  185. {
  186. struct v9fs_fcall msg;
  187. long errorno = -1;
  188. dprintk(DEBUG_9P, "fid %d mode %d\n", fid, mode);
  189. msg.id = TOPEN;
  190. msg.params.topen.fid = fid;
  191. msg.params.topen.mode = mode;
  192. errorno = v9fs_mux_rpc(v9ses, &msg, fcall);
  193. return errorno;
  194. }
  195. /**
  196. * v9fs_t_remove - remove a file or directory
  197. * @v9ses: 9P2000 session information
  198. * @fid: fid to remove
  199. * @fcall: pointer to response fcall
  200. *
  201. */
  202. int
  203. v9fs_t_remove(struct v9fs_session_info *v9ses, u32 fid,
  204. struct v9fs_fcall **fcall)
  205. {
  206. struct v9fs_fcall msg;
  207. dprintk(DEBUG_9P, "fid %d\n", fid);
  208. msg.id = TREMOVE;
  209. msg.params.tremove.fid = fid;
  210. return v9fs_mux_rpc(v9ses, &msg, fcall);
  211. }
  212. /**
  213. * v9fs_t_create - create a file or directory
  214. * @v9ses: 9P2000 session information
  215. * @fid: fid to create
  216. * @name: name of the file or directory to create
  217. * @perm: permissions to create with
  218. * @mode: mode to open file (R, RW, etc)
  219. * @fcall: pointer to response fcall
  220. *
  221. */
  222. int
  223. v9fs_t_create(struct v9fs_session_info *v9ses, u32 fid, char *name,
  224. u32 perm, u8 mode, struct v9fs_fcall **fcall)
  225. {
  226. struct v9fs_fcall msg;
  227. dprintk(DEBUG_9P, "fid %d name '%s' perm %x mode %d\n",
  228. fid, name, perm, mode);
  229. msg.id = TCREATE;
  230. msg.params.tcreate.fid = fid;
  231. msg.params.tcreate.name = name;
  232. msg.params.tcreate.perm = perm;
  233. msg.params.tcreate.mode = mode;
  234. return v9fs_mux_rpc(v9ses, &msg, fcall);
  235. }
  236. /**
  237. * v9fs_t_read - read data
  238. * @v9ses: 9P2000 session information
  239. * @fid: fid to read from
  240. * @offset: offset to start read at
  241. * @count: how many bytes to read
  242. * @fcall: pointer to response fcall (with data)
  243. *
  244. */
  245. int
  246. v9fs_t_read(struct v9fs_session_info *v9ses, u32 fid, u64 offset,
  247. u32 count, struct v9fs_fcall **fcall)
  248. {
  249. struct v9fs_fcall msg;
  250. struct v9fs_fcall *rc = NULL;
  251. long errorno = -1;
  252. dprintk(DEBUG_9P, "fid %d offset 0x%lx count 0x%x\n", fid,
  253. (long unsigned int)offset, count);
  254. msg.id = TREAD;
  255. msg.params.tread.fid = fid;
  256. msg.params.tread.offset = offset;
  257. msg.params.tread.count = count;
  258. errorno = v9fs_mux_rpc(v9ses, &msg, &rc);
  259. if (!errorno) {
  260. errorno = rc->params.rread.count;
  261. dump_data(rc->params.rread.data, rc->params.rread.count);
  262. }
  263. if (fcall)
  264. *fcall = rc;
  265. else
  266. kfree(rc);
  267. return errorno;
  268. }
  269. /**
  270. * v9fs_t_write - write data
  271. * @v9ses: 9P2000 session information
  272. * @fid: fid to write to
  273. * @offset: offset to start write at
  274. * @count: how many bytes to write
  275. * @fcall: pointer to response fcall
  276. *
  277. */
  278. int
  279. v9fs_t_write(struct v9fs_session_info *v9ses, u32 fid,
  280. u64 offset, u32 count, void *data, struct v9fs_fcall **fcall)
  281. {
  282. struct v9fs_fcall msg;
  283. struct v9fs_fcall *rc = NULL;
  284. long errorno = -1;
  285. dprintk(DEBUG_9P, "fid %d offset 0x%llx count 0x%x\n", fid,
  286. (unsigned long long)offset, count);
  287. dump_data(data, count);
  288. msg.id = TWRITE;
  289. msg.params.twrite.fid = fid;
  290. msg.params.twrite.offset = offset;
  291. msg.params.twrite.count = count;
  292. msg.params.twrite.data = data;
  293. errorno = v9fs_mux_rpc(v9ses, &msg, &rc);
  294. if (!errorno)
  295. errorno = rc->params.rwrite.count;
  296. if (fcall)
  297. *fcall = rc;
  298. else
  299. kfree(rc);
  300. return errorno;
  301. }