conv.c 18 KB

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