conv.c 18 KB

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