conv.c 19 KB

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