protocol.c 13 KB

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