protocol.c 14 KB

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