protocol.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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 proto_version, 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 proto_version, const char *fmt,
  132. va_list ap)
  133. {
  134. const char *ptr;
  135. int errcode = 0;
  136. for (ptr = fmt; *ptr; ptr++) {
  137. switch (*ptr) {
  138. case 'b':{
  139. int8_t *val = va_arg(ap, int8_t *);
  140. if (pdu_read(pdu, val, sizeof(*val))) {
  141. errcode = -EFAULT;
  142. break;
  143. }
  144. }
  145. break;
  146. case 'w':{
  147. int16_t *val = va_arg(ap, int16_t *);
  148. __le16 le_val;
  149. if (pdu_read(pdu, &le_val, sizeof(le_val))) {
  150. errcode = -EFAULT;
  151. break;
  152. }
  153. *val = le16_to_cpu(le_val);
  154. }
  155. break;
  156. case 'd':{
  157. int32_t *val = va_arg(ap, int32_t *);
  158. __le32 le_val;
  159. if (pdu_read(pdu, &le_val, sizeof(le_val))) {
  160. errcode = -EFAULT;
  161. break;
  162. }
  163. *val = le32_to_cpu(le_val);
  164. }
  165. break;
  166. case 'q':{
  167. int64_t *val = va_arg(ap, int64_t *);
  168. __le64 le_val;
  169. if (pdu_read(pdu, &le_val, sizeof(le_val))) {
  170. errcode = -EFAULT;
  171. break;
  172. }
  173. *val = le64_to_cpu(le_val);
  174. }
  175. break;
  176. case 's':{
  177. char **sptr = va_arg(ap, char **);
  178. int16_t len;
  179. int size;
  180. errcode = p9pdu_readf(pdu, proto_version,
  181. "w", &len);
  182. if (errcode)
  183. break;
  184. size = MAX(len, 0);
  185. *sptr = kmalloc(size + 1, GFP_KERNEL);
  186. if (*sptr == NULL) {
  187. errcode = -EFAULT;
  188. break;
  189. }
  190. if (pdu_read(pdu, *sptr, size)) {
  191. errcode = -EFAULT;
  192. kfree(*sptr);
  193. *sptr = NULL;
  194. } else
  195. (*sptr)[size] = 0;
  196. }
  197. break;
  198. case 'Q':{
  199. struct p9_qid *qid =
  200. va_arg(ap, struct p9_qid *);
  201. errcode = p9pdu_readf(pdu, proto_version, "bdq",
  202. &qid->type, &qid->version,
  203. &qid->path);
  204. }
  205. break;
  206. case 'S':{
  207. struct p9_wstat *stbuf =
  208. va_arg(ap, struct p9_wstat *);
  209. memset(stbuf, 0, sizeof(struct p9_wstat));
  210. stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
  211. -1;
  212. errcode =
  213. p9pdu_readf(pdu, proto_version,
  214. "wwdQdddqssss?sddd",
  215. &stbuf->size, &stbuf->type,
  216. &stbuf->dev, &stbuf->qid,
  217. &stbuf->mode, &stbuf->atime,
  218. &stbuf->mtime, &stbuf->length,
  219. &stbuf->name, &stbuf->uid,
  220. &stbuf->gid, &stbuf->muid,
  221. &stbuf->extension,
  222. &stbuf->n_uid, &stbuf->n_gid,
  223. &stbuf->n_muid);
  224. if (errcode)
  225. p9stat_free(stbuf);
  226. }
  227. break;
  228. case 'D':{
  229. int32_t *count = va_arg(ap, int32_t *);
  230. void **data = va_arg(ap, void **);
  231. errcode =
  232. p9pdu_readf(pdu, proto_version, "d", count);
  233. if (!errcode) {
  234. *count =
  235. MIN(*count,
  236. pdu->size - pdu->offset);
  237. *data = &pdu->sdata[pdu->offset];
  238. }
  239. }
  240. break;
  241. case 'T':{
  242. int16_t *nwname = va_arg(ap, int16_t *);
  243. char ***wnames = va_arg(ap, char ***);
  244. errcode = p9pdu_readf(pdu, proto_version,
  245. "w", nwname);
  246. if (!errcode) {
  247. *wnames =
  248. kmalloc(sizeof(char *) * *nwname,
  249. GFP_KERNEL);
  250. if (!*wnames)
  251. errcode = -ENOMEM;
  252. }
  253. if (!errcode) {
  254. int i;
  255. for (i = 0; i < *nwname; i++) {
  256. errcode =
  257. p9pdu_readf(pdu,
  258. proto_version,
  259. "s",
  260. &(*wnames)[i]);
  261. if (errcode)
  262. break;
  263. }
  264. }
  265. if (errcode) {
  266. if (*wnames) {
  267. int i;
  268. for (i = 0; i < *nwname; i++)
  269. kfree((*wnames)[i]);
  270. }
  271. kfree(*wnames);
  272. *wnames = NULL;
  273. }
  274. }
  275. break;
  276. case 'R':{
  277. int16_t *nwqid = va_arg(ap, int16_t *);
  278. struct p9_qid **wqids =
  279. va_arg(ap, struct p9_qid **);
  280. *wqids = NULL;
  281. errcode =
  282. p9pdu_readf(pdu, proto_version, "w", nwqid);
  283. if (!errcode) {
  284. *wqids =
  285. kmalloc(*nwqid *
  286. sizeof(struct p9_qid),
  287. GFP_KERNEL);
  288. if (*wqids == NULL)
  289. errcode = -ENOMEM;
  290. }
  291. if (!errcode) {
  292. int i;
  293. for (i = 0; i < *nwqid; i++) {
  294. errcode =
  295. p9pdu_readf(pdu,
  296. proto_version,
  297. "Q",
  298. &(*wqids)[i]);
  299. if (errcode)
  300. break;
  301. }
  302. }
  303. if (errcode) {
  304. kfree(*wqids);
  305. *wqids = NULL;
  306. }
  307. }
  308. break;
  309. case '?':
  310. if (proto_version != p9_proto_2000u)
  311. return 0;
  312. break;
  313. default:
  314. BUG();
  315. break;
  316. }
  317. if (errcode)
  318. break;
  319. }
  320. return errcode;
  321. }
  322. int
  323. p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
  324. va_list ap)
  325. {
  326. const char *ptr;
  327. int errcode = 0;
  328. for (ptr = fmt; *ptr; ptr++) {
  329. switch (*ptr) {
  330. case 'b':{
  331. int8_t val = va_arg(ap, int);
  332. if (pdu_write(pdu, &val, sizeof(val)))
  333. errcode = -EFAULT;
  334. }
  335. break;
  336. case 'w':{
  337. __le16 val = cpu_to_le16(va_arg(ap, int));
  338. if (pdu_write(pdu, &val, sizeof(val)))
  339. errcode = -EFAULT;
  340. }
  341. break;
  342. case 'd':{
  343. __le32 val = cpu_to_le32(va_arg(ap, int32_t));
  344. if (pdu_write(pdu, &val, sizeof(val)))
  345. errcode = -EFAULT;
  346. }
  347. break;
  348. case 'q':{
  349. __le64 val = cpu_to_le64(va_arg(ap, int64_t));
  350. if (pdu_write(pdu, &val, sizeof(val)))
  351. errcode = -EFAULT;
  352. }
  353. break;
  354. case 's':{
  355. const char *sptr = va_arg(ap, const char *);
  356. int16_t len = 0;
  357. if (sptr)
  358. len = MIN(strlen(sptr), USHORT_MAX);
  359. errcode = p9pdu_writef(pdu, proto_version,
  360. "w", len);
  361. if (!errcode && pdu_write(pdu, sptr, len))
  362. errcode = -EFAULT;
  363. }
  364. break;
  365. case 'Q':{
  366. const struct p9_qid *qid =
  367. va_arg(ap, const struct p9_qid *);
  368. errcode =
  369. p9pdu_writef(pdu, proto_version, "bdq",
  370. qid->type, qid->version,
  371. qid->path);
  372. } break;
  373. case 'S':{
  374. const struct p9_wstat *stbuf =
  375. va_arg(ap, const struct p9_wstat *);
  376. errcode =
  377. p9pdu_writef(pdu, proto_version,
  378. "wwdQdddqssss?sddd",
  379. stbuf->size, stbuf->type,
  380. stbuf->dev, &stbuf->qid,
  381. stbuf->mode, stbuf->atime,
  382. stbuf->mtime, stbuf->length,
  383. stbuf->name, stbuf->uid,
  384. stbuf->gid, stbuf->muid,
  385. stbuf->extension, stbuf->n_uid,
  386. stbuf->n_gid, stbuf->n_muid);
  387. } break;
  388. case 'D':{
  389. int32_t count = va_arg(ap, int32_t);
  390. const void *data = va_arg(ap, const void *);
  391. errcode = p9pdu_writef(pdu, proto_version, "d",
  392. count);
  393. if (!errcode && pdu_write(pdu, data, count))
  394. errcode = -EFAULT;
  395. }
  396. break;
  397. case 'U':{
  398. int32_t count = va_arg(ap, int32_t);
  399. const char __user *udata =
  400. va_arg(ap, const void __user *);
  401. errcode = p9pdu_writef(pdu, proto_version, "d",
  402. count);
  403. if (!errcode && pdu_write_u(pdu, udata, count))
  404. errcode = -EFAULT;
  405. }
  406. break;
  407. case 'T':{
  408. int16_t nwname = va_arg(ap, int);
  409. const char **wnames = va_arg(ap, const char **);
  410. errcode = p9pdu_writef(pdu, proto_version, "w",
  411. nwname);
  412. if (!errcode) {
  413. int i;
  414. for (i = 0; i < nwname; i++) {
  415. errcode =
  416. p9pdu_writef(pdu,
  417. proto_version,
  418. "s",
  419. wnames[i]);
  420. if (errcode)
  421. break;
  422. }
  423. }
  424. }
  425. break;
  426. case 'R':{
  427. int16_t nwqid = va_arg(ap, int);
  428. struct p9_qid *wqids =
  429. va_arg(ap, struct p9_qid *);
  430. errcode = p9pdu_writef(pdu, proto_version, "w",
  431. nwqid);
  432. if (!errcode) {
  433. int i;
  434. for (i = 0; i < nwqid; i++) {
  435. errcode =
  436. p9pdu_writef(pdu,
  437. proto_version,
  438. "Q",
  439. &wqids[i]);
  440. if (errcode)
  441. break;
  442. }
  443. }
  444. }
  445. break;
  446. case '?':
  447. if (proto_version != p9_proto_2000u)
  448. return 0;
  449. break;
  450. default:
  451. BUG();
  452. break;
  453. }
  454. if (errcode)
  455. break;
  456. }
  457. return errcode;
  458. }
  459. int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
  460. {
  461. va_list ap;
  462. int ret;
  463. va_start(ap, fmt);
  464. ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
  465. va_end(ap);
  466. return ret;
  467. }
  468. static int
  469. p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
  470. {
  471. va_list ap;
  472. int ret;
  473. va_start(ap, fmt);
  474. ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
  475. va_end(ap);
  476. return ret;
  477. }
  478. int p9stat_read(char *buf, int len, struct p9_wstat *st, int proto_version)
  479. {
  480. struct p9_fcall fake_pdu;
  481. int ret;
  482. fake_pdu.size = len;
  483. fake_pdu.capacity = len;
  484. fake_pdu.sdata = buf;
  485. fake_pdu.offset = 0;
  486. ret = p9pdu_readf(&fake_pdu, proto_version, "S", st);
  487. if (ret) {
  488. P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
  489. p9pdu_dump(1, &fake_pdu);
  490. }
  491. return ret;
  492. }
  493. EXPORT_SYMBOL(p9stat_read);
  494. int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
  495. {
  496. return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
  497. }
  498. int p9pdu_finalize(struct p9_fcall *pdu)
  499. {
  500. int size = pdu->size;
  501. int err;
  502. pdu->size = 0;
  503. err = p9pdu_writef(pdu, 0, "d", size);
  504. pdu->size = size;
  505. #ifdef CONFIG_NET_9P_DEBUG
  506. if ((p9_debug_level & P9_DEBUG_PKT) == P9_DEBUG_PKT)
  507. p9pdu_dump(0, pdu);
  508. #endif
  509. P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size,
  510. pdu->id, pdu->tag);
  511. return err;
  512. }
  513. void p9pdu_reset(struct p9_fcall *pdu)
  514. {
  515. pdu->offset = 0;
  516. pdu->size = 0;
  517. }