protocol.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. * net/9p/protocol.c
  3. *
  4. * 9P Protocol Support Code
  5. *
  6. * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
  7. *
  8. * Base on code from Anthony Liguori <aliguori@us.ibm.com>
  9. * Copyright (C) 2008 by IBM, Corp.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2
  13. * as published by the Free Software Foundation.
  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/module.h>
  28. #include <linux/errno.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/sched.h>
  31. #include <linux/types.h>
  32. #include <net/9p/9p.h>
  33. #include <net/9p/client.h>
  34. #include "protocol.h"
  35. #ifndef MIN
  36. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  37. #endif
  38. #ifndef MAX
  39. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  40. #endif
  41. #ifndef offset_of
  42. #define offset_of(type, memb) \
  43. ((unsigned long)(&((type *)0)->memb))
  44. #endif
  45. #ifndef container_of
  46. #define container_of(obj, type, memb) \
  47. ((type *)(((char *)obj) - offset_of(type, memb)))
  48. #endif
  49. static int
  50. p9pdu_writef(struct p9_fcall *pdu, int optional, const char *fmt, ...);
  51. #ifdef CONFIG_NET_9P_DEBUG
  52. void
  53. p9pdu_dump(int way, struct p9_fcall *pdu)
  54. {
  55. int i, n;
  56. u8 *data = pdu->sdata;
  57. int datalen = pdu->size;
  58. char buf[255];
  59. int buflen = 255;
  60. i = n = 0;
  61. if (datalen > (buflen-16))
  62. datalen = buflen-16;
  63. while (i < datalen) {
  64. n += scnprintf(buf + n, buflen - n, "%02x ", data[i]);
  65. if (i%4 == 3)
  66. n += scnprintf(buf + n, buflen - n, " ");
  67. if (i%32 == 31)
  68. n += scnprintf(buf + n, buflen - n, "\n");
  69. i++;
  70. }
  71. n += scnprintf(buf + n, buflen - n, "\n");
  72. if (way)
  73. P9_DPRINTK(P9_DEBUG_PKT, "[[[(%d) %s\n", datalen, buf);
  74. else
  75. P9_DPRINTK(P9_DEBUG_PKT, "]]](%d) %s\n", datalen, buf);
  76. }
  77. #else
  78. void
  79. p9pdu_dump(int way, struct p9_fcall *pdu)
  80. {
  81. }
  82. #endif
  83. EXPORT_SYMBOL(p9pdu_dump);
  84. void p9stat_free(struct p9_wstat *stbuf)
  85. {
  86. kfree(stbuf->name);
  87. kfree(stbuf->uid);
  88. kfree(stbuf->gid);
  89. kfree(stbuf->muid);
  90. kfree(stbuf->extension);
  91. }
  92. EXPORT_SYMBOL(p9stat_free);
  93. static size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
  94. {
  95. size_t len = MIN(pdu->size - pdu->offset, size);
  96. memcpy(data, &pdu->sdata[pdu->offset], len);
  97. pdu->offset += len;
  98. return size - len;
  99. }
  100. static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
  101. {
  102. size_t len = MIN(pdu->capacity - pdu->size, size);
  103. memcpy(&pdu->sdata[pdu->size], data, len);
  104. pdu->size += len;
  105. return size - len;
  106. }
  107. static size_t
  108. pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size)
  109. {
  110. size_t len = MIN(pdu->capacity - pdu->size, size);
  111. int err = copy_from_user(&pdu->sdata[pdu->size], udata, len);
  112. if (err)
  113. printk(KERN_WARNING "pdu_write_u returning: %d\n", err);
  114. pdu->size += len;
  115. return size - len;
  116. }
  117. /*
  118. b - int8_t
  119. w - int16_t
  120. d - int32_t
  121. q - int64_t
  122. s - string
  123. S - stat
  124. Q - qid
  125. D - data blob (int32_t size followed by void *, results are not freed)
  126. T - array of strings (int16_t count, followed by strings)
  127. R - array of qids (int16_t count, followed by qids)
  128. ? - if optional = 1, continue parsing
  129. */
  130. static int
  131. p9pdu_vreadf(struct p9_fcall *pdu, int optional, const char *fmt, va_list ap)
  132. {
  133. const char *ptr;
  134. int errcode = 0;
  135. for (ptr = fmt; *ptr; ptr++) {
  136. switch (*ptr) {
  137. case 'b':{
  138. int8_t *val = va_arg(ap, int8_t *);
  139. if (pdu_read(pdu, val, sizeof(*val))) {
  140. errcode = -EFAULT;
  141. break;
  142. }
  143. }
  144. break;
  145. case 'w':{
  146. int16_t *val = va_arg(ap, int16_t *);
  147. __le16 le_val;
  148. if (pdu_read(pdu, &le_val, sizeof(le_val))) {
  149. errcode = -EFAULT;
  150. break;
  151. }
  152. *val = le16_to_cpu(le_val);
  153. }
  154. break;
  155. case 'd':{
  156. int32_t *val = va_arg(ap, int32_t *);
  157. __le32 le_val;
  158. if (pdu_read(pdu, &le_val, sizeof(le_val))) {
  159. errcode = -EFAULT;
  160. break;
  161. }
  162. *val = le32_to_cpu(le_val);
  163. }
  164. break;
  165. case 'q':{
  166. int64_t *val = va_arg(ap, int64_t *);
  167. __le64 le_val;
  168. if (pdu_read(pdu, &le_val, sizeof(le_val))) {
  169. errcode = -EFAULT;
  170. break;
  171. }
  172. *val = le64_to_cpu(le_val);
  173. }
  174. break;
  175. case 's':{
  176. char **sptr = va_arg(ap, char **);
  177. int16_t len;
  178. int size;
  179. errcode = p9pdu_readf(pdu, optional, "w", &len);
  180. if (errcode)
  181. break;
  182. size = MAX(len, 0);
  183. *sptr = kmalloc(size + 1, GFP_KERNEL);
  184. if (*sptr == NULL) {
  185. errcode = -EFAULT;
  186. break;
  187. }
  188. if (pdu_read(pdu, *sptr, size)) {
  189. errcode = -EFAULT;
  190. kfree(*sptr);
  191. *sptr = NULL;
  192. } else
  193. (*sptr)[size] = 0;
  194. }
  195. break;
  196. case 'Q':{
  197. struct p9_qid *qid =
  198. va_arg(ap, struct p9_qid *);
  199. errcode = p9pdu_readf(pdu, optional, "bdq",
  200. &qid->type, &qid->version,
  201. &qid->path);
  202. }
  203. break;
  204. case 'S':{
  205. struct p9_wstat *stbuf =
  206. va_arg(ap, struct p9_wstat *);
  207. memset(stbuf, 0, sizeof(struct p9_wstat));
  208. stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
  209. -1;
  210. errcode =
  211. p9pdu_readf(pdu, optional,
  212. "wwdQdddqssss?sddd",
  213. &stbuf->size, &stbuf->type,
  214. &stbuf->dev, &stbuf->qid,
  215. &stbuf->mode, &stbuf->atime,
  216. &stbuf->mtime, &stbuf->length,
  217. &stbuf->name, &stbuf->uid,
  218. &stbuf->gid, &stbuf->muid,
  219. &stbuf->extension,
  220. &stbuf->n_uid, &stbuf->n_gid,
  221. &stbuf->n_muid);
  222. if (errcode)
  223. p9stat_free(stbuf);
  224. }
  225. break;
  226. case 'D':{
  227. int32_t *count = va_arg(ap, int32_t *);
  228. void **data = va_arg(ap, void **);
  229. errcode =
  230. p9pdu_readf(pdu, optional, "d", count);
  231. if (!errcode) {
  232. *count =
  233. MIN(*count,
  234. pdu->size - pdu->offset);
  235. *data = &pdu->sdata[pdu->offset];
  236. }
  237. }
  238. break;
  239. case 'T':{
  240. int16_t *nwname = va_arg(ap, int16_t *);
  241. char ***wnames = va_arg(ap, char ***);
  242. errcode =
  243. p9pdu_readf(pdu, optional, "w", nwname);
  244. if (!errcode) {
  245. *wnames =
  246. kmalloc(sizeof(char *) * *nwname,
  247. GFP_KERNEL);
  248. if (!*wnames)
  249. errcode = -ENOMEM;
  250. }
  251. if (!errcode) {
  252. int i;
  253. for (i = 0; i < *nwname; i++) {
  254. errcode =
  255. p9pdu_readf(pdu, optional,
  256. "s",
  257. &(*wnames)[i]);
  258. if (errcode)
  259. break;
  260. }
  261. }
  262. if (errcode) {
  263. if (*wnames) {
  264. int i;
  265. for (i = 0; i < *nwname; i++)
  266. kfree((*wnames)[i]);
  267. }
  268. kfree(*wnames);
  269. *wnames = NULL;
  270. }
  271. }
  272. break;
  273. case 'R':{
  274. int16_t *nwqid = va_arg(ap, int16_t *);
  275. struct p9_qid **wqids =
  276. va_arg(ap, struct p9_qid **);
  277. *wqids = NULL;
  278. errcode =
  279. p9pdu_readf(pdu, optional, "w", nwqid);
  280. if (!errcode) {
  281. *wqids =
  282. kmalloc(*nwqid *
  283. sizeof(struct p9_qid),
  284. GFP_KERNEL);
  285. if (*wqids == NULL)
  286. errcode = -ENOMEM;
  287. }
  288. if (!errcode) {
  289. int i;
  290. for (i = 0; i < *nwqid; i++) {
  291. errcode =
  292. p9pdu_readf(pdu, optional,
  293. "Q",
  294. &(*wqids)[i]);
  295. if (errcode)
  296. break;
  297. }
  298. }
  299. if (errcode) {
  300. kfree(*wqids);
  301. *wqids = NULL;
  302. }
  303. }
  304. break;
  305. case '?':
  306. if (!optional)
  307. return 0;
  308. break;
  309. default:
  310. BUG();
  311. break;
  312. }
  313. if (errcode)
  314. break;
  315. }
  316. return errcode;
  317. }
  318. int
  319. p9pdu_vwritef(struct p9_fcall *pdu, int optional, const char *fmt, va_list ap)
  320. {
  321. const char *ptr;
  322. int errcode = 0;
  323. for (ptr = fmt; *ptr; ptr++) {
  324. switch (*ptr) {
  325. case 'b':{
  326. int8_t val = va_arg(ap, int);
  327. if (pdu_write(pdu, &val, sizeof(val)))
  328. errcode = -EFAULT;
  329. }
  330. break;
  331. case 'w':{
  332. __le16 val = cpu_to_le16(va_arg(ap, int));
  333. if (pdu_write(pdu, &val, sizeof(val)))
  334. errcode = -EFAULT;
  335. }
  336. break;
  337. case 'd':{
  338. __le32 val = cpu_to_le32(va_arg(ap, int32_t));
  339. if (pdu_write(pdu, &val, sizeof(val)))
  340. errcode = -EFAULT;
  341. }
  342. break;
  343. case 'q':{
  344. __le64 val = cpu_to_le64(va_arg(ap, int64_t));
  345. if (pdu_write(pdu, &val, sizeof(val)))
  346. errcode = -EFAULT;
  347. }
  348. break;
  349. case 's':{
  350. const char *sptr = va_arg(ap, const char *);
  351. int16_t len = 0;
  352. if (sptr)
  353. len = MIN(strlen(sptr), USHORT_MAX);
  354. errcode = p9pdu_writef(pdu, optional, "w", len);
  355. if (!errcode && pdu_write(pdu, sptr, len))
  356. errcode = -EFAULT;
  357. }
  358. break;
  359. case 'Q':{
  360. const struct p9_qid *qid =
  361. va_arg(ap, const struct p9_qid *);
  362. errcode =
  363. p9pdu_writef(pdu, optional, "bdq",
  364. qid->type, qid->version,
  365. qid->path);
  366. } break;
  367. case 'S':{
  368. const struct p9_wstat *stbuf =
  369. va_arg(ap, const struct p9_wstat *);
  370. errcode =
  371. p9pdu_writef(pdu, optional,
  372. "wwdQdddqssss?sddd",
  373. stbuf->size, stbuf->type,
  374. stbuf->dev, &stbuf->qid,
  375. stbuf->mode, stbuf->atime,
  376. stbuf->mtime, stbuf->length,
  377. stbuf->name, stbuf->uid,
  378. stbuf->gid, stbuf->muid,
  379. stbuf->extension, stbuf->n_uid,
  380. stbuf->n_gid, stbuf->n_muid);
  381. } break;
  382. case 'D':{
  383. int32_t count = va_arg(ap, int32_t);
  384. const void *data = va_arg(ap, const void *);
  385. errcode =
  386. p9pdu_writef(pdu, optional, "d", count);
  387. if (!errcode && pdu_write(pdu, data, count))
  388. errcode = -EFAULT;
  389. }
  390. break;
  391. case 'U':{
  392. int32_t count = va_arg(ap, int32_t);
  393. const char __user *udata =
  394. va_arg(ap, const void __user *);
  395. errcode =
  396. p9pdu_writef(pdu, optional, "d", count);
  397. if (!errcode && pdu_write_u(pdu, udata, count))
  398. errcode = -EFAULT;
  399. }
  400. break;
  401. case 'T':{
  402. int16_t nwname = va_arg(ap, int);
  403. const char **wnames = va_arg(ap, const char **);
  404. errcode =
  405. p9pdu_writef(pdu, optional, "w", nwname);
  406. if (!errcode) {
  407. int i;
  408. for (i = 0; i < nwname; i++) {
  409. errcode =
  410. p9pdu_writef(pdu, optional,
  411. "s",
  412. wnames[i]);
  413. if (errcode)
  414. break;
  415. }
  416. }
  417. }
  418. break;
  419. case 'R':{
  420. int16_t nwqid = va_arg(ap, int);
  421. struct p9_qid *wqids =
  422. va_arg(ap, struct p9_qid *);
  423. errcode =
  424. p9pdu_writef(pdu, optional, "w", nwqid);
  425. if (!errcode) {
  426. int i;
  427. for (i = 0; i < nwqid; i++) {
  428. errcode =
  429. p9pdu_writef(pdu, optional,
  430. "Q",
  431. &wqids[i]);
  432. if (errcode)
  433. break;
  434. }
  435. }
  436. }
  437. break;
  438. case '?':
  439. if (!optional)
  440. return 0;
  441. break;
  442. default:
  443. BUG();
  444. break;
  445. }
  446. if (errcode)
  447. break;
  448. }
  449. return errcode;
  450. }
  451. int p9pdu_readf(struct p9_fcall *pdu, int optional, const char *fmt, ...)
  452. {
  453. va_list ap;
  454. int ret;
  455. va_start(ap, fmt);
  456. ret = p9pdu_vreadf(pdu, optional, fmt, ap);
  457. va_end(ap);
  458. return ret;
  459. }
  460. static int
  461. p9pdu_writef(struct p9_fcall *pdu, int optional, const char *fmt, ...)
  462. {
  463. va_list ap;
  464. int ret;
  465. va_start(ap, fmt);
  466. ret = p9pdu_vwritef(pdu, optional, fmt, ap);
  467. va_end(ap);
  468. return ret;
  469. }
  470. int p9stat_read(char *buf, int len, struct p9_wstat *st, int dotu)
  471. {
  472. struct p9_fcall fake_pdu;
  473. int ret;
  474. fake_pdu.size = len;
  475. fake_pdu.capacity = len;
  476. fake_pdu.sdata = buf;
  477. fake_pdu.offset = 0;
  478. ret = p9pdu_readf(&fake_pdu, dotu, "S", st);
  479. if (ret) {
  480. P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
  481. p9pdu_dump(1, &fake_pdu);
  482. }
  483. return ret;
  484. }
  485. EXPORT_SYMBOL(p9stat_read);
  486. int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
  487. {
  488. return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
  489. }
  490. int p9pdu_finalize(struct p9_fcall *pdu)
  491. {
  492. int size = pdu->size;
  493. int err;
  494. pdu->size = 0;
  495. err = p9pdu_writef(pdu, 0, "d", size);
  496. pdu->size = size;
  497. #ifdef CONFIG_NET_9P_DEBUG
  498. if ((p9_debug_level & P9_DEBUG_PKT) == P9_DEBUG_PKT)
  499. p9pdu_dump(0, pdu);
  500. #endif
  501. P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size,
  502. pdu->id, pdu->tag);
  503. return err;
  504. }
  505. void p9pdu_reset(struct p9_fcall *pdu)
  506. {
  507. pdu->offset = 0;
  508. pdu->size = 0;
  509. }