conv.c 20 KB

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