protocol.c 12 KB

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