protocol.c 11 KB

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