conv.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /*
  2. * net/9p/conv.c
  3. *
  4. * 9P protocol conversion functions
  5. *
  6. * Copyright (C) 2004, 2005 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 <linux/uaccess.h>
  32. #include <net/9p/9p.h>
  33. /*
  34. * Buffer to help with string parsing
  35. */
  36. struct cbuf {
  37. unsigned char *sp;
  38. unsigned char *p;
  39. unsigned char *ep;
  40. };
  41. static inline void buf_init(struct cbuf *buf, void *data, int datalen)
  42. {
  43. buf->sp = buf->p = data;
  44. buf->ep = data + datalen;
  45. }
  46. static inline int buf_check_overflow(struct cbuf *buf)
  47. {
  48. return buf->p > buf->ep;
  49. }
  50. static int buf_check_size(struct cbuf *buf, int len)
  51. {
  52. if (buf->p + len > buf->ep) {
  53. if (buf->p < buf->ep) {
  54. P9_EPRINTK(KERN_ERR,
  55. "buffer overflow: want %d has %d\n", len,
  56. (int)(buf->ep - buf->p));
  57. dump_stack();
  58. buf->p = buf->ep + 1;
  59. }
  60. return 0;
  61. }
  62. return 1;
  63. }
  64. static void *buf_alloc(struct cbuf *buf, int len)
  65. {
  66. void *ret = NULL;
  67. if (buf_check_size(buf, len)) {
  68. ret = buf->p;
  69. buf->p += len;
  70. }
  71. return ret;
  72. }
  73. static void buf_put_int8(struct cbuf *buf, u8 val)
  74. {
  75. if (buf_check_size(buf, 1)) {
  76. buf->p[0] = val;
  77. buf->p++;
  78. }
  79. }
  80. static void buf_put_int16(struct cbuf *buf, u16 val)
  81. {
  82. if (buf_check_size(buf, 2)) {
  83. *(__le16 *) buf->p = cpu_to_le16(val);
  84. buf->p += 2;
  85. }
  86. }
  87. static void buf_put_int32(struct cbuf *buf, u32 val)
  88. {
  89. if (buf_check_size(buf, 4)) {
  90. *(__le32 *)buf->p = cpu_to_le32(val);
  91. buf->p += 4;
  92. }
  93. }
  94. static void buf_put_int64(struct cbuf *buf, u64 val)
  95. {
  96. if (buf_check_size(buf, 8)) {
  97. *(__le64 *)buf->p = cpu_to_le64(val);
  98. buf->p += 8;
  99. }
  100. }
  101. static char *buf_put_stringn(struct cbuf *buf, const char *s, u16 slen)
  102. {
  103. char *ret;
  104. ret = NULL;
  105. if (buf_check_size(buf, slen + 2)) {
  106. buf_put_int16(buf, slen);
  107. ret = buf->p;
  108. memcpy(buf->p, s, slen);
  109. buf->p += slen;
  110. }
  111. return ret;
  112. }
  113. static inline void buf_put_string(struct cbuf *buf, const char *s)
  114. {
  115. buf_put_stringn(buf, s, strlen(s));
  116. }
  117. static u8 buf_get_int8(struct cbuf *buf)
  118. {
  119. u8 ret = 0;
  120. if (buf_check_size(buf, 1)) {
  121. ret = buf->p[0];
  122. buf->p++;
  123. }
  124. return ret;
  125. }
  126. static u16 buf_get_int16(struct cbuf *buf)
  127. {
  128. u16 ret = 0;
  129. if (buf_check_size(buf, 2)) {
  130. ret = le16_to_cpu(*(__le16 *)buf->p);
  131. buf->p += 2;
  132. }
  133. return ret;
  134. }
  135. static u32 buf_get_int32(struct cbuf *buf)
  136. {
  137. u32 ret = 0;
  138. if (buf_check_size(buf, 4)) {
  139. ret = le32_to_cpu(*(__le32 *)buf->p);
  140. buf->p += 4;
  141. }
  142. return ret;
  143. }
  144. static u64 buf_get_int64(struct cbuf *buf)
  145. {
  146. u64 ret = 0;
  147. if (buf_check_size(buf, 8)) {
  148. ret = le64_to_cpu(*(__le64 *)buf->p);
  149. buf->p += 8;
  150. }
  151. return ret;
  152. }
  153. static void buf_get_str(struct cbuf *buf, struct p9_str *vstr)
  154. {
  155. vstr->len = buf_get_int16(buf);
  156. if (!buf_check_overflow(buf) && buf_check_size(buf, vstr->len)) {
  157. vstr->str = buf->p;
  158. buf->p += vstr->len;
  159. } else {
  160. vstr->len = 0;
  161. vstr->str = NULL;
  162. }
  163. }
  164. static void buf_get_qid(struct cbuf *bufp, struct p9_qid *qid)
  165. {
  166. qid->type = buf_get_int8(bufp);
  167. qid->version = buf_get_int32(bufp);
  168. qid->path = buf_get_int64(bufp);
  169. }
  170. /**
  171. * p9_size_wstat - calculate the size of a variable length stat struct
  172. * @stat: metadata (stat) structure
  173. * @dotu: non-zero if 9P2000.u
  174. *
  175. */
  176. static int p9_size_wstat(struct p9_wstat *wstat, int dotu)
  177. {
  178. int size = 0;
  179. if (wstat == NULL) {
  180. P9_EPRINTK(KERN_ERR, "p9_size_stat: got a NULL stat pointer\n");
  181. return 0;
  182. }
  183. size = /* 2 + *//* size[2] */
  184. 2 + /* type[2] */
  185. 4 + /* dev[4] */
  186. 1 + /* qid.type[1] */
  187. 4 + /* qid.vers[4] */
  188. 8 + /* qid.path[8] */
  189. 4 + /* mode[4] */
  190. 4 + /* atime[4] */
  191. 4 + /* mtime[4] */
  192. 8 + /* length[8] */
  193. 8; /* minimum sum of string lengths */
  194. if (wstat->name)
  195. size += strlen(wstat->name);
  196. if (wstat->uid)
  197. size += strlen(wstat->uid);
  198. if (wstat->gid)
  199. size += strlen(wstat->gid);
  200. if (wstat->muid)
  201. size += strlen(wstat->muid);
  202. if (dotu) {
  203. size += 4 + /* n_uid[4] */
  204. 4 + /* n_gid[4] */
  205. 4 + /* n_muid[4] */
  206. 2; /* string length of extension[4] */
  207. if (wstat->extension)
  208. size += strlen(wstat->extension);
  209. }
  210. return size;
  211. }
  212. /**
  213. * buf_get_stat - safely decode a recieved metadata (stat) structure
  214. * @bufp: buffer to deserialize
  215. * @stat: metadata (stat) structure
  216. * @dotu: non-zero if 9P2000.u
  217. *
  218. */
  219. static void
  220. buf_get_stat(struct cbuf *bufp, struct p9_stat *stat, int dotu)
  221. {
  222. stat->size = buf_get_int16(bufp);
  223. stat->type = buf_get_int16(bufp);
  224. stat->dev = buf_get_int32(bufp);
  225. stat->qid.type = buf_get_int8(bufp);
  226. stat->qid.version = buf_get_int32(bufp);
  227. stat->qid.path = buf_get_int64(bufp);
  228. stat->mode = buf_get_int32(bufp);
  229. stat->atime = buf_get_int32(bufp);
  230. stat->mtime = buf_get_int32(bufp);
  231. stat->length = buf_get_int64(bufp);
  232. buf_get_str(bufp, &stat->name);
  233. buf_get_str(bufp, &stat->uid);
  234. buf_get_str(bufp, &stat->gid);
  235. buf_get_str(bufp, &stat->muid);
  236. if (dotu) {
  237. buf_get_str(bufp, &stat->extension);
  238. stat->n_uid = buf_get_int32(bufp);
  239. stat->n_gid = buf_get_int32(bufp);
  240. stat->n_muid = buf_get_int32(bufp);
  241. }
  242. }
  243. /**
  244. * p9_deserialize_stat - decode a received metadata structure
  245. * @buf: buffer to deserialize
  246. * @buflen: length of received buffer
  247. * @stat: metadata structure to decode into
  248. * @dotu: non-zero if 9P2000.u
  249. *
  250. * Note: stat will point to the buf region.
  251. */
  252. int
  253. p9_deserialize_stat(void *buf, u32 buflen, struct p9_stat *stat,
  254. int dotu)
  255. {
  256. struct cbuf buffer;
  257. struct cbuf *bufp = &buffer;
  258. unsigned char *p;
  259. buf_init(bufp, buf, buflen);
  260. p = bufp->p;
  261. buf_get_stat(bufp, stat, dotu);
  262. if (buf_check_overflow(bufp))
  263. return 0;
  264. else
  265. return bufp->p - p;
  266. }
  267. EXPORT_SYMBOL(p9_deserialize_stat);
  268. /**
  269. * deserialize_fcall - unmarshal a response
  270. * @buf: recieved buffer
  271. * @buflen: length of received buffer
  272. * @rcall: fcall structure to populate
  273. * @rcalllen: length of fcall structure to populate
  274. * @dotu: non-zero if 9P2000.u
  275. *
  276. */
  277. int
  278. p9_deserialize_fcall(void *buf, u32 buflen, struct p9_fcall *rcall,
  279. int dotu)
  280. {
  281. struct cbuf buffer;
  282. struct cbuf *bufp = &buffer;
  283. int i = 0;
  284. buf_init(bufp, buf, buflen);
  285. rcall->size = buf_get_int32(bufp);
  286. rcall->id = buf_get_int8(bufp);
  287. rcall->tag = buf_get_int16(bufp);
  288. P9_DPRINTK(P9_DEBUG_CONV, "size %d id %d tag %d\n", rcall->size,
  289. rcall->id, rcall->tag);
  290. switch (rcall->id) {
  291. default:
  292. P9_EPRINTK(KERN_ERR, "unknown message type: %d\n", rcall->id);
  293. return -EPROTO;
  294. case P9_RVERSION:
  295. rcall->params.rversion.msize = buf_get_int32(bufp);
  296. buf_get_str(bufp, &rcall->params.rversion.version);
  297. break;
  298. case P9_RFLUSH:
  299. break;
  300. case P9_RATTACH:
  301. rcall->params.rattach.qid.type = buf_get_int8(bufp);
  302. rcall->params.rattach.qid.version = buf_get_int32(bufp);
  303. rcall->params.rattach.qid.path = buf_get_int64(bufp);
  304. break;
  305. case P9_RWALK:
  306. rcall->params.rwalk.nwqid = buf_get_int16(bufp);
  307. if (rcall->params.rwalk.nwqid > P9_MAXWELEM) {
  308. P9_EPRINTK(KERN_ERR,
  309. "Rwalk with more than %d qids: %d\n",
  310. P9_MAXWELEM, rcall->params.rwalk.nwqid);
  311. return -EPROTO;
  312. }
  313. for (i = 0; i < rcall->params.rwalk.nwqid; i++)
  314. buf_get_qid(bufp, &rcall->params.rwalk.wqids[i]);
  315. break;
  316. case P9_ROPEN:
  317. buf_get_qid(bufp, &rcall->params.ropen.qid);
  318. rcall->params.ropen.iounit = buf_get_int32(bufp);
  319. break;
  320. case P9_RCREATE:
  321. buf_get_qid(bufp, &rcall->params.rcreate.qid);
  322. rcall->params.rcreate.iounit = buf_get_int32(bufp);
  323. break;
  324. case P9_RREAD:
  325. rcall->params.rread.count = buf_get_int32(bufp);
  326. rcall->params.rread.data = bufp->p;
  327. buf_check_size(bufp, rcall->params.rread.count);
  328. break;
  329. case P9_RWRITE:
  330. rcall->params.rwrite.count = buf_get_int32(bufp);
  331. break;
  332. case P9_RCLUNK:
  333. break;
  334. case P9_RREMOVE:
  335. break;
  336. case P9_RSTAT:
  337. buf_get_int16(bufp);
  338. buf_get_stat(bufp, &rcall->params.rstat.stat, dotu);
  339. break;
  340. case P9_RWSTAT:
  341. break;
  342. case P9_RERROR:
  343. buf_get_str(bufp, &rcall->params.rerror.error);
  344. if (dotu)
  345. rcall->params.rerror.errno = buf_get_int16(bufp);
  346. break;
  347. }
  348. if (buf_check_overflow(bufp)) {
  349. P9_DPRINTK(P9_DEBUG_ERROR, "buffer overflow\n");
  350. return -EIO;
  351. }
  352. return bufp->p - bufp->sp;
  353. }
  354. EXPORT_SYMBOL(p9_deserialize_fcall);
  355. static inline void p9_put_int8(struct cbuf *bufp, u8 val, u8 * p)
  356. {
  357. *p = val;
  358. buf_put_int8(bufp, val);
  359. }
  360. static inline void p9_put_int16(struct cbuf *bufp, u16 val, u16 * p)
  361. {
  362. *p = val;
  363. buf_put_int16(bufp, val);
  364. }
  365. static inline void p9_put_int32(struct cbuf *bufp, u32 val, u32 * p)
  366. {
  367. *p = val;
  368. buf_put_int32(bufp, val);
  369. }
  370. static inline void p9_put_int64(struct cbuf *bufp, u64 val, u64 * p)
  371. {
  372. *p = val;
  373. buf_put_int64(bufp, val);
  374. }
  375. static void
  376. p9_put_str(struct cbuf *bufp, char *data, struct p9_str *str)
  377. {
  378. int len;
  379. char *s;
  380. if (data)
  381. len = strlen(data);
  382. else
  383. len = 0;
  384. s = buf_put_stringn(bufp, data, len);
  385. if (str) {
  386. str->len = len;
  387. str->str = s;
  388. }
  389. }
  390. static int
  391. p9_put_data(struct cbuf *bufp, const char *data, int count,
  392. unsigned char **pdata)
  393. {
  394. *pdata = buf_alloc(bufp, count);
  395. memmove(*pdata, data, count);
  396. return count;
  397. }
  398. static int
  399. p9_put_user_data(struct cbuf *bufp, const char __user *data, int count,
  400. unsigned char **pdata)
  401. {
  402. *pdata = buf_alloc(bufp, count);
  403. return copy_from_user(*pdata, data, count);
  404. }
  405. static void
  406. p9_put_wstat(struct cbuf *bufp, struct p9_wstat *wstat,
  407. struct p9_stat *stat, int statsz, int dotu)
  408. {
  409. p9_put_int16(bufp, statsz, &stat->size);
  410. p9_put_int16(bufp, wstat->type, &stat->type);
  411. p9_put_int32(bufp, wstat->dev, &stat->dev);
  412. p9_put_int8(bufp, wstat->qid.type, &stat->qid.type);
  413. p9_put_int32(bufp, wstat->qid.version, &stat->qid.version);
  414. p9_put_int64(bufp, wstat->qid.path, &stat->qid.path);
  415. p9_put_int32(bufp, wstat->mode, &stat->mode);
  416. p9_put_int32(bufp, wstat->atime, &stat->atime);
  417. p9_put_int32(bufp, wstat->mtime, &stat->mtime);
  418. p9_put_int64(bufp, wstat->length, &stat->length);
  419. p9_put_str(bufp, wstat->name, &stat->name);
  420. p9_put_str(bufp, wstat->uid, &stat->uid);
  421. p9_put_str(bufp, wstat->gid, &stat->gid);
  422. p9_put_str(bufp, wstat->muid, &stat->muid);
  423. if (dotu) {
  424. p9_put_str(bufp, wstat->extension, &stat->extension);
  425. p9_put_int32(bufp, wstat->n_uid, &stat->n_uid);
  426. p9_put_int32(bufp, wstat->n_gid, &stat->n_gid);
  427. p9_put_int32(bufp, wstat->n_muid, &stat->n_muid);
  428. }
  429. }
  430. static struct p9_fcall *
  431. p9_create_common(struct cbuf *bufp, u32 size, u8 id)
  432. {
  433. struct p9_fcall *fc;
  434. size += 4 + 1 + 2; /* size[4] id[1] tag[2] */
  435. fc = kmalloc(sizeof(struct p9_fcall) + size, GFP_KERNEL);
  436. if (!fc)
  437. return ERR_PTR(-ENOMEM);
  438. fc->sdata = (char *)fc + sizeof(*fc);
  439. buf_init(bufp, (char *)fc->sdata, size);
  440. p9_put_int32(bufp, size, &fc->size);
  441. p9_put_int8(bufp, id, &fc->id);
  442. p9_put_int16(bufp, P9_NOTAG, &fc->tag);
  443. return fc;
  444. }
  445. void p9_set_tag(struct p9_fcall *fc, u16 tag)
  446. {
  447. fc->tag = tag;
  448. *(__le16 *) (fc->sdata + 5) = cpu_to_le16(tag);
  449. }
  450. EXPORT_SYMBOL(p9_set_tag);
  451. struct p9_fcall *p9_create_tversion(u32 msize, char *version)
  452. {
  453. int size;
  454. struct p9_fcall *fc;
  455. struct cbuf buffer;
  456. struct cbuf *bufp = &buffer;
  457. size = 4 + 2 + strlen(version); /* msize[4] version[s] */
  458. fc = p9_create_common(bufp, size, P9_TVERSION);
  459. if (IS_ERR(fc))
  460. goto error;
  461. p9_put_int32(bufp, msize, &fc->params.tversion.msize);
  462. p9_put_str(bufp, version, &fc->params.tversion.version);
  463. if (buf_check_overflow(bufp)) {
  464. kfree(fc);
  465. fc = ERR_PTR(-ENOMEM);
  466. }
  467. error:
  468. return fc;
  469. }
  470. EXPORT_SYMBOL(p9_create_tversion);
  471. struct p9_fcall *p9_create_tauth(u32 afid, char *uname, char *aname)
  472. {
  473. int size;
  474. struct p9_fcall *fc;
  475. struct cbuf buffer;
  476. struct cbuf *bufp = &buffer;
  477. /* afid[4] uname[s] aname[s] */
  478. size = 4 + 2 + strlen(uname) + 2 + strlen(aname);
  479. fc = p9_create_common(bufp, size, P9_TAUTH);
  480. if (IS_ERR(fc))
  481. goto error;
  482. p9_put_int32(bufp, afid, &fc->params.tauth.afid);
  483. p9_put_str(bufp, uname, &fc->params.tauth.uname);
  484. p9_put_str(bufp, aname, &fc->params.tauth.aname);
  485. if (buf_check_overflow(bufp)) {
  486. kfree(fc);
  487. fc = ERR_PTR(-ENOMEM);
  488. }
  489. error:
  490. return fc;
  491. }
  492. EXPORT_SYMBOL(p9_create_tauth);
  493. struct p9_fcall *
  494. p9_create_tattach(u32 fid, u32 afid, char *uname, char *aname)
  495. {
  496. int size;
  497. struct p9_fcall *fc;
  498. struct cbuf buffer;
  499. struct cbuf *bufp = &buffer;
  500. /* fid[4] afid[4] uname[s] aname[s] */
  501. size = 4 + 4 + 2 + strlen(uname) + 2 + strlen(aname);
  502. fc = p9_create_common(bufp, size, P9_TATTACH);
  503. if (IS_ERR(fc))
  504. goto error;
  505. p9_put_int32(bufp, fid, &fc->params.tattach.fid);
  506. p9_put_int32(bufp, afid, &fc->params.tattach.afid);
  507. p9_put_str(bufp, uname, &fc->params.tattach.uname);
  508. p9_put_str(bufp, aname, &fc->params.tattach.aname);
  509. error:
  510. return fc;
  511. }
  512. EXPORT_SYMBOL(p9_create_tattach);
  513. struct p9_fcall *p9_create_tflush(u16 oldtag)
  514. {
  515. int size;
  516. struct p9_fcall *fc;
  517. struct cbuf buffer;
  518. struct cbuf *bufp = &buffer;
  519. size = 2; /* oldtag[2] */
  520. fc = p9_create_common(bufp, size, P9_TFLUSH);
  521. if (IS_ERR(fc))
  522. goto error;
  523. p9_put_int16(bufp, oldtag, &fc->params.tflush.oldtag);
  524. if (buf_check_overflow(bufp)) {
  525. kfree(fc);
  526. fc = ERR_PTR(-ENOMEM);
  527. }
  528. error:
  529. return fc;
  530. }
  531. EXPORT_SYMBOL(p9_create_tflush);
  532. struct p9_fcall *p9_create_twalk(u32 fid, u32 newfid, u16 nwname,
  533. char **wnames)
  534. {
  535. int i, size;
  536. struct p9_fcall *fc;
  537. struct cbuf buffer;
  538. struct cbuf *bufp = &buffer;
  539. if (nwname > P9_MAXWELEM) {
  540. P9_DPRINTK(P9_DEBUG_ERROR, "nwname > %d\n", P9_MAXWELEM);
  541. return NULL;
  542. }
  543. size = 4 + 4 + 2; /* fid[4] newfid[4] nwname[2] ... */
  544. for (i = 0; i < nwname; i++) {
  545. size += 2 + strlen(wnames[i]); /* wname[s] */
  546. }
  547. fc = p9_create_common(bufp, size, P9_TWALK);
  548. if (IS_ERR(fc))
  549. goto error;
  550. p9_put_int32(bufp, fid, &fc->params.twalk.fid);
  551. p9_put_int32(bufp, newfid, &fc->params.twalk.newfid);
  552. p9_put_int16(bufp, nwname, &fc->params.twalk.nwname);
  553. for (i = 0; i < nwname; i++) {
  554. p9_put_str(bufp, wnames[i], &fc->params.twalk.wnames[i]);
  555. }
  556. if (buf_check_overflow(bufp)) {
  557. kfree(fc);
  558. fc = ERR_PTR(-ENOMEM);
  559. }
  560. error:
  561. return fc;
  562. }
  563. EXPORT_SYMBOL(p9_create_twalk);
  564. struct p9_fcall *p9_create_topen(u32 fid, u8 mode)
  565. {
  566. int size;
  567. struct p9_fcall *fc;
  568. struct cbuf buffer;
  569. struct cbuf *bufp = &buffer;
  570. size = 4 + 1; /* fid[4] mode[1] */
  571. fc = p9_create_common(bufp, size, P9_TOPEN);
  572. if (IS_ERR(fc))
  573. goto error;
  574. p9_put_int32(bufp, fid, &fc->params.topen.fid);
  575. p9_put_int8(bufp, mode, &fc->params.topen.mode);
  576. if (buf_check_overflow(bufp)) {
  577. kfree(fc);
  578. fc = ERR_PTR(-ENOMEM);
  579. }
  580. error:
  581. return fc;
  582. }
  583. EXPORT_SYMBOL(p9_create_topen);
  584. struct p9_fcall *p9_create_tcreate(u32 fid, char *name, u32 perm, u8 mode,
  585. char *extension, int dotu)
  586. {
  587. int size;
  588. struct p9_fcall *fc;
  589. struct cbuf buffer;
  590. struct cbuf *bufp = &buffer;
  591. /* fid[4] name[s] perm[4] mode[1] */
  592. size = 4 + 2 + strlen(name) + 4 + 1;
  593. if (dotu) {
  594. size += 2 + /* extension[s] */
  595. (extension == NULL ? 0 : strlen(extension));
  596. }
  597. fc = p9_create_common(bufp, size, P9_TCREATE);
  598. if (IS_ERR(fc))
  599. goto error;
  600. p9_put_int32(bufp, fid, &fc->params.tcreate.fid);
  601. p9_put_str(bufp, name, &fc->params.tcreate.name);
  602. p9_put_int32(bufp, perm, &fc->params.tcreate.perm);
  603. p9_put_int8(bufp, mode, &fc->params.tcreate.mode);
  604. if (dotu)
  605. p9_put_str(bufp, extension, &fc->params.tcreate.extension);
  606. if (buf_check_overflow(bufp)) {
  607. kfree(fc);
  608. fc = ERR_PTR(-ENOMEM);
  609. }
  610. error:
  611. return fc;
  612. }
  613. EXPORT_SYMBOL(p9_create_tcreate);
  614. struct p9_fcall *p9_create_tread(u32 fid, u64 offset, u32 count)
  615. {
  616. int size;
  617. struct p9_fcall *fc;
  618. struct cbuf buffer;
  619. struct cbuf *bufp = &buffer;
  620. size = 4 + 8 + 4; /* fid[4] offset[8] count[4] */
  621. fc = p9_create_common(bufp, size, P9_TREAD);
  622. if (IS_ERR(fc))
  623. goto error;
  624. p9_put_int32(bufp, fid, &fc->params.tread.fid);
  625. p9_put_int64(bufp, offset, &fc->params.tread.offset);
  626. p9_put_int32(bufp, count, &fc->params.tread.count);
  627. if (buf_check_overflow(bufp)) {
  628. kfree(fc);
  629. fc = ERR_PTR(-ENOMEM);
  630. }
  631. error:
  632. return fc;
  633. }
  634. EXPORT_SYMBOL(p9_create_tread);
  635. struct p9_fcall *p9_create_twrite(u32 fid, u64 offset, u32 count,
  636. const char *data)
  637. {
  638. int size, err;
  639. struct p9_fcall *fc;
  640. struct cbuf buffer;
  641. struct cbuf *bufp = &buffer;
  642. /* fid[4] offset[8] count[4] data[count] */
  643. size = 4 + 8 + 4 + count;
  644. fc = p9_create_common(bufp, size, P9_TWRITE);
  645. if (IS_ERR(fc))
  646. goto error;
  647. p9_put_int32(bufp, fid, &fc->params.twrite.fid);
  648. p9_put_int64(bufp, offset, &fc->params.twrite.offset);
  649. p9_put_int32(bufp, count, &fc->params.twrite.count);
  650. err = p9_put_data(bufp, data, count, &fc->params.twrite.data);
  651. if (err) {
  652. kfree(fc);
  653. fc = ERR_PTR(err);
  654. goto error;
  655. }
  656. if (buf_check_overflow(bufp)) {
  657. kfree(fc);
  658. fc = ERR_PTR(-ENOMEM);
  659. }
  660. error:
  661. return fc;
  662. }
  663. EXPORT_SYMBOL(p9_create_twrite);
  664. struct p9_fcall *p9_create_twrite_u(u32 fid, u64 offset, u32 count,
  665. const char __user *data)
  666. {
  667. int size, err;
  668. struct p9_fcall *fc;
  669. struct cbuf buffer;
  670. struct cbuf *bufp = &buffer;
  671. /* fid[4] offset[8] count[4] data[count] */
  672. size = 4 + 8 + 4 + count;
  673. fc = p9_create_common(bufp, size, P9_TWRITE);
  674. if (IS_ERR(fc))
  675. goto error;
  676. p9_put_int32(bufp, fid, &fc->params.twrite.fid);
  677. p9_put_int64(bufp, offset, &fc->params.twrite.offset);
  678. p9_put_int32(bufp, count, &fc->params.twrite.count);
  679. err = p9_put_user_data(bufp, data, count, &fc->params.twrite.data);
  680. if (err) {
  681. kfree(fc);
  682. fc = ERR_PTR(err);
  683. goto error;
  684. }
  685. if (buf_check_overflow(bufp)) {
  686. kfree(fc);
  687. fc = ERR_PTR(-ENOMEM);
  688. }
  689. error:
  690. return fc;
  691. }
  692. EXPORT_SYMBOL(p9_create_twrite_u);
  693. struct p9_fcall *p9_create_tclunk(u32 fid)
  694. {
  695. int size;
  696. struct p9_fcall *fc;
  697. struct cbuf buffer;
  698. struct cbuf *bufp = &buffer;
  699. size = 4; /* fid[4] */
  700. fc = p9_create_common(bufp, size, P9_TCLUNK);
  701. if (IS_ERR(fc))
  702. goto error;
  703. p9_put_int32(bufp, fid, &fc->params.tclunk.fid);
  704. if (buf_check_overflow(bufp)) {
  705. kfree(fc);
  706. fc = ERR_PTR(-ENOMEM);
  707. }
  708. error:
  709. return fc;
  710. }
  711. EXPORT_SYMBOL(p9_create_tclunk);
  712. struct p9_fcall *p9_create_tremove(u32 fid)
  713. {
  714. int size;
  715. struct p9_fcall *fc;
  716. struct cbuf buffer;
  717. struct cbuf *bufp = &buffer;
  718. size = 4; /* fid[4] */
  719. fc = p9_create_common(bufp, size, P9_TREMOVE);
  720. if (IS_ERR(fc))
  721. goto error;
  722. p9_put_int32(bufp, fid, &fc->params.tremove.fid);
  723. if (buf_check_overflow(bufp)) {
  724. kfree(fc);
  725. fc = ERR_PTR(-ENOMEM);
  726. }
  727. error:
  728. return fc;
  729. }
  730. EXPORT_SYMBOL(p9_create_tremove);
  731. struct p9_fcall *p9_create_tstat(u32 fid)
  732. {
  733. int size;
  734. struct p9_fcall *fc;
  735. struct cbuf buffer;
  736. struct cbuf *bufp = &buffer;
  737. size = 4; /* fid[4] */
  738. fc = p9_create_common(bufp, size, P9_TSTAT);
  739. if (IS_ERR(fc))
  740. goto error;
  741. p9_put_int32(bufp, fid, &fc->params.tstat.fid);
  742. if (buf_check_overflow(bufp)) {
  743. kfree(fc);
  744. fc = ERR_PTR(-ENOMEM);
  745. }
  746. error:
  747. return fc;
  748. }
  749. EXPORT_SYMBOL(p9_create_tstat);
  750. struct p9_fcall *p9_create_twstat(u32 fid, struct p9_wstat *wstat,
  751. int dotu)
  752. {
  753. int size, statsz;
  754. struct p9_fcall *fc;
  755. struct cbuf buffer;
  756. struct cbuf *bufp = &buffer;
  757. statsz = p9_size_wstat(wstat, dotu);
  758. size = 4 + 2 + 2 + statsz; /* fid[4] stat[n] */
  759. fc = p9_create_common(bufp, size, P9_TWSTAT);
  760. if (IS_ERR(fc))
  761. goto error;
  762. p9_put_int32(bufp, fid, &fc->params.twstat.fid);
  763. buf_put_int16(bufp, statsz + 2);
  764. p9_put_wstat(bufp, wstat, &fc->params.twstat.stat, statsz, dotu);
  765. if (buf_check_overflow(bufp)) {
  766. kfree(fc);
  767. fc = ERR_PTR(-ENOMEM);
  768. }
  769. error:
  770. return fc;
  771. }
  772. EXPORT_SYMBOL(p9_create_twstat);