conv.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. /*
  2. * net/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 <linux/uaccess.h>
  32. #include <net/9p/9p.h>
  33. /*
  34. * Buffer to help with string parsing
  35. */
  36. struct cbuf {
  37. unsigned char *sp;
  38. unsigned char *p;
  39. unsigned char *ep;
  40. };
  41. static inline void buf_init(struct cbuf *buf, void *data, int datalen)
  42. {
  43. buf->sp = buf->p = data;
  44. buf->ep = data + datalen;
  45. }
  46. static inline int buf_check_overflow(struct cbuf *buf)
  47. {
  48. return buf->p > buf->ep;
  49. }
  50. static int buf_check_size(struct cbuf *buf, int len)
  51. {
  52. if (buf->p + len > buf->ep) {
  53. if (buf->p < buf->ep) {
  54. P9_EPRINTK(KERN_ERR,
  55. "buffer overflow: want %d has %d\n", len,
  56. (int)(buf->ep - buf->p));
  57. dump_stack();
  58. buf->p = buf->ep + 1;
  59. }
  60. return 0;
  61. }
  62. return 1;
  63. }
  64. static void *buf_alloc(struct cbuf *buf, int len)
  65. {
  66. void *ret = NULL;
  67. if (buf_check_size(buf, len)) {
  68. ret = buf->p;
  69. buf->p += len;
  70. }
  71. return ret;
  72. }
  73. static void buf_put_int8(struct cbuf *buf, u8 val)
  74. {
  75. if (buf_check_size(buf, 1)) {
  76. buf->p[0] = val;
  77. buf->p++;
  78. }
  79. }
  80. static void buf_put_int16(struct cbuf *buf, u16 val)
  81. {
  82. if (buf_check_size(buf, 2)) {
  83. *(__le16 *) buf->p = cpu_to_le16(val);
  84. buf->p += 2;
  85. }
  86. }
  87. static void buf_put_int32(struct cbuf *buf, u32 val)
  88. {
  89. if (buf_check_size(buf, 4)) {
  90. *(__le32 *)buf->p = cpu_to_le32(val);
  91. buf->p += 4;
  92. }
  93. }
  94. static void buf_put_int64(struct cbuf *buf, u64 val)
  95. {
  96. if (buf_check_size(buf, 8)) {
  97. *(__le64 *)buf->p = cpu_to_le64(val);
  98. buf->p += 8;
  99. }
  100. }
  101. static char *buf_put_stringn(struct cbuf *buf, const char *s, u16 slen)
  102. {
  103. char *ret;
  104. ret = NULL;
  105. if (buf_check_size(buf, slen + 2)) {
  106. buf_put_int16(buf, slen);
  107. ret = buf->p;
  108. memcpy(buf->p, s, slen);
  109. buf->p += slen;
  110. }
  111. return ret;
  112. }
  113. static u8 buf_get_int8(struct cbuf *buf)
  114. {
  115. u8 ret = 0;
  116. if (buf_check_size(buf, 1)) {
  117. ret = buf->p[0];
  118. buf->p++;
  119. }
  120. return ret;
  121. }
  122. static u16 buf_get_int16(struct cbuf *buf)
  123. {
  124. u16 ret = 0;
  125. if (buf_check_size(buf, 2)) {
  126. ret = le16_to_cpu(*(__le16 *)buf->p);
  127. buf->p += 2;
  128. }
  129. return ret;
  130. }
  131. static u32 buf_get_int32(struct cbuf *buf)
  132. {
  133. u32 ret = 0;
  134. if (buf_check_size(buf, 4)) {
  135. ret = le32_to_cpu(*(__le32 *)buf->p);
  136. buf->p += 4;
  137. }
  138. return ret;
  139. }
  140. static u64 buf_get_int64(struct cbuf *buf)
  141. {
  142. u64 ret = 0;
  143. if (buf_check_size(buf, 8)) {
  144. ret = le64_to_cpu(*(__le64 *)buf->p);
  145. buf->p += 8;
  146. }
  147. return ret;
  148. }
  149. static void buf_get_str(struct cbuf *buf, struct p9_str *vstr)
  150. {
  151. vstr->len = buf_get_int16(buf);
  152. if (!buf_check_overflow(buf) && buf_check_size(buf, vstr->len)) {
  153. vstr->str = buf->p;
  154. buf->p += vstr->len;
  155. } else {
  156. vstr->len = 0;
  157. vstr->str = NULL;
  158. }
  159. }
  160. static void buf_get_qid(struct cbuf *bufp, struct p9_qid *qid)
  161. {
  162. qid->type = buf_get_int8(bufp);
  163. qid->version = buf_get_int32(bufp);
  164. qid->path = buf_get_int64(bufp);
  165. }
  166. /**
  167. * p9_size_wstat - calculate the size of a variable length stat struct
  168. * @wstat: metadata (stat) structure
  169. * @dotu: non-zero if 9P2000.u
  170. *
  171. */
  172. static int p9_size_wstat(struct p9_wstat *wstat, int dotu)
  173. {
  174. int size = 0;
  175. if (wstat == NULL) {
  176. P9_EPRINTK(KERN_ERR, "p9_size_stat: got a NULL stat pointer\n");
  177. return 0;
  178. }
  179. size = /* 2 + *//* size[2] */
  180. 2 + /* type[2] */
  181. 4 + /* dev[4] */
  182. 1 + /* qid.type[1] */
  183. 4 + /* qid.vers[4] */
  184. 8 + /* qid.path[8] */
  185. 4 + /* mode[4] */
  186. 4 + /* atime[4] */
  187. 4 + /* mtime[4] */
  188. 8 + /* length[8] */
  189. 8; /* minimum sum of string lengths */
  190. if (wstat->name)
  191. size += strlen(wstat->name);
  192. if (wstat->uid)
  193. size += strlen(wstat->uid);
  194. if (wstat->gid)
  195. size += strlen(wstat->gid);
  196. if (wstat->muid)
  197. size += strlen(wstat->muid);
  198. if (dotu) {
  199. size += 4 + /* n_uid[4] */
  200. 4 + /* n_gid[4] */
  201. 4 + /* n_muid[4] */
  202. 2; /* string length of extension[4] */
  203. if (wstat->extension)
  204. size += strlen(wstat->extension);
  205. }
  206. return size;
  207. }
  208. /**
  209. * buf_get_stat - safely decode a recieved metadata (stat) structure
  210. * @bufp: buffer to deserialize
  211. * @stat: metadata (stat) structure
  212. * @dotu: non-zero if 9P2000.u
  213. *
  214. */
  215. static void
  216. buf_get_stat(struct cbuf *bufp, struct p9_stat *stat, int dotu)
  217. {
  218. stat->size = buf_get_int16(bufp);
  219. stat->type = buf_get_int16(bufp);
  220. stat->dev = buf_get_int32(bufp);
  221. stat->qid.type = buf_get_int8(bufp);
  222. stat->qid.version = buf_get_int32(bufp);
  223. stat->qid.path = buf_get_int64(bufp);
  224. stat->mode = buf_get_int32(bufp);
  225. stat->atime = buf_get_int32(bufp);
  226. stat->mtime = buf_get_int32(bufp);
  227. stat->length = buf_get_int64(bufp);
  228. buf_get_str(bufp, &stat->name);
  229. buf_get_str(bufp, &stat->uid);
  230. buf_get_str(bufp, &stat->gid);
  231. buf_get_str(bufp, &stat->muid);
  232. if (dotu) {
  233. buf_get_str(bufp, &stat->extension);
  234. stat->n_uid = buf_get_int32(bufp);
  235. stat->n_gid = buf_get_int32(bufp);
  236. stat->n_muid = buf_get_int32(bufp);
  237. }
  238. }
  239. /**
  240. * p9_deserialize_stat - decode a received metadata structure
  241. * @buf: buffer to deserialize
  242. * @buflen: length of received buffer
  243. * @stat: metadata structure to decode into
  244. * @dotu: non-zero if 9P2000.u
  245. *
  246. * Note: stat will point to the buf region.
  247. */
  248. int
  249. p9_deserialize_stat(void *buf, u32 buflen, struct p9_stat *stat,
  250. int dotu)
  251. {
  252. struct cbuf buffer;
  253. struct cbuf *bufp = &buffer;
  254. unsigned char *p;
  255. buf_init(bufp, buf, buflen);
  256. p = bufp->p;
  257. buf_get_stat(bufp, stat, dotu);
  258. if (buf_check_overflow(bufp))
  259. return 0;
  260. else
  261. return bufp->p - p;
  262. }
  263. EXPORT_SYMBOL(p9_deserialize_stat);
  264. /**
  265. * deserialize_fcall - unmarshal a response
  266. * @buf: recieved buffer
  267. * @buflen: length of received buffer
  268. * @rcall: fcall structure to populate
  269. * @rcalllen: length of fcall structure to populate
  270. * @dotu: non-zero if 9P2000.u
  271. *
  272. */
  273. int
  274. p9_deserialize_fcall(void *buf, u32 buflen, struct p9_fcall *rcall,
  275. int dotu)
  276. {
  277. struct cbuf buffer;
  278. struct cbuf *bufp = &buffer;
  279. int i = 0;
  280. buf_init(bufp, buf, buflen);
  281. rcall->size = buf_get_int32(bufp);
  282. rcall->id = buf_get_int8(bufp);
  283. rcall->tag = buf_get_int16(bufp);
  284. P9_DPRINTK(P9_DEBUG_CONV, "size %d id %d tag %d\n", rcall->size,
  285. rcall->id, rcall->tag);
  286. switch (rcall->id) {
  287. default:
  288. P9_EPRINTK(KERN_ERR, "unknown message type: %d\n", rcall->id);
  289. return -EPROTO;
  290. case P9_RVERSION:
  291. rcall->params.rversion.msize = buf_get_int32(bufp);
  292. buf_get_str(bufp, &rcall->params.rversion.version);
  293. break;
  294. case P9_RFLUSH:
  295. break;
  296. case P9_RATTACH:
  297. rcall->params.rattach.qid.type = buf_get_int8(bufp);
  298. rcall->params.rattach.qid.version = buf_get_int32(bufp);
  299. rcall->params.rattach.qid.path = buf_get_int64(bufp);
  300. break;
  301. case P9_RWALK:
  302. rcall->params.rwalk.nwqid = buf_get_int16(bufp);
  303. if (rcall->params.rwalk.nwqid > P9_MAXWELEM) {
  304. P9_EPRINTK(KERN_ERR,
  305. "Rwalk with more than %d qids: %d\n",
  306. P9_MAXWELEM, rcall->params.rwalk.nwqid);
  307. return -EPROTO;
  308. }
  309. for (i = 0; i < rcall->params.rwalk.nwqid; i++)
  310. buf_get_qid(bufp, &rcall->params.rwalk.wqids[i]);
  311. break;
  312. case P9_ROPEN:
  313. buf_get_qid(bufp, &rcall->params.ropen.qid);
  314. rcall->params.ropen.iounit = buf_get_int32(bufp);
  315. break;
  316. case P9_RCREATE:
  317. buf_get_qid(bufp, &rcall->params.rcreate.qid);
  318. rcall->params.rcreate.iounit = buf_get_int32(bufp);
  319. break;
  320. case P9_RREAD:
  321. rcall->params.rread.count = buf_get_int32(bufp);
  322. rcall->params.rread.data = bufp->p;
  323. buf_check_size(bufp, rcall->params.rread.count);
  324. break;
  325. case P9_RWRITE:
  326. rcall->params.rwrite.count = buf_get_int32(bufp);
  327. break;
  328. case P9_RCLUNK:
  329. break;
  330. case P9_RREMOVE:
  331. break;
  332. case P9_RSTAT:
  333. buf_get_int16(bufp);
  334. buf_get_stat(bufp, &rcall->params.rstat.stat, dotu);
  335. break;
  336. case P9_RWSTAT:
  337. break;
  338. case P9_RERROR:
  339. buf_get_str(bufp, &rcall->params.rerror.error);
  340. if (dotu)
  341. rcall->params.rerror.errno = buf_get_int16(bufp);
  342. break;
  343. }
  344. if (buf_check_overflow(bufp)) {
  345. P9_DPRINTK(P9_DEBUG_ERROR, "buffer overflow\n");
  346. return -EIO;
  347. }
  348. return bufp->p - bufp->sp;
  349. }
  350. EXPORT_SYMBOL(p9_deserialize_fcall);
  351. static inline void p9_put_int8(struct cbuf *bufp, u8 val, u8 * p)
  352. {
  353. *p = val;
  354. buf_put_int8(bufp, val);
  355. }
  356. static inline void p9_put_int16(struct cbuf *bufp, u16 val, u16 * p)
  357. {
  358. *p = val;
  359. buf_put_int16(bufp, val);
  360. }
  361. static inline void p9_put_int32(struct cbuf *bufp, u32 val, u32 * p)
  362. {
  363. *p = val;
  364. buf_put_int32(bufp, val);
  365. }
  366. static inline void p9_put_int64(struct cbuf *bufp, u64 val, u64 * p)
  367. {
  368. *p = val;
  369. buf_put_int64(bufp, val);
  370. }
  371. static void
  372. p9_put_str(struct cbuf *bufp, char *data, struct p9_str *str)
  373. {
  374. int len;
  375. char *s;
  376. if (data)
  377. len = strlen(data);
  378. else
  379. len = 0;
  380. s = buf_put_stringn(bufp, data, len);
  381. if (str) {
  382. str->len = len;
  383. str->str = s;
  384. }
  385. }
  386. static int
  387. p9_put_data(struct cbuf *bufp, const char *data, int count,
  388. unsigned char **pdata)
  389. {
  390. *pdata = buf_alloc(bufp, count);
  391. if (*pdata == NULL)
  392. return -ENOMEM;
  393. memmove(*pdata, data, count);
  394. return 0;
  395. }
  396. static int
  397. p9_put_user_data(struct cbuf *bufp, const char __user *data, int count,
  398. unsigned char **pdata)
  399. {
  400. *pdata = buf_alloc(bufp, count);
  401. if (*pdata == NULL)
  402. return -ENOMEM;
  403. return copy_from_user(*pdata, data, count);
  404. }
  405. static void
  406. p9_put_wstat(struct cbuf *bufp, struct p9_wstat *wstat,
  407. struct p9_stat *stat, int statsz, int dotu)
  408. {
  409. p9_put_int16(bufp, statsz, &stat->size);
  410. p9_put_int16(bufp, wstat->type, &stat->type);
  411. p9_put_int32(bufp, wstat->dev, &stat->dev);
  412. p9_put_int8(bufp, wstat->qid.type, &stat->qid.type);
  413. p9_put_int32(bufp, wstat->qid.version, &stat->qid.version);
  414. p9_put_int64(bufp, wstat->qid.path, &stat->qid.path);
  415. p9_put_int32(bufp, wstat->mode, &stat->mode);
  416. p9_put_int32(bufp, wstat->atime, &stat->atime);
  417. p9_put_int32(bufp, wstat->mtime, &stat->mtime);
  418. p9_put_int64(bufp, wstat->length, &stat->length);
  419. p9_put_str(bufp, wstat->name, &stat->name);
  420. p9_put_str(bufp, wstat->uid, &stat->uid);
  421. p9_put_str(bufp, wstat->gid, &stat->gid);
  422. p9_put_str(bufp, wstat->muid, &stat->muid);
  423. if (dotu) {
  424. p9_put_str(bufp, wstat->extension, &stat->extension);
  425. p9_put_int32(bufp, wstat->n_uid, &stat->n_uid);
  426. p9_put_int32(bufp, wstat->n_gid, &stat->n_gid);
  427. p9_put_int32(bufp, wstat->n_muid, &stat->n_muid);
  428. }
  429. }
  430. static struct p9_fcall *
  431. p9_create_common(struct cbuf *bufp, u32 size, u8 id)
  432. {
  433. struct p9_fcall *fc;
  434. size += 4 + 1 + 2; /* size[4] id[1] tag[2] */
  435. fc = kmalloc(sizeof(struct p9_fcall) + size, GFP_KERNEL);
  436. if (!fc)
  437. return ERR_PTR(-ENOMEM);
  438. fc->sdata = (char *)fc + sizeof(*fc);
  439. buf_init(bufp, (char *)fc->sdata, size);
  440. p9_put_int32(bufp, size, &fc->size);
  441. p9_put_int8(bufp, id, &fc->id);
  442. p9_put_int16(bufp, P9_NOTAG, &fc->tag);
  443. return fc;
  444. }
  445. /**
  446. * p9_set_tag - set the tag field of an &p9_fcall structure
  447. * @fc: fcall structure to set tag within
  448. * @tag: tag id to set
  449. */
  450. void p9_set_tag(struct p9_fcall *fc, u16 tag)
  451. {
  452. fc->tag = tag;
  453. *(__le16 *) (fc->sdata + 5) = cpu_to_le16(tag);
  454. }
  455. EXPORT_SYMBOL(p9_set_tag);
  456. /**
  457. * p9_create_tversion - allocates and creates a T_VERSION request
  458. * @msize: requested maximum data size
  459. * @version: version string to negotiate
  460. *
  461. */
  462. struct p9_fcall *p9_create_tversion(u32 msize, char *version)
  463. {
  464. int size;
  465. struct p9_fcall *fc;
  466. struct cbuf buffer;
  467. struct cbuf *bufp = &buffer;
  468. size = 4 + 2 + strlen(version); /* msize[4] version[s] */
  469. fc = p9_create_common(bufp, size, P9_TVERSION);
  470. if (IS_ERR(fc))
  471. goto error;
  472. p9_put_int32(bufp, msize, &fc->params.tversion.msize);
  473. p9_put_str(bufp, version, &fc->params.tversion.version);
  474. if (buf_check_overflow(bufp)) {
  475. kfree(fc);
  476. fc = ERR_PTR(-ENOMEM);
  477. }
  478. error:
  479. return fc;
  480. }
  481. EXPORT_SYMBOL(p9_create_tversion);
  482. /**
  483. * p9_create_tauth - allocates and creates a T_AUTH request
  484. * @afid: handle to use for authentication protocol
  485. * @uname: user name attempting to authenticate
  486. * @aname: mount specifier for remote server
  487. * @n_uname: numeric id for user attempting to authneticate
  488. * @dotu: 9P2000.u extension flag
  489. *
  490. */
  491. struct p9_fcall *p9_create_tauth(u32 afid, char *uname, char *aname,
  492. u32 n_uname, int dotu)
  493. {
  494. int size;
  495. struct p9_fcall *fc;
  496. struct cbuf buffer;
  497. struct cbuf *bufp = &buffer;
  498. /* afid[4] uname[s] aname[s] */
  499. size = 4 + 2 + 2;
  500. if (uname)
  501. size += strlen(uname);
  502. if (aname)
  503. size += strlen(aname);
  504. if (dotu)
  505. size += 4; /* n_uname */
  506. fc = p9_create_common(bufp, size, P9_TAUTH);
  507. if (IS_ERR(fc))
  508. goto error;
  509. p9_put_int32(bufp, afid, &fc->params.tauth.afid);
  510. p9_put_str(bufp, uname, &fc->params.tauth.uname);
  511. p9_put_str(bufp, aname, &fc->params.tauth.aname);
  512. if (dotu)
  513. p9_put_int32(bufp, n_uname, &fc->params.tauth.n_uname);
  514. if (buf_check_overflow(bufp)) {
  515. kfree(fc);
  516. fc = ERR_PTR(-ENOMEM);
  517. }
  518. error:
  519. return fc;
  520. }
  521. EXPORT_SYMBOL(p9_create_tauth);
  522. /**
  523. * p9_create_tattach - allocates and creates a T_ATTACH request
  524. * @fid: handle to use for the new mount point
  525. * @afid: handle to use for authentication protocol
  526. * @uname: user name attempting to attach
  527. * @aname: mount specifier for remote server
  528. * @n_uname: numeric id for user attempting to attach
  529. * @n_uname: numeric id for user attempting to attach
  530. * @dotu: 9P2000.u extension flag
  531. *
  532. */
  533. struct p9_fcall *
  534. p9_create_tattach(u32 fid, u32 afid, char *uname, char *aname,
  535. u32 n_uname, int dotu)
  536. {
  537. int size;
  538. struct p9_fcall *fc;
  539. struct cbuf buffer;
  540. struct cbuf *bufp = &buffer;
  541. /* fid[4] afid[4] uname[s] aname[s] */
  542. size = 4 + 4 + 2 + 2;
  543. if (uname)
  544. size += strlen(uname);
  545. if (aname)
  546. size += strlen(aname);
  547. if (dotu)
  548. size += 4; /* n_uname */
  549. fc = p9_create_common(bufp, size, P9_TATTACH);
  550. if (IS_ERR(fc))
  551. goto error;
  552. p9_put_int32(bufp, fid, &fc->params.tattach.fid);
  553. p9_put_int32(bufp, afid, &fc->params.tattach.afid);
  554. p9_put_str(bufp, uname, &fc->params.tattach.uname);
  555. p9_put_str(bufp, aname, &fc->params.tattach.aname);
  556. if (dotu)
  557. p9_put_int32(bufp, n_uname, &fc->params.tattach.n_uname);
  558. error:
  559. return fc;
  560. }
  561. EXPORT_SYMBOL(p9_create_tattach);
  562. /**
  563. * p9_create_tflush - allocates and creates a T_FLUSH request
  564. * @oldtag: tag id for the transaction we are attempting to cancel
  565. *
  566. */
  567. struct p9_fcall *p9_create_tflush(u16 oldtag)
  568. {
  569. int size;
  570. struct p9_fcall *fc;
  571. struct cbuf buffer;
  572. struct cbuf *bufp = &buffer;
  573. size = 2; /* oldtag[2] */
  574. fc = p9_create_common(bufp, size, P9_TFLUSH);
  575. if (IS_ERR(fc))
  576. goto error;
  577. p9_put_int16(bufp, oldtag, &fc->params.tflush.oldtag);
  578. if (buf_check_overflow(bufp)) {
  579. kfree(fc);
  580. fc = ERR_PTR(-ENOMEM);
  581. }
  582. error:
  583. return fc;
  584. }
  585. EXPORT_SYMBOL(p9_create_tflush);
  586. /**
  587. * p9_create_twalk - allocates and creates a T_FLUSH request
  588. * @fid: handle we are traversing from
  589. * @newfid: a new handle for this transaction
  590. * @nwname: number of path elements to traverse
  591. * @wnames: array of path elements
  592. *
  593. */
  594. struct p9_fcall *p9_create_twalk(u32 fid, u32 newfid, u16 nwname,
  595. char **wnames)
  596. {
  597. int i, size;
  598. struct p9_fcall *fc;
  599. struct cbuf buffer;
  600. struct cbuf *bufp = &buffer;
  601. if (nwname > P9_MAXWELEM) {
  602. P9_DPRINTK(P9_DEBUG_ERROR, "nwname > %d\n", P9_MAXWELEM);
  603. return NULL;
  604. }
  605. size = 4 + 4 + 2; /* fid[4] newfid[4] nwname[2] ... */
  606. for (i = 0; i < nwname; i++) {
  607. size += 2 + strlen(wnames[i]); /* wname[s] */
  608. }
  609. fc = p9_create_common(bufp, size, P9_TWALK);
  610. if (IS_ERR(fc))
  611. goto error;
  612. p9_put_int32(bufp, fid, &fc->params.twalk.fid);
  613. p9_put_int32(bufp, newfid, &fc->params.twalk.newfid);
  614. p9_put_int16(bufp, nwname, &fc->params.twalk.nwname);
  615. for (i = 0; i < nwname; i++) {
  616. p9_put_str(bufp, wnames[i], &fc->params.twalk.wnames[i]);
  617. }
  618. if (buf_check_overflow(bufp)) {
  619. kfree(fc);
  620. fc = ERR_PTR(-ENOMEM);
  621. }
  622. error:
  623. return fc;
  624. }
  625. EXPORT_SYMBOL(p9_create_twalk);
  626. /**
  627. * p9_create_topen - allocates and creates a T_OPEN request
  628. * @fid: handle we are trying to open
  629. * @mode: what mode we are trying to open the file in
  630. *
  631. */
  632. struct p9_fcall *p9_create_topen(u32 fid, u8 mode)
  633. {
  634. int size;
  635. struct p9_fcall *fc;
  636. struct cbuf buffer;
  637. struct cbuf *bufp = &buffer;
  638. size = 4 + 1; /* fid[4] mode[1] */
  639. fc = p9_create_common(bufp, size, P9_TOPEN);
  640. if (IS_ERR(fc))
  641. goto error;
  642. p9_put_int32(bufp, fid, &fc->params.topen.fid);
  643. p9_put_int8(bufp, mode, &fc->params.topen.mode);
  644. if (buf_check_overflow(bufp)) {
  645. kfree(fc);
  646. fc = ERR_PTR(-ENOMEM);
  647. }
  648. error:
  649. return fc;
  650. }
  651. EXPORT_SYMBOL(p9_create_topen);
  652. /**
  653. * p9_create_tcreate - allocates and creates a T_CREATE request
  654. * @fid: handle of directory we are trying to create in
  655. * @name: name of the file we are trying to create
  656. * @perm: permissions for the file we are trying to create
  657. * @mode: what mode we are trying to open the file in
  658. * @extension: 9p2000.u extension string (for special files)
  659. * @dotu: 9p2000.u enabled flag
  660. *
  661. * Note: Plan 9 create semantics include opening the resulting file
  662. * which is why mode is included.
  663. */
  664. struct p9_fcall *p9_create_tcreate(u32 fid, char *name, u32 perm, u8 mode,
  665. char *extension, int dotu)
  666. {
  667. int size;
  668. struct p9_fcall *fc;
  669. struct cbuf buffer;
  670. struct cbuf *bufp = &buffer;
  671. /* fid[4] name[s] perm[4] mode[1] */
  672. size = 4 + 2 + strlen(name) + 4 + 1;
  673. if (dotu) {
  674. size += 2 + /* extension[s] */
  675. (extension == NULL ? 0 : strlen(extension));
  676. }
  677. fc = p9_create_common(bufp, size, P9_TCREATE);
  678. if (IS_ERR(fc))
  679. goto error;
  680. p9_put_int32(bufp, fid, &fc->params.tcreate.fid);
  681. p9_put_str(bufp, name, &fc->params.tcreate.name);
  682. p9_put_int32(bufp, perm, &fc->params.tcreate.perm);
  683. p9_put_int8(bufp, mode, &fc->params.tcreate.mode);
  684. if (dotu)
  685. p9_put_str(bufp, extension, &fc->params.tcreate.extension);
  686. if (buf_check_overflow(bufp)) {
  687. kfree(fc);
  688. fc = ERR_PTR(-ENOMEM);
  689. }
  690. error:
  691. return fc;
  692. }
  693. EXPORT_SYMBOL(p9_create_tcreate);
  694. /**
  695. * p9_create_tread - allocates and creates a T_READ request
  696. * @fid: handle of the file we are trying to read
  697. * @offset: offset to start reading from
  698. * @count: how many bytes to read
  699. */
  700. struct p9_fcall *p9_create_tread(u32 fid, u64 offset, u32 count)
  701. {
  702. int size;
  703. struct p9_fcall *fc;
  704. struct cbuf buffer;
  705. struct cbuf *bufp = &buffer;
  706. size = 4 + 8 + 4; /* fid[4] offset[8] count[4] */
  707. fc = p9_create_common(bufp, size, P9_TREAD);
  708. if (IS_ERR(fc))
  709. goto error;
  710. p9_put_int32(bufp, fid, &fc->params.tread.fid);
  711. p9_put_int64(bufp, offset, &fc->params.tread.offset);
  712. p9_put_int32(bufp, count, &fc->params.tread.count);
  713. if (buf_check_overflow(bufp)) {
  714. kfree(fc);
  715. fc = ERR_PTR(-ENOMEM);
  716. }
  717. error:
  718. return fc;
  719. }
  720. EXPORT_SYMBOL(p9_create_tread);
  721. /**
  722. * p9_create_twrite - allocates and creates a T_WRITE request from the kernel
  723. * @fid: handle of the file we are trying to write
  724. * @offset: offset to start writing at
  725. * @count: how many bytes to write
  726. * @data: data to write
  727. *
  728. * This function will create a requst with data buffers from the kernel
  729. * such as the page cache.
  730. */
  731. struct p9_fcall *p9_create_twrite(u32 fid, u64 offset, u32 count,
  732. const char *data)
  733. {
  734. int size, err;
  735. struct p9_fcall *fc;
  736. struct cbuf buffer;
  737. struct cbuf *bufp = &buffer;
  738. /* fid[4] offset[8] count[4] data[count] */
  739. size = 4 + 8 + 4 + count;
  740. fc = p9_create_common(bufp, size, P9_TWRITE);
  741. if (IS_ERR(fc))
  742. goto error;
  743. p9_put_int32(bufp, fid, &fc->params.twrite.fid);
  744. p9_put_int64(bufp, offset, &fc->params.twrite.offset);
  745. p9_put_int32(bufp, count, &fc->params.twrite.count);
  746. err = p9_put_data(bufp, data, count, &fc->params.twrite.data);
  747. if (err) {
  748. kfree(fc);
  749. fc = ERR_PTR(err);
  750. goto error;
  751. }
  752. if (buf_check_overflow(bufp)) {
  753. kfree(fc);
  754. fc = ERR_PTR(-ENOMEM);
  755. }
  756. error:
  757. return fc;
  758. }
  759. EXPORT_SYMBOL(p9_create_twrite);
  760. /**
  761. * p9_create_twrite_u - allocates and creates a T_WRITE request from userspace
  762. * @fid: handle of the file we are trying to write
  763. * @offset: offset to start writing at
  764. * @count: how many bytes to write
  765. * @data: data to write
  766. *
  767. * This function will create a request with data buffers from userspace
  768. */
  769. struct p9_fcall *p9_create_twrite_u(u32 fid, u64 offset, u32 count,
  770. const char __user *data)
  771. {
  772. int size, err;
  773. struct p9_fcall *fc;
  774. struct cbuf buffer;
  775. struct cbuf *bufp = &buffer;
  776. /* fid[4] offset[8] count[4] data[count] */
  777. size = 4 + 8 + 4 + count;
  778. fc = p9_create_common(bufp, size, P9_TWRITE);
  779. if (IS_ERR(fc))
  780. goto error;
  781. p9_put_int32(bufp, fid, &fc->params.twrite.fid);
  782. p9_put_int64(bufp, offset, &fc->params.twrite.offset);
  783. p9_put_int32(bufp, count, &fc->params.twrite.count);
  784. err = p9_put_user_data(bufp, data, count, &fc->params.twrite.data);
  785. if (err) {
  786. kfree(fc);
  787. fc = ERR_PTR(err);
  788. goto error;
  789. }
  790. if (buf_check_overflow(bufp)) {
  791. kfree(fc);
  792. fc = ERR_PTR(-ENOMEM);
  793. }
  794. error:
  795. return fc;
  796. }
  797. EXPORT_SYMBOL(p9_create_twrite_u);
  798. /**
  799. * p9_create_tclunk - allocate a request to forget about a file handle
  800. * @fid: handle of the file we closing or forgetting about
  801. *
  802. * clunk is used both to close open files and to discard transient handles
  803. * which may be created during meta-data operations and hierarchy traversal.
  804. */
  805. struct p9_fcall *p9_create_tclunk(u32 fid)
  806. {
  807. int size;
  808. struct p9_fcall *fc;
  809. struct cbuf buffer;
  810. struct cbuf *bufp = &buffer;
  811. size = 4; /* fid[4] */
  812. fc = p9_create_common(bufp, size, P9_TCLUNK);
  813. if (IS_ERR(fc))
  814. goto error;
  815. p9_put_int32(bufp, fid, &fc->params.tclunk.fid);
  816. if (buf_check_overflow(bufp)) {
  817. kfree(fc);
  818. fc = ERR_PTR(-ENOMEM);
  819. }
  820. error:
  821. return fc;
  822. }
  823. EXPORT_SYMBOL(p9_create_tclunk);
  824. /**
  825. * p9_create_tremove - allocate and create a request to remove a file
  826. * @fid: handle of the file or directory we are removing
  827. *
  828. */
  829. struct p9_fcall *p9_create_tremove(u32 fid)
  830. {
  831. int size;
  832. struct p9_fcall *fc;
  833. struct cbuf buffer;
  834. struct cbuf *bufp = &buffer;
  835. size = 4; /* fid[4] */
  836. fc = p9_create_common(bufp, size, P9_TREMOVE);
  837. if (IS_ERR(fc))
  838. goto error;
  839. p9_put_int32(bufp, fid, &fc->params.tremove.fid);
  840. if (buf_check_overflow(bufp)) {
  841. kfree(fc);
  842. fc = ERR_PTR(-ENOMEM);
  843. }
  844. error:
  845. return fc;
  846. }
  847. EXPORT_SYMBOL(p9_create_tremove);
  848. /**
  849. * p9_create_tstat - allocate and populate a request for attributes
  850. * @fid: handle of the file or directory we are trying to get the attributes of
  851. *
  852. */
  853. struct p9_fcall *p9_create_tstat(u32 fid)
  854. {
  855. int size;
  856. struct p9_fcall *fc;
  857. struct cbuf buffer;
  858. struct cbuf *bufp = &buffer;
  859. size = 4; /* fid[4] */
  860. fc = p9_create_common(bufp, size, P9_TSTAT);
  861. if (IS_ERR(fc))
  862. goto error;
  863. p9_put_int32(bufp, fid, &fc->params.tstat.fid);
  864. if (buf_check_overflow(bufp)) {
  865. kfree(fc);
  866. fc = ERR_PTR(-ENOMEM);
  867. }
  868. error:
  869. return fc;
  870. }
  871. EXPORT_SYMBOL(p9_create_tstat);
  872. /**
  873. * p9_create_tstat - allocate and populate a request to change attributes
  874. * @fid: handle of the file or directory we are trying to change
  875. * @wstat: &p9_stat structure with attributes we wish to set
  876. * @dotu: 9p2000.u enabled flag
  877. *
  878. */
  879. struct p9_fcall *p9_create_twstat(u32 fid, struct p9_wstat *wstat,
  880. int dotu)
  881. {
  882. int size, statsz;
  883. struct p9_fcall *fc;
  884. struct cbuf buffer;
  885. struct cbuf *bufp = &buffer;
  886. statsz = p9_size_wstat(wstat, dotu);
  887. size = 4 + 2 + 2 + statsz; /* fid[4] stat[n] */
  888. fc = p9_create_common(bufp, size, P9_TWSTAT);
  889. if (IS_ERR(fc))
  890. goto error;
  891. p9_put_int32(bufp, fid, &fc->params.twstat.fid);
  892. buf_put_int16(bufp, statsz + 2);
  893. p9_put_wstat(bufp, wstat, &fc->params.twstat.stat, statsz, dotu);
  894. if (buf_check_overflow(bufp)) {
  895. kfree(fc);
  896. fc = ERR_PTR(-ENOMEM);
  897. }
  898. error:
  899. return fc;
  900. }
  901. EXPORT_SYMBOL(p9_create_twstat);