conv.c 18 KB

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