conv.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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. u32 n_uname, int dotu)
  473. {
  474. int size;
  475. struct p9_fcall *fc;
  476. struct cbuf buffer;
  477. struct cbuf *bufp = &buffer;
  478. /* afid[4] uname[s] aname[s] */
  479. size = 4 + 2 + 2;
  480. if (uname)
  481. size += strlen(uname);
  482. if (aname)
  483. size += strlen(aname);
  484. if (dotu)
  485. size += 4; /* n_uname */
  486. fc = p9_create_common(bufp, size, P9_TAUTH);
  487. if (IS_ERR(fc))
  488. goto error;
  489. p9_put_int32(bufp, afid, &fc->params.tauth.afid);
  490. p9_put_str(bufp, uname, &fc->params.tauth.uname);
  491. p9_put_str(bufp, aname, &fc->params.tauth.aname);
  492. if (dotu)
  493. p9_put_int32(bufp, n_uname, &fc->params.tauth.n_uname);
  494. if (buf_check_overflow(bufp)) {
  495. kfree(fc);
  496. fc = ERR_PTR(-ENOMEM);
  497. }
  498. error:
  499. return fc;
  500. }
  501. EXPORT_SYMBOL(p9_create_tauth);
  502. struct p9_fcall *
  503. p9_create_tattach(u32 fid, u32 afid, char *uname, char *aname,
  504. u32 n_uname, int dotu)
  505. {
  506. int size;
  507. struct p9_fcall *fc;
  508. struct cbuf buffer;
  509. struct cbuf *bufp = &buffer;
  510. /* fid[4] afid[4] uname[s] aname[s] */
  511. size = 4 + 4 + 2 + 2;
  512. if (uname)
  513. size += strlen(uname);
  514. if (aname)
  515. size += strlen(aname);
  516. if (dotu)
  517. size += 4; /* n_uname */
  518. fc = p9_create_common(bufp, size, P9_TATTACH);
  519. if (IS_ERR(fc))
  520. goto error;
  521. p9_put_int32(bufp, fid, &fc->params.tattach.fid);
  522. p9_put_int32(bufp, afid, &fc->params.tattach.afid);
  523. p9_put_str(bufp, uname, &fc->params.tattach.uname);
  524. p9_put_str(bufp, aname, &fc->params.tattach.aname);
  525. if (dotu)
  526. p9_put_int32(bufp, n_uname, &fc->params.tattach.n_uname);
  527. error:
  528. return fc;
  529. }
  530. EXPORT_SYMBOL(p9_create_tattach);
  531. struct p9_fcall *p9_create_tflush(u16 oldtag)
  532. {
  533. int size;
  534. struct p9_fcall *fc;
  535. struct cbuf buffer;
  536. struct cbuf *bufp = &buffer;
  537. size = 2; /* oldtag[2] */
  538. fc = p9_create_common(bufp, size, P9_TFLUSH);
  539. if (IS_ERR(fc))
  540. goto error;
  541. p9_put_int16(bufp, oldtag, &fc->params.tflush.oldtag);
  542. if (buf_check_overflow(bufp)) {
  543. kfree(fc);
  544. fc = ERR_PTR(-ENOMEM);
  545. }
  546. error:
  547. return fc;
  548. }
  549. EXPORT_SYMBOL(p9_create_tflush);
  550. struct p9_fcall *p9_create_twalk(u32 fid, u32 newfid, u16 nwname,
  551. char **wnames)
  552. {
  553. int i, size;
  554. struct p9_fcall *fc;
  555. struct cbuf buffer;
  556. struct cbuf *bufp = &buffer;
  557. if (nwname > P9_MAXWELEM) {
  558. P9_DPRINTK(P9_DEBUG_ERROR, "nwname > %d\n", P9_MAXWELEM);
  559. return NULL;
  560. }
  561. size = 4 + 4 + 2; /* fid[4] newfid[4] nwname[2] ... */
  562. for (i = 0; i < nwname; i++) {
  563. size += 2 + strlen(wnames[i]); /* wname[s] */
  564. }
  565. fc = p9_create_common(bufp, size, P9_TWALK);
  566. if (IS_ERR(fc))
  567. goto error;
  568. p9_put_int32(bufp, fid, &fc->params.twalk.fid);
  569. p9_put_int32(bufp, newfid, &fc->params.twalk.newfid);
  570. p9_put_int16(bufp, nwname, &fc->params.twalk.nwname);
  571. for (i = 0; i < nwname; i++) {
  572. p9_put_str(bufp, wnames[i], &fc->params.twalk.wnames[i]);
  573. }
  574. if (buf_check_overflow(bufp)) {
  575. kfree(fc);
  576. fc = ERR_PTR(-ENOMEM);
  577. }
  578. error:
  579. return fc;
  580. }
  581. EXPORT_SYMBOL(p9_create_twalk);
  582. struct p9_fcall *p9_create_topen(u32 fid, u8 mode)
  583. {
  584. int size;
  585. struct p9_fcall *fc;
  586. struct cbuf buffer;
  587. struct cbuf *bufp = &buffer;
  588. size = 4 + 1; /* fid[4] mode[1] */
  589. fc = p9_create_common(bufp, size, P9_TOPEN);
  590. if (IS_ERR(fc))
  591. goto error;
  592. p9_put_int32(bufp, fid, &fc->params.topen.fid);
  593. p9_put_int8(bufp, mode, &fc->params.topen.mode);
  594. if (buf_check_overflow(bufp)) {
  595. kfree(fc);
  596. fc = ERR_PTR(-ENOMEM);
  597. }
  598. error:
  599. return fc;
  600. }
  601. EXPORT_SYMBOL(p9_create_topen);
  602. struct p9_fcall *p9_create_tcreate(u32 fid, char *name, u32 perm, u8 mode,
  603. char *extension, int dotu)
  604. {
  605. int size;
  606. struct p9_fcall *fc;
  607. struct cbuf buffer;
  608. struct cbuf *bufp = &buffer;
  609. /* fid[4] name[s] perm[4] mode[1] */
  610. size = 4 + 2 + strlen(name) + 4 + 1;
  611. if (dotu) {
  612. size += 2 + /* extension[s] */
  613. (extension == NULL ? 0 : strlen(extension));
  614. }
  615. fc = p9_create_common(bufp, size, P9_TCREATE);
  616. if (IS_ERR(fc))
  617. goto error;
  618. p9_put_int32(bufp, fid, &fc->params.tcreate.fid);
  619. p9_put_str(bufp, name, &fc->params.tcreate.name);
  620. p9_put_int32(bufp, perm, &fc->params.tcreate.perm);
  621. p9_put_int8(bufp, mode, &fc->params.tcreate.mode);
  622. if (dotu)
  623. p9_put_str(bufp, extension, &fc->params.tcreate.extension);
  624. if (buf_check_overflow(bufp)) {
  625. kfree(fc);
  626. fc = ERR_PTR(-ENOMEM);
  627. }
  628. error:
  629. return fc;
  630. }
  631. EXPORT_SYMBOL(p9_create_tcreate);
  632. struct p9_fcall *p9_create_tread(u32 fid, u64 offset, u32 count)
  633. {
  634. int size;
  635. struct p9_fcall *fc;
  636. struct cbuf buffer;
  637. struct cbuf *bufp = &buffer;
  638. size = 4 + 8 + 4; /* fid[4] offset[8] count[4] */
  639. fc = p9_create_common(bufp, size, P9_TREAD);
  640. if (IS_ERR(fc))
  641. goto error;
  642. p9_put_int32(bufp, fid, &fc->params.tread.fid);
  643. p9_put_int64(bufp, offset, &fc->params.tread.offset);
  644. p9_put_int32(bufp, count, &fc->params.tread.count);
  645. if (buf_check_overflow(bufp)) {
  646. kfree(fc);
  647. fc = ERR_PTR(-ENOMEM);
  648. }
  649. error:
  650. return fc;
  651. }
  652. EXPORT_SYMBOL(p9_create_tread);
  653. struct p9_fcall *p9_create_twrite(u32 fid, u64 offset, u32 count,
  654. const char *data)
  655. {
  656. int size, err;
  657. struct p9_fcall *fc;
  658. struct cbuf buffer;
  659. struct cbuf *bufp = &buffer;
  660. /* fid[4] offset[8] count[4] data[count] */
  661. size = 4 + 8 + 4 + count;
  662. fc = p9_create_common(bufp, size, P9_TWRITE);
  663. if (IS_ERR(fc))
  664. goto error;
  665. p9_put_int32(bufp, fid, &fc->params.twrite.fid);
  666. p9_put_int64(bufp, offset, &fc->params.twrite.offset);
  667. p9_put_int32(bufp, count, &fc->params.twrite.count);
  668. err = p9_put_data(bufp, data, count, &fc->params.twrite.data);
  669. if (err) {
  670. kfree(fc);
  671. fc = ERR_PTR(err);
  672. goto error;
  673. }
  674. if (buf_check_overflow(bufp)) {
  675. kfree(fc);
  676. fc = ERR_PTR(-ENOMEM);
  677. }
  678. error:
  679. return fc;
  680. }
  681. EXPORT_SYMBOL(p9_create_twrite);
  682. struct p9_fcall *p9_create_twrite_u(u32 fid, u64 offset, u32 count,
  683. const char __user *data)
  684. {
  685. int size, err;
  686. struct p9_fcall *fc;
  687. struct cbuf buffer;
  688. struct cbuf *bufp = &buffer;
  689. /* fid[4] offset[8] count[4] data[count] */
  690. size = 4 + 8 + 4 + count;
  691. fc = p9_create_common(bufp, size, P9_TWRITE);
  692. if (IS_ERR(fc))
  693. goto error;
  694. p9_put_int32(bufp, fid, &fc->params.twrite.fid);
  695. p9_put_int64(bufp, offset, &fc->params.twrite.offset);
  696. p9_put_int32(bufp, count, &fc->params.twrite.count);
  697. err = p9_put_user_data(bufp, data, count, &fc->params.twrite.data);
  698. if (err) {
  699. kfree(fc);
  700. fc = ERR_PTR(err);
  701. goto error;
  702. }
  703. if (buf_check_overflow(bufp)) {
  704. kfree(fc);
  705. fc = ERR_PTR(-ENOMEM);
  706. }
  707. error:
  708. return fc;
  709. }
  710. EXPORT_SYMBOL(p9_create_twrite_u);
  711. struct p9_fcall *p9_create_tclunk(u32 fid)
  712. {
  713. int size;
  714. struct p9_fcall *fc;
  715. struct cbuf buffer;
  716. struct cbuf *bufp = &buffer;
  717. size = 4; /* fid[4] */
  718. fc = p9_create_common(bufp, size, P9_TCLUNK);
  719. if (IS_ERR(fc))
  720. goto error;
  721. p9_put_int32(bufp, fid, &fc->params.tclunk.fid);
  722. if (buf_check_overflow(bufp)) {
  723. kfree(fc);
  724. fc = ERR_PTR(-ENOMEM);
  725. }
  726. error:
  727. return fc;
  728. }
  729. EXPORT_SYMBOL(p9_create_tclunk);
  730. struct p9_fcall *p9_create_tremove(u32 fid)
  731. {
  732. int size;
  733. struct p9_fcall *fc;
  734. struct cbuf buffer;
  735. struct cbuf *bufp = &buffer;
  736. size = 4; /* fid[4] */
  737. fc = p9_create_common(bufp, size, P9_TREMOVE);
  738. if (IS_ERR(fc))
  739. goto error;
  740. p9_put_int32(bufp, fid, &fc->params.tremove.fid);
  741. if (buf_check_overflow(bufp)) {
  742. kfree(fc);
  743. fc = ERR_PTR(-ENOMEM);
  744. }
  745. error:
  746. return fc;
  747. }
  748. EXPORT_SYMBOL(p9_create_tremove);
  749. struct p9_fcall *p9_create_tstat(u32 fid)
  750. {
  751. int size;
  752. struct p9_fcall *fc;
  753. struct cbuf buffer;
  754. struct cbuf *bufp = &buffer;
  755. size = 4; /* fid[4] */
  756. fc = p9_create_common(bufp, size, P9_TSTAT);
  757. if (IS_ERR(fc))
  758. goto error;
  759. p9_put_int32(bufp, fid, &fc->params.tstat.fid);
  760. if (buf_check_overflow(bufp)) {
  761. kfree(fc);
  762. fc = ERR_PTR(-ENOMEM);
  763. }
  764. error:
  765. return fc;
  766. }
  767. EXPORT_SYMBOL(p9_create_tstat);
  768. struct p9_fcall *p9_create_twstat(u32 fid, struct p9_wstat *wstat,
  769. int dotu)
  770. {
  771. int size, statsz;
  772. struct p9_fcall *fc;
  773. struct cbuf buffer;
  774. struct cbuf *bufp = &buffer;
  775. statsz = p9_size_wstat(wstat, dotu);
  776. size = 4 + 2 + 2 + statsz; /* fid[4] stat[n] */
  777. fc = p9_create_common(bufp, size, P9_TWSTAT);
  778. if (IS_ERR(fc))
  779. goto error;
  780. p9_put_int32(bufp, fid, &fc->params.twstat.fid);
  781. buf_put_int16(bufp, statsz + 2);
  782. p9_put_wstat(bufp, wstat, &fc->params.twstat.stat, statsz, dotu);
  783. if (buf_check_overflow(bufp)) {
  784. kfree(fc);
  785. fc = ERR_PTR(-ENOMEM);
  786. }
  787. error:
  788. return fc;
  789. }
  790. EXPORT_SYMBOL(p9_create_twstat);