protocol.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * 9P Protocol Support Code
  4. *
  5. * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
  6. *
  7. * Base on code from Anthony Liguori <aliguori@us.ibm.com>
  8. * Copyright (C) 2008 by IBM, Corp.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/errno.h>
  12. #include <linux/kernel.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/slab.h>
  15. #include <linux/sched.h>
  16. #include <linux/stddef.h>
  17. #include <linux/types.h>
  18. #include <linux/uio.h>
  19. #include <net/9p/9p.h>
  20. #include <net/9p/client.h>
  21. #include "protocol.h"
  22. #include <trace/events/9p.h>
  23. /* len[2] text[len] */
  24. #define P9_STRLEN(s) \
  25. (2 + min_t(size_t, s ? strlen(s) : 0, USHRT_MAX))
  26. /**
  27. * p9_msg_buf_size - Returns a buffer size sufficiently large to hold the
  28. * intended 9p message.
  29. * @c: client
  30. * @type: message type
  31. * @fmt: format template for assembling request message
  32. * (see p9pdu_vwritef)
  33. * @ap: variable arguments to be fed to passed format template
  34. * (see p9pdu_vwritef)
  35. *
  36. * Note: Even for response types (P9_R*) the format template and variable
  37. * arguments must always be for the originating request type (P9_T*).
  38. */
  39. size_t p9_msg_buf_size(struct p9_client *c, enum p9_msg_t type,
  40. const char *fmt, va_list ap)
  41. {
  42. /* size[4] type[1] tag[2] */
  43. const int hdr = 4 + 1 + 2;
  44. /* ename[s] errno[4] */
  45. const int rerror_size = hdr + P9_ERRMAX + 4;
  46. /* ecode[4] */
  47. const int rlerror_size = hdr + 4;
  48. const int err_size =
  49. c->proto_version == p9_proto_2000L ? rlerror_size : rerror_size;
  50. static_assert(NAME_MAX <= 4*1024, "p9_msg_buf_size() currently assumes "
  51. "a max. allowed directory entry name length of 4k");
  52. switch (type) {
  53. /* message types not used at all */
  54. case P9_TERROR:
  55. case P9_TLERROR:
  56. case P9_TAUTH:
  57. case P9_RAUTH:
  58. BUG();
  59. /* variable length & potentially large message types */
  60. case P9_TATTACH:
  61. BUG_ON(strcmp("ddss?u", fmt));
  62. va_arg(ap, int32_t);
  63. va_arg(ap, int32_t);
  64. {
  65. const char *uname = va_arg(ap, const char *);
  66. const char *aname = va_arg(ap, const char *);
  67. /* fid[4] afid[4] uname[s] aname[s] n_uname[4] */
  68. return hdr + 4 + 4 + P9_STRLEN(uname) + P9_STRLEN(aname) + 4;
  69. }
  70. case P9_TWALK:
  71. BUG_ON(strcmp("ddT", fmt));
  72. va_arg(ap, int32_t);
  73. va_arg(ap, int32_t);
  74. {
  75. uint i, nwname = va_arg(ap, int);
  76. size_t wname_all;
  77. const char **wnames = va_arg(ap, const char **);
  78. for (i = 0, wname_all = 0; i < nwname; ++i) {
  79. wname_all += P9_STRLEN(wnames[i]);
  80. }
  81. /* fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
  82. return hdr + 4 + 4 + 2 + wname_all;
  83. }
  84. case P9_RWALK:
  85. BUG_ON(strcmp("ddT", fmt));
  86. va_arg(ap, int32_t);
  87. va_arg(ap, int32_t);
  88. {
  89. uint nwname = va_arg(ap, int);
  90. /* nwqid[2] nwqid*(wqid[13]) */
  91. return max_t(size_t, hdr + 2 + nwname * 13, err_size);
  92. }
  93. case P9_TCREATE:
  94. BUG_ON(strcmp("dsdb?s", fmt));
  95. va_arg(ap, int32_t);
  96. {
  97. const char *name = va_arg(ap, const char *);
  98. if (c->proto_version == p9_proto_legacy) {
  99. /* fid[4] name[s] perm[4] mode[1] */
  100. return hdr + 4 + P9_STRLEN(name) + 4 + 1;
  101. } else {
  102. va_arg(ap, int32_t);
  103. va_arg(ap, int);
  104. {
  105. const char *ext = va_arg(ap, const char *);
  106. /* fid[4] name[s] perm[4] mode[1] extension[s] */
  107. return hdr + 4 + P9_STRLEN(name) + 4 + 1 + P9_STRLEN(ext);
  108. }
  109. }
  110. }
  111. case P9_TLCREATE:
  112. BUG_ON(strcmp("dsddg", fmt));
  113. va_arg(ap, int32_t);
  114. {
  115. const char *name = va_arg(ap, const char *);
  116. /* fid[4] name[s] flags[4] mode[4] gid[4] */
  117. return hdr + 4 + P9_STRLEN(name) + 4 + 4 + 4;
  118. }
  119. case P9_RREAD:
  120. case P9_RREADDIR:
  121. BUG_ON(strcmp("dqd", fmt));
  122. va_arg(ap, int32_t);
  123. va_arg(ap, int64_t);
  124. {
  125. const int32_t count = va_arg(ap, int32_t);
  126. /* count[4] data[count] */
  127. return max_t(size_t, hdr + 4 + count, err_size);
  128. }
  129. case P9_TWRITE:
  130. BUG_ON(strcmp("dqV", fmt));
  131. va_arg(ap, int32_t);
  132. va_arg(ap, int64_t);
  133. {
  134. const int32_t count = va_arg(ap, int32_t);
  135. /* fid[4] offset[8] count[4] data[count] */
  136. return hdr + 4 + 8 + 4 + count;
  137. }
  138. case P9_TRENAMEAT:
  139. BUG_ON(strcmp("dsds", fmt));
  140. va_arg(ap, int32_t);
  141. {
  142. const char *oldname, *newname;
  143. oldname = va_arg(ap, const char *);
  144. va_arg(ap, int32_t);
  145. newname = va_arg(ap, const char *);
  146. /* olddirfid[4] oldname[s] newdirfid[4] newname[s] */
  147. return hdr + 4 + P9_STRLEN(oldname) + 4 + P9_STRLEN(newname);
  148. }
  149. case P9_TSYMLINK:
  150. BUG_ON(strcmp("dssg", fmt));
  151. va_arg(ap, int32_t);
  152. {
  153. const char *name = va_arg(ap, const char *);
  154. const char *symtgt = va_arg(ap, const char *);
  155. /* fid[4] name[s] symtgt[s] gid[4] */
  156. return hdr + 4 + P9_STRLEN(name) + P9_STRLEN(symtgt) + 4;
  157. }
  158. case P9_RERROR:
  159. return rerror_size;
  160. case P9_RLERROR:
  161. return rlerror_size;
  162. /* small message types */
  163. case P9_TWSTAT:
  164. case P9_RSTAT:
  165. case P9_RREADLINK:
  166. case P9_TXATTRWALK:
  167. case P9_TXATTRCREATE:
  168. case P9_TLINK:
  169. case P9_TMKDIR:
  170. case P9_TMKNOD:
  171. case P9_TRENAME:
  172. case P9_TUNLINKAT:
  173. case P9_TLOCK:
  174. return 8 * 1024;
  175. /* tiny message types */
  176. default:
  177. return 4 * 1024;
  178. }
  179. }
  180. static int
  181. p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
  182. void p9stat_free(struct p9_wstat *stbuf)
  183. {
  184. kfree(stbuf->name);
  185. stbuf->name = NULL;
  186. kfree(stbuf->uid);
  187. stbuf->uid = NULL;
  188. kfree(stbuf->gid);
  189. stbuf->gid = NULL;
  190. kfree(stbuf->muid);
  191. stbuf->muid = NULL;
  192. kfree(stbuf->extension);
  193. stbuf->extension = NULL;
  194. }
  195. EXPORT_SYMBOL(p9stat_free);
  196. size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
  197. {
  198. size_t len = min(pdu->size - pdu->offset, size);
  199. memcpy(data, &pdu->sdata[pdu->offset], len);
  200. pdu->offset += len;
  201. return size - len;
  202. }
  203. static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
  204. {
  205. size_t len = min(pdu->capacity - pdu->size, size);
  206. memcpy(&pdu->sdata[pdu->size], data, len);
  207. pdu->size += len;
  208. return size - len;
  209. }
  210. static size_t
  211. pdu_write_u(struct p9_fcall *pdu, struct iov_iter *from, size_t size)
  212. {
  213. size_t len = min(pdu->capacity - pdu->size, size);
  214. if (!copy_from_iter_full(&pdu->sdata[pdu->size], len, from))
  215. len = 0;
  216. pdu->size += len;
  217. return size - len;
  218. }
  219. /* b - int8_t
  220. * w - int16_t
  221. * d - int32_t
  222. * q - int64_t
  223. * s - string
  224. * u - numeric uid
  225. * g - numeric gid
  226. * S - stat
  227. * Q - qid
  228. * D - data blob (int32_t size followed by void *, results are not freed)
  229. * T - array of strings (int16_t count, followed by strings)
  230. * R - array of qids (int16_t count, followed by qids)
  231. * A - stat for 9p2000.L (p9_stat_dotl)
  232. * ? - if optional = 1, continue parsing
  233. */
  234. static int
  235. p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
  236. va_list ap)
  237. {
  238. const char *ptr;
  239. int errcode = 0;
  240. for (ptr = fmt; *ptr; ptr++) {
  241. switch (*ptr) {
  242. case 'b':{
  243. int8_t *val = va_arg(ap, int8_t *);
  244. if (pdu_read(pdu, val, sizeof(*val))) {
  245. errcode = -EFAULT;
  246. break;
  247. }
  248. }
  249. break;
  250. case 'w':{
  251. int16_t *val = va_arg(ap, int16_t *);
  252. __le16 le_val;
  253. if (pdu_read(pdu, &le_val, sizeof(le_val))) {
  254. errcode = -EFAULT;
  255. break;
  256. }
  257. *val = le16_to_cpu(le_val);
  258. }
  259. break;
  260. case 'd':{
  261. int32_t *val = va_arg(ap, int32_t *);
  262. __le32 le_val;
  263. if (pdu_read(pdu, &le_val, sizeof(le_val))) {
  264. errcode = -EFAULT;
  265. break;
  266. }
  267. *val = le32_to_cpu(le_val);
  268. }
  269. break;
  270. case 'q':{
  271. int64_t *val = va_arg(ap, int64_t *);
  272. __le64 le_val;
  273. if (pdu_read(pdu, &le_val, sizeof(le_val))) {
  274. errcode = -EFAULT;
  275. break;
  276. }
  277. *val = le64_to_cpu(le_val);
  278. }
  279. break;
  280. case 's':{
  281. char **sptr = va_arg(ap, char **);
  282. uint16_t len;
  283. errcode = p9pdu_readf(pdu, proto_version,
  284. "w", &len);
  285. if (errcode)
  286. break;
  287. *sptr = kmalloc(len + 1, GFP_NOFS);
  288. if (*sptr == NULL) {
  289. errcode = -ENOMEM;
  290. break;
  291. }
  292. if (pdu_read(pdu, *sptr, len)) {
  293. errcode = -EFAULT;
  294. kfree(*sptr);
  295. *sptr = NULL;
  296. } else
  297. (*sptr)[len] = 0;
  298. }
  299. break;
  300. case 'u': {
  301. kuid_t *uid = va_arg(ap, kuid_t *);
  302. __le32 le_val;
  303. if (pdu_read(pdu, &le_val, sizeof(le_val))) {
  304. errcode = -EFAULT;
  305. break;
  306. }
  307. *uid = make_kuid(&init_user_ns,
  308. le32_to_cpu(le_val));
  309. } break;
  310. case 'g': {
  311. kgid_t *gid = va_arg(ap, kgid_t *);
  312. __le32 le_val;
  313. if (pdu_read(pdu, &le_val, sizeof(le_val))) {
  314. errcode = -EFAULT;
  315. break;
  316. }
  317. *gid = make_kgid(&init_user_ns,
  318. le32_to_cpu(le_val));
  319. } break;
  320. case 'Q':{
  321. struct p9_qid *qid =
  322. va_arg(ap, struct p9_qid *);
  323. errcode = p9pdu_readf(pdu, proto_version, "bdq",
  324. &qid->type, &qid->version,
  325. &qid->path);
  326. }
  327. break;
  328. case 'S':{
  329. struct p9_wstat *stbuf =
  330. va_arg(ap, struct p9_wstat *);
  331. memset(stbuf, 0, sizeof(struct p9_wstat));
  332. stbuf->n_uid = stbuf->n_muid = INVALID_UID;
  333. stbuf->n_gid = INVALID_GID;
  334. errcode =
  335. p9pdu_readf(pdu, proto_version,
  336. "wwdQdddqssss?sugu",
  337. &stbuf->size, &stbuf->type,
  338. &stbuf->dev, &stbuf->qid,
  339. &stbuf->mode, &stbuf->atime,
  340. &stbuf->mtime, &stbuf->length,
  341. &stbuf->name, &stbuf->uid,
  342. &stbuf->gid, &stbuf->muid,
  343. &stbuf->extension,
  344. &stbuf->n_uid, &stbuf->n_gid,
  345. &stbuf->n_muid);
  346. if (errcode)
  347. p9stat_free(stbuf);
  348. }
  349. break;
  350. case 'D':{
  351. uint32_t *count = va_arg(ap, uint32_t *);
  352. void **data = va_arg(ap, void **);
  353. errcode =
  354. p9pdu_readf(pdu, proto_version, "d", count);
  355. if (!errcode) {
  356. *count =
  357. min_t(uint32_t, *count,
  358. pdu->size - pdu->offset);
  359. *data = &pdu->sdata[pdu->offset];
  360. }
  361. }
  362. break;
  363. case 'T':{
  364. uint16_t *nwname = va_arg(ap, uint16_t *);
  365. char ***wnames = va_arg(ap, char ***);
  366. *wnames = NULL;
  367. errcode = p9pdu_readf(pdu, proto_version,
  368. "w", nwname);
  369. if (!errcode) {
  370. *wnames =
  371. kmalloc_array(*nwname,
  372. sizeof(char *),
  373. GFP_NOFS);
  374. if (!*wnames)
  375. errcode = -ENOMEM;
  376. else
  377. (*wnames)[0] = NULL;
  378. }
  379. if (!errcode) {
  380. int i;
  381. for (i = 0; i < *nwname; i++) {
  382. errcode =
  383. p9pdu_readf(pdu,
  384. proto_version,
  385. "s",
  386. &(*wnames)[i]);
  387. if (errcode) {
  388. (*wnames)[i] = NULL;
  389. break;
  390. }
  391. }
  392. }
  393. if (errcode) {
  394. if (*wnames) {
  395. int i;
  396. for (i = 0; i < *nwname; i++) {
  397. if (!(*wnames)[i])
  398. break;
  399. kfree((*wnames)[i]);
  400. }
  401. kfree(*wnames);
  402. *wnames = NULL;
  403. }
  404. }
  405. }
  406. break;
  407. case 'R':{
  408. uint16_t *nwqid = va_arg(ap, uint16_t *);
  409. struct p9_qid **wqids =
  410. va_arg(ap, struct p9_qid **);
  411. *wqids = NULL;
  412. errcode =
  413. p9pdu_readf(pdu, proto_version, "w", nwqid);
  414. if (!errcode) {
  415. *wqids =
  416. kmalloc_array(*nwqid,
  417. sizeof(struct p9_qid),
  418. GFP_NOFS);
  419. if (*wqids == NULL)
  420. errcode = -ENOMEM;
  421. }
  422. if (!errcode) {
  423. int i;
  424. for (i = 0; i < *nwqid; i++) {
  425. errcode =
  426. p9pdu_readf(pdu,
  427. proto_version,
  428. "Q",
  429. &(*wqids)[i]);
  430. if (errcode)
  431. break;
  432. }
  433. }
  434. if (errcode) {
  435. kfree(*wqids);
  436. *wqids = NULL;
  437. }
  438. }
  439. break;
  440. case 'A': {
  441. struct p9_stat_dotl *stbuf =
  442. va_arg(ap, struct p9_stat_dotl *);
  443. memset(stbuf, 0, sizeof(struct p9_stat_dotl));
  444. errcode =
  445. p9pdu_readf(pdu, proto_version,
  446. "qQdugqqqqqqqqqqqqqqq",
  447. &stbuf->st_result_mask,
  448. &stbuf->qid,
  449. &stbuf->st_mode,
  450. &stbuf->st_uid, &stbuf->st_gid,
  451. &stbuf->st_nlink,
  452. &stbuf->st_rdev, &stbuf->st_size,
  453. &stbuf->st_blksize, &stbuf->st_blocks,
  454. &stbuf->st_atime_sec,
  455. &stbuf->st_atime_nsec,
  456. &stbuf->st_mtime_sec,
  457. &stbuf->st_mtime_nsec,
  458. &stbuf->st_ctime_sec,
  459. &stbuf->st_ctime_nsec,
  460. &stbuf->st_btime_sec,
  461. &stbuf->st_btime_nsec,
  462. &stbuf->st_gen,
  463. &stbuf->st_data_version);
  464. }
  465. break;
  466. case '?':
  467. if ((proto_version != p9_proto_2000u) &&
  468. (proto_version != p9_proto_2000L))
  469. return 0;
  470. break;
  471. default:
  472. BUG();
  473. break;
  474. }
  475. if (errcode)
  476. break;
  477. }
  478. return errcode;
  479. }
  480. int
  481. p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
  482. va_list ap)
  483. {
  484. const char *ptr;
  485. int errcode = 0;
  486. for (ptr = fmt; *ptr; ptr++) {
  487. switch (*ptr) {
  488. case 'b':{
  489. int8_t val = va_arg(ap, int);
  490. if (pdu_write(pdu, &val, sizeof(val)))
  491. errcode = -EFAULT;
  492. }
  493. break;
  494. case 'w':{
  495. __le16 val = cpu_to_le16(va_arg(ap, int));
  496. if (pdu_write(pdu, &val, sizeof(val)))
  497. errcode = -EFAULT;
  498. }
  499. break;
  500. case 'd':{
  501. __le32 val = cpu_to_le32(va_arg(ap, int32_t));
  502. if (pdu_write(pdu, &val, sizeof(val)))
  503. errcode = -EFAULT;
  504. }
  505. break;
  506. case 'q':{
  507. __le64 val = cpu_to_le64(va_arg(ap, int64_t));
  508. if (pdu_write(pdu, &val, sizeof(val)))
  509. errcode = -EFAULT;
  510. }
  511. break;
  512. case 's':{
  513. const char *sptr = va_arg(ap, const char *);
  514. uint16_t len = 0;
  515. if (sptr)
  516. len = min_t(size_t, strlen(sptr),
  517. USHRT_MAX);
  518. errcode = p9pdu_writef(pdu, proto_version,
  519. "w", len);
  520. if (!errcode && pdu_write(pdu, sptr, len))
  521. errcode = -EFAULT;
  522. }
  523. break;
  524. case 'u': {
  525. kuid_t uid = va_arg(ap, kuid_t);
  526. __le32 val = cpu_to_le32(
  527. from_kuid(&init_user_ns, uid));
  528. if (pdu_write(pdu, &val, sizeof(val)))
  529. errcode = -EFAULT;
  530. } break;
  531. case 'g': {
  532. kgid_t gid = va_arg(ap, kgid_t);
  533. __le32 val = cpu_to_le32(
  534. from_kgid(&init_user_ns, gid));
  535. if (pdu_write(pdu, &val, sizeof(val)))
  536. errcode = -EFAULT;
  537. } break;
  538. case 'Q':{
  539. const struct p9_qid *qid =
  540. va_arg(ap, const struct p9_qid *);
  541. errcode =
  542. p9pdu_writef(pdu, proto_version, "bdq",
  543. qid->type, qid->version,
  544. qid->path);
  545. } break;
  546. case 'S':{
  547. const struct p9_wstat *stbuf =
  548. va_arg(ap, const struct p9_wstat *);
  549. errcode =
  550. p9pdu_writef(pdu, proto_version,
  551. "wwdQdddqssss?sugu",
  552. stbuf->size, stbuf->type,
  553. stbuf->dev, &stbuf->qid,
  554. stbuf->mode, stbuf->atime,
  555. stbuf->mtime, stbuf->length,
  556. stbuf->name, stbuf->uid,
  557. stbuf->gid, stbuf->muid,
  558. stbuf->extension, stbuf->n_uid,
  559. stbuf->n_gid, stbuf->n_muid);
  560. } break;
  561. case 'V':{
  562. uint32_t count = va_arg(ap, uint32_t);
  563. struct iov_iter *from =
  564. va_arg(ap, struct iov_iter *);
  565. errcode = p9pdu_writef(pdu, proto_version, "d",
  566. count);
  567. if (!errcode && pdu_write_u(pdu, from, count))
  568. errcode = -EFAULT;
  569. }
  570. break;
  571. case 'T':{
  572. uint16_t nwname = va_arg(ap, int);
  573. const char **wnames = va_arg(ap, const char **);
  574. errcode = p9pdu_writef(pdu, proto_version, "w",
  575. nwname);
  576. if (!errcode) {
  577. int i;
  578. for (i = 0; i < nwname; i++) {
  579. errcode =
  580. p9pdu_writef(pdu,
  581. proto_version,
  582. "s",
  583. wnames[i]);
  584. if (errcode)
  585. break;
  586. }
  587. }
  588. }
  589. break;
  590. case 'R':{
  591. uint16_t nwqid = va_arg(ap, int);
  592. struct p9_qid *wqids =
  593. va_arg(ap, struct p9_qid *);
  594. errcode = p9pdu_writef(pdu, proto_version, "w",
  595. nwqid);
  596. if (!errcode) {
  597. int i;
  598. for (i = 0; i < nwqid; i++) {
  599. errcode =
  600. p9pdu_writef(pdu,
  601. proto_version,
  602. "Q",
  603. &wqids[i]);
  604. if (errcode)
  605. break;
  606. }
  607. }
  608. }
  609. break;
  610. case 'I':{
  611. struct p9_iattr_dotl *p9attr = va_arg(ap,
  612. struct p9_iattr_dotl *);
  613. errcode = p9pdu_writef(pdu, proto_version,
  614. "ddugqqqqq",
  615. p9attr->valid,
  616. p9attr->mode,
  617. p9attr->uid,
  618. p9attr->gid,
  619. p9attr->size,
  620. p9attr->atime_sec,
  621. p9attr->atime_nsec,
  622. p9attr->mtime_sec,
  623. p9attr->mtime_nsec);
  624. }
  625. break;
  626. case '?':
  627. if ((proto_version != p9_proto_2000u) &&
  628. (proto_version != p9_proto_2000L))
  629. return 0;
  630. break;
  631. default:
  632. BUG();
  633. break;
  634. }
  635. if (errcode)
  636. break;
  637. }
  638. return errcode;
  639. }
  640. int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
  641. {
  642. va_list ap;
  643. int ret;
  644. va_start(ap, fmt);
  645. ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
  646. va_end(ap);
  647. return ret;
  648. }
  649. static int
  650. p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
  651. {
  652. va_list ap;
  653. int ret;
  654. va_start(ap, fmt);
  655. ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
  656. va_end(ap);
  657. return ret;
  658. }
  659. int p9stat_read(struct p9_client *clnt, char *buf, int len, struct p9_wstat *st)
  660. {
  661. struct p9_fcall fake_pdu;
  662. int ret;
  663. fake_pdu.size = len;
  664. fake_pdu.capacity = len;
  665. fake_pdu.sdata = buf;
  666. fake_pdu.offset = 0;
  667. ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "S", st);
  668. if (ret) {
  669. p9_debug(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
  670. trace_9p_protocol_dump(clnt, &fake_pdu);
  671. return ret;
  672. }
  673. return fake_pdu.offset;
  674. }
  675. EXPORT_SYMBOL(p9stat_read);
  676. int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
  677. {
  678. pdu->id = type;
  679. return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
  680. }
  681. int p9pdu_finalize(struct p9_client *clnt, struct p9_fcall *pdu)
  682. {
  683. int size = pdu->size;
  684. int err;
  685. pdu->size = 0;
  686. err = p9pdu_writef(pdu, 0, "d", size);
  687. pdu->size = size;
  688. trace_9p_protocol_dump(clnt, pdu);
  689. p9_debug(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n",
  690. pdu->size, pdu->id, pdu->tag);
  691. return err;
  692. }
  693. void p9pdu_reset(struct p9_fcall *pdu)
  694. {
  695. pdu->offset = 0;
  696. pdu->size = 0;
  697. }
  698. int p9dirent_read(struct p9_client *clnt, char *buf, int len,
  699. struct p9_dirent *dirent)
  700. {
  701. struct p9_fcall fake_pdu;
  702. int ret;
  703. char *nameptr;
  704. fake_pdu.size = len;
  705. fake_pdu.capacity = len;
  706. fake_pdu.sdata = buf;
  707. fake_pdu.offset = 0;
  708. ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "Qqbs", &dirent->qid,
  709. &dirent->d_off, &dirent->d_type, &nameptr);
  710. if (ret) {
  711. p9_debug(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
  712. trace_9p_protocol_dump(clnt, &fake_pdu);
  713. return ret;
  714. }
  715. ret = strscpy(dirent->d_name, nameptr, sizeof(dirent->d_name));
  716. if (ret < 0) {
  717. p9_debug(P9_DEBUG_ERROR,
  718. "On the wire dirent name too long: %s\n",
  719. nameptr);
  720. kfree(nameptr);
  721. return ret;
  722. }
  723. kfree(nameptr);
  724. return fake_pdu.offset;
  725. }
  726. EXPORT_SYMBOL(p9dirent_read);