9p.c 8.9 KB

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