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_full(&pdu->sdata[pdu->size], len, &i))
  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 = -ENOMEM;
  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_array(*nwname,
  228. sizeof(char *),
  229. GFP_NOFS);
  230. if (!*wnames)
  231. errcode = -ENOMEM;
  232. }
  233. if (!errcode) {
  234. int i;
  235. for (i = 0; i < *nwname; i++) {
  236. errcode =
  237. p9pdu_readf(pdu,
  238. proto_version,
  239. "s",
  240. &(*wnames)[i]);
  241. if (errcode)
  242. break;
  243. }
  244. }
  245. if (errcode) {
  246. if (*wnames) {
  247. int i;
  248. for (i = 0; i < *nwname; i++)
  249. kfree((*wnames)[i]);
  250. }
  251. kfree(*wnames);
  252. *wnames = NULL;
  253. }
  254. }
  255. break;
  256. case 'R':{
  257. uint16_t *nwqid = va_arg(ap, uint16_t *);
  258. struct p9_qid **wqids =
  259. va_arg(ap, struct p9_qid **);
  260. *wqids = NULL;
  261. errcode =
  262. p9pdu_readf(pdu, proto_version, "w", nwqid);
  263. if (!errcode) {
  264. *wqids =
  265. kmalloc_array(*nwqid,
  266. sizeof(struct p9_qid),
  267. GFP_NOFS);
  268. if (*wqids == NULL)
  269. errcode = -ENOMEM;
  270. }
  271. if (!errcode) {
  272. int i;
  273. for (i = 0; i < *nwqid; i++) {
  274. errcode =
  275. p9pdu_readf(pdu,
  276. proto_version,
  277. "Q",
  278. &(*wqids)[i]);
  279. if (errcode)
  280. break;
  281. }
  282. }
  283. if (errcode) {
  284. kfree(*wqids);
  285. *wqids = NULL;
  286. }
  287. }
  288. break;
  289. case 'A': {
  290. struct p9_stat_dotl *stbuf =
  291. va_arg(ap, struct p9_stat_dotl *);
  292. memset(stbuf, 0, sizeof(struct p9_stat_dotl));
  293. errcode =
  294. p9pdu_readf(pdu, proto_version,
  295. "qQdugqqqqqqqqqqqqqqq",
  296. &stbuf->st_result_mask,
  297. &stbuf->qid,
  298. &stbuf->st_mode,
  299. &stbuf->st_uid, &stbuf->st_gid,
  300. &stbuf->st_nlink,
  301. &stbuf->st_rdev, &stbuf->st_size,
  302. &stbuf->st_blksize, &stbuf->st_blocks,
  303. &stbuf->st_atime_sec,
  304. &stbuf->st_atime_nsec,
  305. &stbuf->st_mtime_sec,
  306. &stbuf->st_mtime_nsec,
  307. &stbuf->st_ctime_sec,
  308. &stbuf->st_ctime_nsec,
  309. &stbuf->st_btime_sec,
  310. &stbuf->st_btime_nsec,
  311. &stbuf->st_gen,
  312. &stbuf->st_data_version);
  313. }
  314. break;
  315. case '?':
  316. if ((proto_version != p9_proto_2000u) &&
  317. (proto_version != p9_proto_2000L))
  318. return 0;
  319. break;
  320. default:
  321. BUG();
  322. break;
  323. }
  324. if (errcode)
  325. break;
  326. }
  327. return errcode;
  328. }
  329. int
  330. p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
  331. va_list ap)
  332. {
  333. const char *ptr;
  334. int errcode = 0;
  335. for (ptr = fmt; *ptr; ptr++) {
  336. switch (*ptr) {
  337. case 'b':{
  338. int8_t val = va_arg(ap, int);
  339. if (pdu_write(pdu, &val, sizeof(val)))
  340. errcode = -EFAULT;
  341. }
  342. break;
  343. case 'w':{
  344. __le16 val = cpu_to_le16(va_arg(ap, int));
  345. if (pdu_write(pdu, &val, sizeof(val)))
  346. errcode = -EFAULT;
  347. }
  348. break;
  349. case 'd':{
  350. __le32 val = cpu_to_le32(va_arg(ap, int32_t));
  351. if (pdu_write(pdu, &val, sizeof(val)))
  352. errcode = -EFAULT;
  353. }
  354. break;
  355. case 'q':{
  356. __le64 val = cpu_to_le64(va_arg(ap, int64_t));
  357. if (pdu_write(pdu, &val, sizeof(val)))
  358. errcode = -EFAULT;
  359. }
  360. break;
  361. case 's':{
  362. const char *sptr = va_arg(ap, const char *);
  363. uint16_t len = 0;
  364. if (sptr)
  365. len = min_t(size_t, strlen(sptr),
  366. USHRT_MAX);
  367. errcode = p9pdu_writef(pdu, proto_version,
  368. "w", len);
  369. if (!errcode && pdu_write(pdu, sptr, len))
  370. errcode = -EFAULT;
  371. }
  372. break;
  373. case 'u': {
  374. kuid_t uid = va_arg(ap, kuid_t);
  375. __le32 val = cpu_to_le32(
  376. from_kuid(&init_user_ns, uid));
  377. if (pdu_write(pdu, &val, sizeof(val)))
  378. errcode = -EFAULT;
  379. } break;
  380. case 'g': {
  381. kgid_t gid = va_arg(ap, kgid_t);
  382. __le32 val = cpu_to_le32(
  383. from_kgid(&init_user_ns, gid));
  384. if (pdu_write(pdu, &val, sizeof(val)))
  385. errcode = -EFAULT;
  386. } break;
  387. case 'Q':{
  388. const struct p9_qid *qid =
  389. va_arg(ap, const struct p9_qid *);
  390. errcode =
  391. p9pdu_writef(pdu, proto_version, "bdq",
  392. qid->type, qid->version,
  393. qid->path);
  394. } break;
  395. case 'S':{
  396. const struct p9_wstat *stbuf =
  397. va_arg(ap, const struct p9_wstat *);
  398. errcode =
  399. p9pdu_writef(pdu, proto_version,
  400. "wwdQdddqssss?sugu",
  401. stbuf->size, stbuf->type,
  402. stbuf->dev, &stbuf->qid,
  403. stbuf->mode, stbuf->atime,
  404. stbuf->mtime, stbuf->length,
  405. stbuf->name, stbuf->uid,
  406. stbuf->gid, stbuf->muid,
  407. stbuf->extension, stbuf->n_uid,
  408. stbuf->n_gid, stbuf->n_muid);
  409. } break;
  410. case 'V':{
  411. uint32_t count = va_arg(ap, uint32_t);
  412. struct iov_iter *from =
  413. va_arg(ap, struct iov_iter *);
  414. errcode = p9pdu_writef(pdu, proto_version, "d",
  415. count);
  416. if (!errcode && pdu_write_u(pdu, from, count))
  417. errcode = -EFAULT;
  418. }
  419. break;
  420. case 'T':{
  421. uint16_t nwname = va_arg(ap, int);
  422. const char **wnames = va_arg(ap, const char **);
  423. errcode = p9pdu_writef(pdu, proto_version, "w",
  424. nwname);
  425. if (!errcode) {
  426. int i;
  427. for (i = 0; i < nwname; i++) {
  428. errcode =
  429. p9pdu_writef(pdu,
  430. proto_version,
  431. "s",
  432. wnames[i]);
  433. if (errcode)
  434. break;
  435. }
  436. }
  437. }
  438. break;
  439. case 'R':{
  440. uint16_t nwqid = va_arg(ap, int);
  441. struct p9_qid *wqids =
  442. va_arg(ap, struct p9_qid *);
  443. errcode = p9pdu_writef(pdu, proto_version, "w",
  444. nwqid);
  445. if (!errcode) {
  446. int i;
  447. for (i = 0; i < nwqid; i++) {
  448. errcode =
  449. p9pdu_writef(pdu,
  450. proto_version,
  451. "Q",
  452. &wqids[i]);
  453. if (errcode)
  454. break;
  455. }
  456. }
  457. }
  458. break;
  459. case 'I':{
  460. struct p9_iattr_dotl *p9attr = va_arg(ap,
  461. struct p9_iattr_dotl *);
  462. errcode = p9pdu_writef(pdu, proto_version,
  463. "ddugqqqqq",
  464. p9attr->valid,
  465. p9attr->mode,
  466. p9attr->uid,
  467. p9attr->gid,
  468. p9attr->size,
  469. p9attr->atime_sec,
  470. p9attr->atime_nsec,
  471. p9attr->mtime_sec,
  472. p9attr->mtime_nsec);
  473. }
  474. break;
  475. case '?':
  476. if ((proto_version != p9_proto_2000u) &&
  477. (proto_version != p9_proto_2000L))
  478. return 0;
  479. break;
  480. default:
  481. BUG();
  482. break;
  483. }
  484. if (errcode)
  485. break;
  486. }
  487. return errcode;
  488. }
  489. int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
  490. {
  491. va_list ap;
  492. int ret;
  493. va_start(ap, fmt);
  494. ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
  495. va_end(ap);
  496. return ret;
  497. }
  498. static int
  499. p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
  500. {
  501. va_list ap;
  502. int ret;
  503. va_start(ap, fmt);
  504. ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
  505. va_end(ap);
  506. return ret;
  507. }
  508. int p9stat_read(struct p9_client *clnt, char *buf, int len, struct p9_wstat *st)
  509. {
  510. struct p9_fcall fake_pdu;
  511. int ret;
  512. fake_pdu.size = len;
  513. fake_pdu.capacity = len;
  514. fake_pdu.sdata = buf;
  515. fake_pdu.offset = 0;
  516. ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "S", st);
  517. if (ret) {
  518. p9_debug(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
  519. trace_9p_protocol_dump(clnt, &fake_pdu);
  520. }
  521. return ret;
  522. }
  523. EXPORT_SYMBOL(p9stat_read);
  524. int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
  525. {
  526. pdu->id = type;
  527. return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
  528. }
  529. int p9pdu_finalize(struct p9_client *clnt, struct p9_fcall *pdu)
  530. {
  531. int size = pdu->size;
  532. int err;
  533. pdu->size = 0;
  534. err = p9pdu_writef(pdu, 0, "d", size);
  535. pdu->size = size;
  536. trace_9p_protocol_dump(clnt, pdu);
  537. p9_debug(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n",
  538. pdu->size, pdu->id, pdu->tag);
  539. return err;
  540. }
  541. void p9pdu_reset(struct p9_fcall *pdu)
  542. {
  543. pdu->offset = 0;
  544. pdu->size = 0;
  545. }
  546. int p9dirent_read(struct p9_client *clnt, char *buf, int len,
  547. struct p9_dirent *dirent)
  548. {
  549. struct p9_fcall fake_pdu;
  550. int ret;
  551. char *nameptr;
  552. fake_pdu.size = len;
  553. fake_pdu.capacity = len;
  554. fake_pdu.sdata = buf;
  555. fake_pdu.offset = 0;
  556. ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "Qqbs", &dirent->qid,
  557. &dirent->d_off, &dirent->d_type, &nameptr);
  558. if (ret) {
  559. p9_debug(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
  560. trace_9p_protocol_dump(clnt, &fake_pdu);
  561. goto out;
  562. }
  563. strcpy(dirent->d_name, nameptr);
  564. kfree(nameptr);
  565. out:
  566. return fake_pdu.offset;
  567. }
  568. EXPORT_SYMBOL(p9dirent_read);