protocol.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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/kernel.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/slab.h>
  32. #include <linux/sched.h>
  33. #include <linux/stddef.h>
  34. #include <linux/types.h>
  35. #include <net/9p/9p.h>
  36. #include <net/9p/client.h>
  37. #include "protocol.h"
  38. #include <trace/events/9p.h>
  39. static int
  40. p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
  41. void p9stat_free(struct p9_wstat *stbuf)
  42. {
  43. kfree(stbuf->name);
  44. kfree(stbuf->uid);
  45. kfree(stbuf->gid);
  46. kfree(stbuf->muid);
  47. kfree(stbuf->extension);
  48. }
  49. EXPORT_SYMBOL(p9stat_free);
  50. size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
  51. {
  52. size_t len = min(pdu->size - pdu->offset, size);
  53. memcpy(data, &pdu->sdata[pdu->offset], len);
  54. pdu->offset += len;
  55. return size - len;
  56. }
  57. static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
  58. {
  59. size_t len = min(pdu->capacity - pdu->size, size);
  60. memcpy(&pdu->sdata[pdu->size], data, len);
  61. pdu->size += len;
  62. return size - len;
  63. }
  64. static size_t
  65. pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size)
  66. {
  67. size_t len = min(pdu->capacity - pdu->size, size);
  68. if (copy_from_user(&pdu->sdata[pdu->size], udata, len))
  69. len = 0;
  70. pdu->size += len;
  71. return size - len;
  72. }
  73. /*
  74. b - int8_t
  75. w - int16_t
  76. d - int32_t
  77. q - int64_t
  78. s - string
  79. S - stat
  80. Q - qid
  81. D - data blob (int32_t size followed by void *, results are not freed)
  82. T - array of strings (int16_t count, followed by strings)
  83. R - array of qids (int16_t count, followed by qids)
  84. A - stat for 9p2000.L (p9_stat_dotl)
  85. ? - if optional = 1, continue parsing
  86. */
  87. static int
  88. p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
  89. va_list ap)
  90. {
  91. const char *ptr;
  92. int errcode = 0;
  93. for (ptr = fmt; *ptr; ptr++) {
  94. switch (*ptr) {
  95. case 'b':{
  96. int8_t *val = va_arg(ap, int8_t *);
  97. if (pdu_read(pdu, val, sizeof(*val))) {
  98. errcode = -EFAULT;
  99. break;
  100. }
  101. }
  102. break;
  103. case 'w':{
  104. int16_t *val = va_arg(ap, int16_t *);
  105. __le16 le_val;
  106. if (pdu_read(pdu, &le_val, sizeof(le_val))) {
  107. errcode = -EFAULT;
  108. break;
  109. }
  110. *val = le16_to_cpu(le_val);
  111. }
  112. break;
  113. case 'd':{
  114. int32_t *val = va_arg(ap, int32_t *);
  115. __le32 le_val;
  116. if (pdu_read(pdu, &le_val, sizeof(le_val))) {
  117. errcode = -EFAULT;
  118. break;
  119. }
  120. *val = le32_to_cpu(le_val);
  121. }
  122. break;
  123. case 'q':{
  124. int64_t *val = va_arg(ap, int64_t *);
  125. __le64 le_val;
  126. if (pdu_read(pdu, &le_val, sizeof(le_val))) {
  127. errcode = -EFAULT;
  128. break;
  129. }
  130. *val = le64_to_cpu(le_val);
  131. }
  132. break;
  133. case 's':{
  134. char **sptr = va_arg(ap, char **);
  135. uint16_t len;
  136. errcode = p9pdu_readf(pdu, proto_version,
  137. "w", &len);
  138. if (errcode)
  139. break;
  140. *sptr = kmalloc(len + 1, GFP_NOFS);
  141. if (*sptr == NULL) {
  142. errcode = -EFAULT;
  143. break;
  144. }
  145. if (pdu_read(pdu, *sptr, len)) {
  146. errcode = -EFAULT;
  147. kfree(*sptr);
  148. *sptr = NULL;
  149. } else
  150. (*sptr)[len] = 0;
  151. }
  152. break;
  153. case 'Q':{
  154. struct p9_qid *qid =
  155. va_arg(ap, struct p9_qid *);
  156. errcode = p9pdu_readf(pdu, proto_version, "bdq",
  157. &qid->type, &qid->version,
  158. &qid->path);
  159. }
  160. break;
  161. case 'S':{
  162. struct p9_wstat *stbuf =
  163. va_arg(ap, struct p9_wstat *);
  164. memset(stbuf, 0, sizeof(struct p9_wstat));
  165. stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
  166. -1;
  167. errcode =
  168. p9pdu_readf(pdu, proto_version,
  169. "wwdQdddqssss?sddd",
  170. &stbuf->size, &stbuf->type,
  171. &stbuf->dev, &stbuf->qid,
  172. &stbuf->mode, &stbuf->atime,
  173. &stbuf->mtime, &stbuf->length,
  174. &stbuf->name, &stbuf->uid,
  175. &stbuf->gid, &stbuf->muid,
  176. &stbuf->extension,
  177. &stbuf->n_uid, &stbuf->n_gid,
  178. &stbuf->n_muid);
  179. if (errcode)
  180. p9stat_free(stbuf);
  181. }
  182. break;
  183. case 'D':{
  184. uint32_t *count = va_arg(ap, uint32_t *);
  185. void **data = va_arg(ap, void **);
  186. errcode =
  187. p9pdu_readf(pdu, proto_version, "d", count);
  188. if (!errcode) {
  189. *count =
  190. min_t(uint32_t, *count,
  191. pdu->size - pdu->offset);
  192. *data = &pdu->sdata[pdu->offset];
  193. }
  194. }
  195. break;
  196. case 'T':{
  197. uint16_t *nwname = va_arg(ap, uint16_t *);
  198. char ***wnames = va_arg(ap, char ***);
  199. errcode = p9pdu_readf(pdu, proto_version,
  200. "w", nwname);
  201. if (!errcode) {
  202. *wnames =
  203. kmalloc(sizeof(char *) * *nwname,
  204. GFP_NOFS);
  205. if (!*wnames)
  206. errcode = -ENOMEM;
  207. }
  208. if (!errcode) {
  209. int i;
  210. for (i = 0; i < *nwname; i++) {
  211. errcode =
  212. p9pdu_readf(pdu,
  213. proto_version,
  214. "s",
  215. &(*wnames)[i]);
  216. if (errcode)
  217. break;
  218. }
  219. }
  220. if (errcode) {
  221. if (*wnames) {
  222. int i;
  223. for (i = 0; i < *nwname; i++)
  224. kfree((*wnames)[i]);
  225. }
  226. kfree(*wnames);
  227. *wnames = NULL;
  228. }
  229. }
  230. break;
  231. case 'R':{
  232. int16_t *nwqid = va_arg(ap, int16_t *);
  233. struct p9_qid **wqids =
  234. va_arg(ap, struct p9_qid **);
  235. *wqids = NULL;
  236. errcode =
  237. p9pdu_readf(pdu, proto_version, "w", nwqid);
  238. if (!errcode) {
  239. *wqids =
  240. kmalloc(*nwqid *
  241. sizeof(struct p9_qid),
  242. GFP_NOFS);
  243. if (*wqids == NULL)
  244. errcode = -ENOMEM;
  245. }
  246. if (!errcode) {
  247. int i;
  248. for (i = 0; i < *nwqid; i++) {
  249. errcode =
  250. p9pdu_readf(pdu,
  251. proto_version,
  252. "Q",
  253. &(*wqids)[i]);
  254. if (errcode)
  255. break;
  256. }
  257. }
  258. if (errcode) {
  259. kfree(*wqids);
  260. *wqids = NULL;
  261. }
  262. }
  263. break;
  264. case 'A': {
  265. struct p9_stat_dotl *stbuf =
  266. va_arg(ap, struct p9_stat_dotl *);
  267. memset(stbuf, 0, sizeof(struct p9_stat_dotl));
  268. errcode =
  269. p9pdu_readf(pdu, proto_version,
  270. "qQdddqqqqqqqqqqqqqqq",
  271. &stbuf->st_result_mask,
  272. &stbuf->qid,
  273. &stbuf->st_mode,
  274. &stbuf->st_uid, &stbuf->st_gid,
  275. &stbuf->st_nlink,
  276. &stbuf->st_rdev, &stbuf->st_size,
  277. &stbuf->st_blksize, &stbuf->st_blocks,
  278. &stbuf->st_atime_sec,
  279. &stbuf->st_atime_nsec,
  280. &stbuf->st_mtime_sec,
  281. &stbuf->st_mtime_nsec,
  282. &stbuf->st_ctime_sec,
  283. &stbuf->st_ctime_nsec,
  284. &stbuf->st_btime_sec,
  285. &stbuf->st_btime_nsec,
  286. &stbuf->st_gen,
  287. &stbuf->st_data_version);
  288. }
  289. break;
  290. case '?':
  291. if ((proto_version != p9_proto_2000u) &&
  292. (proto_version != p9_proto_2000L))
  293. return 0;
  294. break;
  295. default:
  296. BUG();
  297. break;
  298. }
  299. if (errcode)
  300. break;
  301. }
  302. return errcode;
  303. }
  304. int
  305. p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
  306. va_list ap)
  307. {
  308. const char *ptr;
  309. int errcode = 0;
  310. for (ptr = fmt; *ptr; ptr++) {
  311. switch (*ptr) {
  312. case 'b':{
  313. int8_t val = va_arg(ap, int);
  314. if (pdu_write(pdu, &val, sizeof(val)))
  315. errcode = -EFAULT;
  316. }
  317. break;
  318. case 'w':{
  319. __le16 val = cpu_to_le16(va_arg(ap, int));
  320. if (pdu_write(pdu, &val, sizeof(val)))
  321. errcode = -EFAULT;
  322. }
  323. break;
  324. case 'd':{
  325. __le32 val = cpu_to_le32(va_arg(ap, int32_t));
  326. if (pdu_write(pdu, &val, sizeof(val)))
  327. errcode = -EFAULT;
  328. }
  329. break;
  330. case 'q':{
  331. __le64 val = cpu_to_le64(va_arg(ap, int64_t));
  332. if (pdu_write(pdu, &val, sizeof(val)))
  333. errcode = -EFAULT;
  334. }
  335. break;
  336. case 's':{
  337. const char *sptr = va_arg(ap, const char *);
  338. uint16_t len = 0;
  339. if (sptr)
  340. len = min_t(uint16_t, strlen(sptr),
  341. USHRT_MAX);
  342. errcode = p9pdu_writef(pdu, proto_version,
  343. "w", len);
  344. if (!errcode && pdu_write(pdu, sptr, 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, proto_version, "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, proto_version,
  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. uint32_t count = va_arg(ap, uint32_t);
  373. const void *data = va_arg(ap, const void *);
  374. errcode = p9pdu_writef(pdu, proto_version, "d",
  375. 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 __user *);
  384. errcode = p9pdu_writef(pdu, proto_version, "d",
  385. count);
  386. if (!errcode && pdu_write_u(pdu, udata, count))
  387. errcode = -EFAULT;
  388. }
  389. break;
  390. case 'T':{
  391. uint16_t nwname = va_arg(ap, int);
  392. const char **wnames = va_arg(ap, const char **);
  393. errcode = p9pdu_writef(pdu, proto_version, "w",
  394. nwname);
  395. if (!errcode) {
  396. int i;
  397. for (i = 0; i < nwname; i++) {
  398. errcode =
  399. p9pdu_writef(pdu,
  400. proto_version,
  401. "s",
  402. wnames[i]);
  403. if (errcode)
  404. break;
  405. }
  406. }
  407. }
  408. break;
  409. case 'R':{
  410. int16_t nwqid = va_arg(ap, int);
  411. struct p9_qid *wqids =
  412. va_arg(ap, struct p9_qid *);
  413. errcode = p9pdu_writef(pdu, proto_version, "w",
  414. nwqid);
  415. if (!errcode) {
  416. int i;
  417. for (i = 0; i < nwqid; i++) {
  418. errcode =
  419. p9pdu_writef(pdu,
  420. proto_version,
  421. "Q",
  422. &wqids[i]);
  423. if (errcode)
  424. break;
  425. }
  426. }
  427. }
  428. break;
  429. case 'I':{
  430. struct p9_iattr_dotl *p9attr = va_arg(ap,
  431. struct p9_iattr_dotl *);
  432. errcode = p9pdu_writef(pdu, proto_version,
  433. "ddddqqqqq",
  434. p9attr->valid,
  435. p9attr->mode,
  436. p9attr->uid,
  437. p9attr->gid,
  438. p9attr->size,
  439. p9attr->atime_sec,
  440. p9attr->atime_nsec,
  441. p9attr->mtime_sec,
  442. p9attr->mtime_nsec);
  443. }
  444. break;
  445. case '?':
  446. if ((proto_version != p9_proto_2000u) &&
  447. (proto_version != p9_proto_2000L))
  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(struct p9_client *clnt, char *buf, int len, struct p9_wstat *st)
  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, clnt->proto_version, "S", st);
  487. if (ret) {
  488. p9_debug(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
  489. trace_9p_protocol_dump(clnt, &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. pdu->id = type;
  497. return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
  498. }
  499. int p9pdu_finalize(struct p9_client *clnt, struct p9_fcall *pdu)
  500. {
  501. int size = pdu->size;
  502. int err;
  503. pdu->size = 0;
  504. err = p9pdu_writef(pdu, 0, "d", size);
  505. pdu->size = size;
  506. trace_9p_protocol_dump(clnt, pdu);
  507. p9_debug(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n",
  508. pdu->size, pdu->id, pdu->tag);
  509. return err;
  510. }
  511. void p9pdu_reset(struct p9_fcall *pdu)
  512. {
  513. pdu->offset = 0;
  514. pdu->size = 0;
  515. }
  516. int p9dirent_read(struct p9_client *clnt, char *buf, int len,
  517. struct p9_dirent *dirent)
  518. {
  519. struct p9_fcall fake_pdu;
  520. int ret;
  521. char *nameptr;
  522. fake_pdu.size = len;
  523. fake_pdu.capacity = len;
  524. fake_pdu.sdata = buf;
  525. fake_pdu.offset = 0;
  526. ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "Qqbs", &dirent->qid,
  527. &dirent->d_off, &dirent->d_type, &nameptr);
  528. if (ret) {
  529. p9_debug(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
  530. trace_9p_protocol_dump(clnt, &fake_pdu);
  531. goto out;
  532. }
  533. strcpy(dirent->d_name, nameptr);
  534. kfree(nameptr);
  535. out:
  536. return fake_pdu.offset;
  537. }
  538. EXPORT_SYMBOL(p9dirent_read);