PageRenderTime 27ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/net/9p/protocol.c

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