conv.c 18 KB

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