protocol.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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_objs(struct p9_qid, *nwqid,
  417. GFP_NOFS);
  418. if (*wqids == NULL)
  419. errcode = -ENOMEM;
  420. }
  421. if (!errcode) {
  422. int i;
  423. for (i = 0; i < *nwqid; i++) {
  424. errcode =
  425. p9pdu_readf(pdu,
  426. proto_version,
  427. "Q",
  428. &(*wqids)[i]);
  429. if (errcode)
  430. break;
  431. }
  432. }
  433. if (errcode) {
  434. kfree(*wqids);
  435. *wqids = NULL;
  436. }
  437. }
  438. break;
  439. case 'A': {
  440. struct p9_stat_dotl *stbuf =
  441. va_arg(ap, struct p9_stat_dotl *);
  442. memset(stbuf, 0, sizeof(struct p9_stat_dotl));
  443. errcode =
  444. p9pdu_readf(pdu, proto_version,
  445. "qQdugqqqqqqqqqqqqqqq",
  446. &stbuf->st_result_mask,
  447. &stbuf->qid,
  448. &stbuf->st_mode,
  449. &stbuf->st_uid, &stbuf->st_gid,
  450. &stbuf->st_nlink,
  451. &stbuf->st_rdev, &stbuf->st_size,
  452. &stbuf->st_blksize, &stbuf->st_blocks,
  453. &stbuf->st_atime_sec,
  454. &stbuf->st_atime_nsec,
  455. &stbuf->st_mtime_sec,
  456. &stbuf->st_mtime_nsec,
  457. &stbuf->st_ctime_sec,
  458. &stbuf->st_ctime_nsec,
  459. &stbuf->st_btime_sec,
  460. &stbuf->st_btime_nsec,
  461. &stbuf->st_gen,
  462. &stbuf->st_data_version);
  463. }
  464. break;
  465. case '?':
  466. if ((proto_version != p9_proto_2000u) &&
  467. (proto_version != p9_proto_2000L))
  468. return 0;
  469. break;
  470. default:
  471. BUG();
  472. break;
  473. }
  474. if (errcode)
  475. break;
  476. }
  477. return errcode;
  478. }
  479. int
  480. p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
  481. va_list ap)
  482. {
  483. const char *ptr;
  484. int errcode = 0;
  485. for (ptr = fmt; *ptr; ptr++) {
  486. switch (*ptr) {
  487. case 'b':{
  488. int8_t val = va_arg(ap, int);
  489. if (pdu_write(pdu, &val, sizeof(val)))
  490. errcode = -EFAULT;
  491. }
  492. break;
  493. case 'w':{
  494. __le16 val = cpu_to_le16(va_arg(ap, int));
  495. if (pdu_write(pdu, &val, sizeof(val)))
  496. errcode = -EFAULT;
  497. }
  498. break;
  499. case 'd':{
  500. __le32 val = cpu_to_le32(va_arg(ap, int32_t));
  501. if (pdu_write(pdu, &val, sizeof(val)))
  502. errcode = -EFAULT;
  503. }
  504. break;
  505. case 'q':{
  506. __le64 val = cpu_to_le64(va_arg(ap, int64_t));
  507. if (pdu_write(pdu, &val, sizeof(val)))
  508. errcode = -EFAULT;
  509. }
  510. break;
  511. case 's':{
  512. const char *sptr = va_arg(ap, const char *);
  513. uint16_t len = 0;
  514. if (sptr)
  515. len = min_t(size_t, strlen(sptr),
  516. USHRT_MAX);
  517. errcode = p9pdu_writef(pdu, proto_version,
  518. "w", len);
  519. if (!errcode && pdu_write(pdu, sptr, len))
  520. errcode = -EFAULT;
  521. }
  522. break;
  523. case 'u': {
  524. kuid_t uid = va_arg(ap, kuid_t);
  525. __le32 val = cpu_to_le32(
  526. from_kuid(&init_user_ns, uid));
  527. if (pdu_write(pdu, &val, sizeof(val)))
  528. errcode = -EFAULT;
  529. } break;
  530. case 'g': {
  531. kgid_t gid = va_arg(ap, kgid_t);
  532. __le32 val = cpu_to_le32(
  533. from_kgid(&init_user_ns, gid));
  534. if (pdu_write(pdu, &val, sizeof(val)))
  535. errcode = -EFAULT;
  536. } break;
  537. case 'Q':{
  538. const struct p9_qid *qid =
  539. va_arg(ap, const struct p9_qid *);
  540. errcode =
  541. p9pdu_writef(pdu, proto_version, "bdq",
  542. qid->type, qid->version,
  543. qid->path);
  544. } break;
  545. case 'S':{
  546. const struct p9_wstat *stbuf =
  547. va_arg(ap, const struct p9_wstat *);
  548. errcode =
  549. p9pdu_writef(pdu, proto_version,
  550. "wwdQdddqssss?sugu",
  551. stbuf->size, stbuf->type,
  552. stbuf->dev, &stbuf->qid,
  553. stbuf->mode, stbuf->atime,
  554. stbuf->mtime, stbuf->length,
  555. stbuf->name, stbuf->uid,
  556. stbuf->gid, stbuf->muid,
  557. stbuf->extension, stbuf->n_uid,
  558. stbuf->n_gid, stbuf->n_muid);
  559. } break;
  560. case 'V':{
  561. uint32_t count = va_arg(ap, uint32_t);
  562. struct iov_iter *from =
  563. va_arg(ap, struct iov_iter *);
  564. errcode = p9pdu_writef(pdu, proto_version, "d",
  565. count);
  566. if (!errcode && pdu_write_u(pdu, from, count))
  567. errcode = -EFAULT;
  568. }
  569. break;
  570. case 'T':{
  571. uint16_t nwname = va_arg(ap, int);
  572. const char **wnames = va_arg(ap, const char **);
  573. errcode = p9pdu_writef(pdu, proto_version, "w",
  574. nwname);
  575. if (!errcode) {
  576. int i;
  577. for (i = 0; i < nwname; i++) {
  578. errcode =
  579. p9pdu_writef(pdu,
  580. proto_version,
  581. "s",
  582. wnames[i]);
  583. if (errcode)
  584. break;
  585. }
  586. }
  587. }
  588. break;
  589. case 'R':{
  590. uint16_t nwqid = va_arg(ap, int);
  591. struct p9_qid *wqids =
  592. va_arg(ap, struct p9_qid *);
  593. errcode = p9pdu_writef(pdu, proto_version, "w",
  594. nwqid);
  595. if (!errcode) {
  596. int i;
  597. for (i = 0; i < nwqid; i++) {
  598. errcode =
  599. p9pdu_writef(pdu,
  600. proto_version,
  601. "Q",
  602. &wqids[i]);
  603. if (errcode)
  604. break;
  605. }
  606. }
  607. }
  608. break;
  609. case 'I':{
  610. struct p9_iattr_dotl *p9attr = va_arg(ap,
  611. struct p9_iattr_dotl *);
  612. errcode = p9pdu_writef(pdu, proto_version,
  613. "ddugqqqqq",
  614. p9attr->valid,
  615. p9attr->mode,
  616. p9attr->uid,
  617. p9attr->gid,
  618. p9attr->size,
  619. p9attr->atime_sec,
  620. p9attr->atime_nsec,
  621. p9attr->mtime_sec,
  622. p9attr->mtime_nsec);
  623. }
  624. break;
  625. case '?':
  626. if ((proto_version != p9_proto_2000u) &&
  627. (proto_version != p9_proto_2000L))
  628. return 0;
  629. break;
  630. default:
  631. BUG();
  632. break;
  633. }
  634. if (errcode)
  635. break;
  636. }
  637. return errcode;
  638. }
  639. int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
  640. {
  641. va_list ap;
  642. int ret;
  643. va_start(ap, fmt);
  644. ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
  645. va_end(ap);
  646. return ret;
  647. }
  648. static int
  649. p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
  650. {
  651. va_list ap;
  652. int ret;
  653. va_start(ap, fmt);
  654. ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
  655. va_end(ap);
  656. return ret;
  657. }
  658. int p9stat_read(struct p9_client *clnt, char *buf, int len, struct p9_wstat *st)
  659. {
  660. struct p9_fcall fake_pdu;
  661. int ret;
  662. fake_pdu.size = len;
  663. fake_pdu.capacity = len;
  664. fake_pdu.sdata = buf;
  665. fake_pdu.offset = 0;
  666. ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "S", st);
  667. if (ret) {
  668. p9_debug(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
  669. trace_9p_protocol_dump(clnt, &fake_pdu);
  670. return ret;
  671. }
  672. return fake_pdu.offset;
  673. }
  674. EXPORT_SYMBOL(p9stat_read);
  675. int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
  676. {
  677. pdu->id = type;
  678. return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
  679. }
  680. int p9pdu_finalize(struct p9_client *clnt, struct p9_fcall *pdu)
  681. {
  682. int size = pdu->size;
  683. int err;
  684. pdu->size = 0;
  685. err = p9pdu_writef(pdu, 0, "d", size);
  686. pdu->size = size;
  687. trace_9p_protocol_dump(clnt, pdu);
  688. p9_debug(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n",
  689. pdu->size, pdu->id, pdu->tag);
  690. return err;
  691. }
  692. void p9pdu_reset(struct p9_fcall *pdu)
  693. {
  694. pdu->offset = 0;
  695. pdu->size = 0;
  696. }
  697. int p9dirent_read(struct p9_client *clnt, char *buf, int len,
  698. struct p9_dirent *dirent)
  699. {
  700. struct p9_fcall fake_pdu;
  701. int ret;
  702. char *nameptr;
  703. fake_pdu.size = len;
  704. fake_pdu.capacity = len;
  705. fake_pdu.sdata = buf;
  706. fake_pdu.offset = 0;
  707. ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "Qqbs", &dirent->qid,
  708. &dirent->d_off, &dirent->d_type, &nameptr);
  709. if (ret) {
  710. p9_debug(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
  711. trace_9p_protocol_dump(clnt, &fake_pdu);
  712. return ret;
  713. }
  714. ret = strscpy(dirent->d_name, nameptr, sizeof(dirent->d_name));
  715. if (ret < 0) {
  716. p9_debug(P9_DEBUG_ERROR,
  717. "On the wire dirent name too long: %s\n",
  718. nameptr);
  719. kfree(nameptr);
  720. return ret;
  721. }
  722. kfree(nameptr);
  723. return fake_pdu.offset;
  724. }
  725. EXPORT_SYMBOL(p9dirent_read);