fcall.c 8.7 KB

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