fcall.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * linux/fs/9p/fcall.c
  3. *
  4. * This file contains functions to perform synchronous 9P calls
  5. *
  6. * Copyright (C) 2004 by Latchesar Ionkov <lucho@ionkov.net>
  7. * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
  8. * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2
  12. * as published by the Free Software Foundation.
  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/module.h>
  27. #include <linux/errno.h>
  28. #include <linux/fs.h>
  29. #include <linux/idr.h>
  30. #include "debug.h"
  31. #include "v9fs.h"
  32. #include "9p.h"
  33. #include "conv.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 **rcp)
  46. {
  47. int ret;
  48. struct v9fs_fcall *tc;
  49. dprintk(DEBUG_9P, "msize: %d version: %s\n", msize, version);
  50. tc = v9fs_create_tversion(msize, version);
  51. if (!IS_ERR(tc)) {
  52. ret = v9fs_mux_rpc(v9ses->mux, tc, rcp);
  53. kfree(tc);
  54. } else
  55. ret = PTR_ERR(tc);
  56. return ret;
  57. }
  58. /**
  59. * v9fs_t_attach - mount the server
  60. * @v9ses: 9P2000 session information
  61. * @uname: user name doing the attach
  62. * @aname: remote name being attached to
  63. * @fid: mount fid to attatch to root node
  64. * @afid: authentication fid (in this case result key)
  65. * @fcall: pointer to response fcall pointer
  66. *
  67. */
  68. int
  69. v9fs_t_attach(struct v9fs_session_info *v9ses, char *uname, char *aname,
  70. u32 fid, u32 afid, struct v9fs_fcall **rcp)
  71. {
  72. int ret;
  73. struct v9fs_fcall* tc;
  74. dprintk(DEBUG_9P, "uname '%s' aname '%s' fid %d afid %d\n", uname,
  75. aname, fid, afid);
  76. tc = v9fs_create_tattach(fid, afid, uname, aname);
  77. if (!IS_ERR(tc)) {
  78. ret = v9fs_mux_rpc(v9ses->mux, tc, rcp);
  79. kfree(tc);
  80. } else
  81. ret = PTR_ERR(tc);
  82. return ret;
  83. }
  84. static void v9fs_t_clunk_cb(void *a, struct v9fs_fcall *tc,
  85. struct v9fs_fcall *rc, int err)
  86. {
  87. int fid, id;
  88. struct v9fs_session_info *v9ses;
  89. id = 0;
  90. fid = tc->params.tclunk.fid;
  91. if (rc)
  92. id = rc->id;
  93. kfree(tc);
  94. kfree(rc);
  95. if (id == RCLUNK) {
  96. v9ses = a;
  97. v9fs_put_idpool(fid, &v9ses->fidpool);
  98. }
  99. }
  100. /**
  101. * v9fs_t_clunk - release a fid (finish a transaction)
  102. * @v9ses: 9P2000 session information
  103. * @fid: fid to release
  104. * @fcall: pointer to response fcall pointer
  105. *
  106. */
  107. int
  108. v9fs_t_clunk(struct v9fs_session_info *v9ses, u32 fid)
  109. {
  110. int ret;
  111. struct v9fs_fcall *tc, *rc;
  112. dprintk(DEBUG_9P, "fid %d\n", fid);
  113. rc = NULL;
  114. tc = v9fs_create_tclunk(fid);
  115. if (!IS_ERR(tc))
  116. ret = v9fs_mux_rpc(v9ses->mux, tc, &rc);
  117. else
  118. ret = PTR_ERR(tc);
  119. if (ret)
  120. dprintk(DEBUG_ERROR, "failed fid %d err %d\n", fid, ret);
  121. v9fs_t_clunk_cb(v9ses, tc, rc, ret);
  122. return ret;
  123. }
  124. #if 0
  125. /**
  126. * v9fs_v9fs_t_flush - flush a pending transaction
  127. * @v9ses: 9P2000 session information
  128. * @tag: tag to release
  129. *
  130. */
  131. int v9fs_t_flush(struct v9fs_session_info *v9ses, u16 oldtag)
  132. {
  133. int ret;
  134. struct v9fs_fcall *tc;
  135. dprintk(DEBUG_9P, "oldtag %d\n", oldtag);
  136. tc = v9fs_create_tflush(oldtag);
  137. if (!IS_ERR(tc)) {
  138. ret = v9fs_mux_rpc(v9ses->mux, tc, NULL);
  139. kfree(tc);
  140. } else
  141. ret = PTR_ERR(tc);
  142. return ret;
  143. }
  144. #endif
  145. /**
  146. * v9fs_t_stat - read a file's meta-data
  147. * @v9ses: 9P2000 session information
  148. * @fid: fid pointing to file or directory to get info about
  149. * @fcall: pointer to response fcall
  150. *
  151. */
  152. int
  153. v9fs_t_stat(struct v9fs_session_info *v9ses, u32 fid, struct v9fs_fcall **rcp)
  154. {
  155. int ret;
  156. struct v9fs_fcall *tc;
  157. dprintk(DEBUG_9P, "fid %d\n", fid);
  158. ret = -ENOMEM;
  159. tc = v9fs_create_tstat(fid);
  160. if (!IS_ERR(tc)) {
  161. ret = v9fs_mux_rpc(v9ses->mux, tc, rcp);
  162. kfree(tc);
  163. } else
  164. ret = PTR_ERR(tc);
  165. return ret;
  166. }
  167. /**
  168. * v9fs_t_wstat - write a file's meta-data
  169. * @v9ses: 9P2000 session information
  170. * @fid: fid pointing to file or directory to write info about
  171. * @stat: metadata
  172. * @fcall: pointer to response fcall
  173. *
  174. */
  175. int
  176. v9fs_t_wstat(struct v9fs_session_info *v9ses, u32 fid,
  177. struct v9fs_wstat *wstat, struct v9fs_fcall **rcp)
  178. {
  179. int ret;
  180. struct v9fs_fcall *tc;
  181. dprintk(DEBUG_9P, "fid %d\n", fid);
  182. tc = v9fs_create_twstat(fid, wstat, v9ses->extended);
  183. if (!IS_ERR(tc)) {
  184. ret = v9fs_mux_rpc(v9ses->mux, tc, rcp);
  185. kfree(tc);
  186. } else
  187. ret = PTR_ERR(tc);
  188. return ret;
  189. }
  190. /**
  191. * v9fs_t_walk - walk a fid to a new file or directory
  192. * @v9ses: 9P2000 session information
  193. * @fid: fid to walk
  194. * @newfid: new fid (for clone operations)
  195. * @name: path to walk fid to
  196. * @fcall: pointer to response fcall
  197. *
  198. */
  199. /* TODO: support multiple walk */
  200. int
  201. v9fs_t_walk(struct v9fs_session_info *v9ses, u32 fid, u32 newfid,
  202. char *name, struct v9fs_fcall **rcp)
  203. {
  204. int ret;
  205. struct v9fs_fcall *tc;
  206. int nwname;
  207. dprintk(DEBUG_9P, "fid %d newfid %d wname '%s'\n", fid, newfid, name);
  208. if (name)
  209. nwname = 1;
  210. else
  211. nwname = 0;
  212. tc = v9fs_create_twalk(fid, newfid, nwname, &name);
  213. if (!IS_ERR(tc)) {
  214. ret = v9fs_mux_rpc(v9ses->mux, tc, rcp);
  215. kfree(tc);
  216. } else
  217. ret = PTR_ERR(tc);
  218. return ret;
  219. }
  220. /**
  221. * v9fs_t_open - open a file
  222. *
  223. * @v9ses - 9P2000 session information
  224. * @fid - fid to open
  225. * @mode - mode to open file (R, RW, etc)
  226. * @fcall - pointer to response fcall
  227. *
  228. */
  229. int
  230. v9fs_t_open(struct v9fs_session_info *v9ses, u32 fid, u8 mode,
  231. struct v9fs_fcall **rcp)
  232. {
  233. int ret;
  234. struct v9fs_fcall *tc;
  235. dprintk(DEBUG_9P, "fid %d mode %d\n", fid, mode);
  236. tc = v9fs_create_topen(fid, mode);
  237. if (!IS_ERR(tc)) {
  238. ret = v9fs_mux_rpc(v9ses->mux, tc, rcp);
  239. kfree(tc);
  240. } else
  241. ret = PTR_ERR(tc);
  242. return ret;
  243. }
  244. /**
  245. * v9fs_t_remove - remove a file or directory
  246. * @v9ses: 9P2000 session information
  247. * @fid: fid to remove
  248. * @fcall: pointer to response fcall
  249. *
  250. */
  251. int
  252. v9fs_t_remove(struct v9fs_session_info *v9ses, u32 fid,
  253. struct v9fs_fcall **rcp)
  254. {
  255. int ret;
  256. struct v9fs_fcall *tc;
  257. dprintk(DEBUG_9P, "fid %d\n", fid);
  258. tc = v9fs_create_tremove(fid);
  259. if (!IS_ERR(tc)) {
  260. ret = v9fs_mux_rpc(v9ses->mux, tc, rcp);
  261. kfree(tc);
  262. } else
  263. ret = PTR_ERR(tc);
  264. return ret;
  265. }
  266. /**
  267. * v9fs_t_create - create a file or directory
  268. * @v9ses: 9P2000 session information
  269. * @fid: fid to create
  270. * @name: name of the file or directory to create
  271. * @perm: permissions to create with
  272. * @mode: mode to open file (R, RW, etc)
  273. * @fcall: pointer to response fcall
  274. *
  275. */
  276. int
  277. v9fs_t_create(struct v9fs_session_info *v9ses, u32 fid, char *name, u32 perm,
  278. u8 mode, char *extension, struct v9fs_fcall **rcp)
  279. {
  280. int ret;
  281. struct v9fs_fcall *tc;
  282. dprintk(DEBUG_9P, "fid %d name '%s' perm %x mode %d\n",
  283. fid, name, perm, mode);
  284. tc = v9fs_create_tcreate(fid, name, perm, mode, extension,
  285. v9ses->extended);
  286. if (!IS_ERR(tc)) {
  287. ret = v9fs_mux_rpc(v9ses->mux, tc, rcp);
  288. kfree(tc);
  289. } else
  290. ret = PTR_ERR(tc);
  291. return ret;
  292. }
  293. /**
  294. * v9fs_t_read - read data
  295. * @v9ses: 9P2000 session information
  296. * @fid: fid to read from
  297. * @offset: offset to start read at
  298. * @count: how many bytes to read
  299. * @fcall: pointer to response fcall (with data)
  300. *
  301. */
  302. int
  303. v9fs_t_read(struct v9fs_session_info *v9ses, u32 fid, u64 offset,
  304. u32 count, struct v9fs_fcall **rcp)
  305. {
  306. int ret;
  307. struct v9fs_fcall *tc, *rc;
  308. dprintk(DEBUG_9P, "fid %d offset 0x%llux count 0x%x\n", fid,
  309. (long long unsigned) offset, count);
  310. tc = v9fs_create_tread(fid, offset, count);
  311. if (!IS_ERR(tc)) {
  312. ret = v9fs_mux_rpc(v9ses->mux, tc, &rc);
  313. if (!ret)
  314. ret = rc->params.rread.count;
  315. if (rcp)
  316. *rcp = rc;
  317. else
  318. kfree(rc);
  319. kfree(tc);
  320. } else
  321. ret = PTR_ERR(tc);
  322. return ret;
  323. }
  324. /**
  325. * v9fs_t_write - write data
  326. * @v9ses: 9P2000 session information
  327. * @fid: fid to write to
  328. * @offset: offset to start write at
  329. * @count: how many bytes to write
  330. * @fcall: pointer to response fcall
  331. *
  332. */
  333. int
  334. v9fs_t_write(struct v9fs_session_info *v9ses, u32 fid, u64 offset, u32 count,
  335. const char __user *data, struct v9fs_fcall **rcp)
  336. {
  337. int ret;
  338. struct v9fs_fcall *tc, *rc;
  339. dprintk(DEBUG_9P, "fid %d offset 0x%llux count 0x%x\n", fid,
  340. (long long unsigned) offset, count);
  341. tc = v9fs_create_twrite(fid, offset, count, data);
  342. if (!IS_ERR(tc)) {
  343. ret = v9fs_mux_rpc(v9ses->mux, tc, &rc);
  344. if (!ret)
  345. ret = rc->params.rwrite.count;
  346. if (rcp)
  347. *rcp = rc;
  348. else
  349. kfree(rc);
  350. kfree(tc);
  351. } else
  352. ret = PTR_ERR(tc);
  353. return ret;
  354. }