client.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277
  1. /*
  2. * net/9p/clnt.c
  3. *
  4. * 9P Client
  5. *
  6. * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
  7. * Copyright (C) 2007 by Latchesar Ionkov <lucho@ionkov.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to:
  20. * Free Software Foundation
  21. * 51 Franklin Street, Fifth Floor
  22. * Boston, MA 02111-1301 USA
  23. *
  24. */
  25. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26. #include <linux/module.h>
  27. #include <linux/errno.h>
  28. #include <linux/fs.h>
  29. #include <linux/poll.h>
  30. #include <linux/idr.h>
  31. #include <linux/mutex.h>
  32. #include <linux/slab.h>
  33. #include <linux/sched.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/uio.h>
  36. #include <net/9p/9p.h>
  37. #include <linux/parser.h>
  38. #include <net/9p/client.h>
  39. #include <net/9p/transport.h>
  40. #include "protocol.h"
  41. #define CREATE_TRACE_POINTS
  42. #include <trace/events/9p.h>
  43. /*
  44. * Client Option Parsing (code inspired by NFS code)
  45. * - a little lazy - parse all client options
  46. */
  47. enum {
  48. Opt_msize,
  49. Opt_trans,
  50. Opt_legacy,
  51. Opt_version,
  52. Opt_err,
  53. };
  54. static const match_table_t tokens = {
  55. {Opt_msize, "msize=%u"},
  56. {Opt_legacy, "noextend"},
  57. {Opt_trans, "trans=%s"},
  58. {Opt_version, "version=%s"},
  59. {Opt_err, NULL},
  60. };
  61. inline int p9_is_proto_dotl(struct p9_client *clnt)
  62. {
  63. return clnt->proto_version == p9_proto_2000L;
  64. }
  65. EXPORT_SYMBOL(p9_is_proto_dotl);
  66. inline int p9_is_proto_dotu(struct p9_client *clnt)
  67. {
  68. return clnt->proto_version == p9_proto_2000u;
  69. }
  70. EXPORT_SYMBOL(p9_is_proto_dotu);
  71. /*
  72. * Some error codes are taken directly from the server replies,
  73. * make sure they are valid.
  74. */
  75. static int safe_errno(int err)
  76. {
  77. if ((err > 0) || (err < -MAX_ERRNO)) {
  78. p9_debug(P9_DEBUG_ERROR, "Invalid error code %d\n", err);
  79. return -EPROTO;
  80. }
  81. return err;
  82. }
  83. /* Interpret mount option for protocol version */
  84. static int get_protocol_version(char *s)
  85. {
  86. int version = -EINVAL;
  87. if (!strcmp(s, "9p2000")) {
  88. version = p9_proto_legacy;
  89. p9_debug(P9_DEBUG_9P, "Protocol version: Legacy\n");
  90. } else if (!strcmp(s, "9p2000.u")) {
  91. version = p9_proto_2000u;
  92. p9_debug(P9_DEBUG_9P, "Protocol version: 9P2000.u\n");
  93. } else if (!strcmp(s, "9p2000.L")) {
  94. version = p9_proto_2000L;
  95. p9_debug(P9_DEBUG_9P, "Protocol version: 9P2000.L\n");
  96. } else
  97. pr_info("Unknown protocol version %s\n", s);
  98. return version;
  99. }
  100. /**
  101. * parse_options - parse mount options into client structure
  102. * @opts: options string passed from mount
  103. * @clnt: existing v9fs client information
  104. *
  105. * Return 0 upon success, -ERRNO upon failure
  106. */
  107. static int parse_opts(char *opts, struct p9_client *clnt)
  108. {
  109. char *options, *tmp_options;
  110. char *p;
  111. substring_t args[MAX_OPT_ARGS];
  112. int option;
  113. char *s;
  114. int ret = 0;
  115. clnt->proto_version = p9_proto_2000L;
  116. clnt->msize = 8192;
  117. if (!opts)
  118. return 0;
  119. tmp_options = kstrdup(opts, GFP_KERNEL);
  120. if (!tmp_options) {
  121. p9_debug(P9_DEBUG_ERROR,
  122. "failed to allocate copy of option string\n");
  123. return -ENOMEM;
  124. }
  125. options = tmp_options;
  126. while ((p = strsep(&options, ",")) != NULL) {
  127. int token, r;
  128. if (!*p)
  129. continue;
  130. token = match_token(p, tokens, args);
  131. switch (token) {
  132. case Opt_msize:
  133. r = match_int(&args[0], &option);
  134. if (r < 0) {
  135. p9_debug(P9_DEBUG_ERROR,
  136. "integer field, but no integer?\n");
  137. ret = r;
  138. continue;
  139. }
  140. clnt->msize = option;
  141. break;
  142. case Opt_trans:
  143. s = match_strdup(&args[0]);
  144. if (!s) {
  145. ret = -ENOMEM;
  146. p9_debug(P9_DEBUG_ERROR,
  147. "problem allocating copy of trans arg\n");
  148. goto free_and_return;
  149. }
  150. clnt->trans_mod = v9fs_get_trans_by_name(s);
  151. if (clnt->trans_mod == NULL) {
  152. pr_info("Could not find request transport: %s\n",
  153. s);
  154. ret = -EINVAL;
  155. kfree(s);
  156. goto free_and_return;
  157. }
  158. kfree(s);
  159. break;
  160. case Opt_legacy:
  161. clnt->proto_version = p9_proto_legacy;
  162. break;
  163. case Opt_version:
  164. s = match_strdup(&args[0]);
  165. if (!s) {
  166. ret = -ENOMEM;
  167. p9_debug(P9_DEBUG_ERROR,
  168. "problem allocating copy of version arg\n");
  169. goto free_and_return;
  170. }
  171. ret = get_protocol_version(s);
  172. if (ret == -EINVAL) {
  173. kfree(s);
  174. goto free_and_return;
  175. }
  176. kfree(s);
  177. clnt->proto_version = ret;
  178. break;
  179. default:
  180. continue;
  181. }
  182. }
  183. free_and_return:
  184. kfree(tmp_options);
  185. return ret;
  186. }
  187. static struct p9_fcall *p9_fcall_alloc(int alloc_msize)
  188. {
  189. struct p9_fcall *fc;
  190. fc = kmalloc(sizeof(struct p9_fcall) + alloc_msize, GFP_NOFS);
  191. if (!fc)
  192. return NULL;
  193. fc->capacity = alloc_msize;
  194. fc->sdata = (char *) fc + sizeof(struct p9_fcall);
  195. return fc;
  196. }
  197. /**
  198. * p9_tag_alloc - lookup/allocate a request by tag
  199. * @c: client session to lookup tag within
  200. * @tag: numeric id for transaction
  201. *
  202. * this is a simple array lookup, but will grow the
  203. * request_slots as necessary to accommodate transaction
  204. * ids which did not previously have a slot.
  205. *
  206. * this code relies on the client spinlock to manage locks, its
  207. * possible we should switch to something else, but I'd rather
  208. * stick with something low-overhead for the common case.
  209. *
  210. */
  211. static struct p9_req_t *
  212. p9_tag_alloc(struct p9_client *c, u16 tag, unsigned int max_size)
  213. {
  214. unsigned long flags;
  215. int row, col;
  216. struct p9_req_t *req;
  217. int alloc_msize = min(c->msize, max_size);
  218. /* This looks up the original request by tag so we know which
  219. * buffer to read the data into */
  220. tag++;
  221. if (tag >= c->max_tag) {
  222. spin_lock_irqsave(&c->lock, flags);
  223. /* check again since original check was outside of lock */
  224. while (tag >= c->max_tag) {
  225. row = (tag / P9_ROW_MAXTAG);
  226. c->reqs[row] = kcalloc(P9_ROW_MAXTAG,
  227. sizeof(struct p9_req_t), GFP_ATOMIC);
  228. if (!c->reqs[row]) {
  229. pr_err("Couldn't grow tag array\n");
  230. spin_unlock_irqrestore(&c->lock, flags);
  231. return ERR_PTR(-ENOMEM);
  232. }
  233. for (col = 0; col < P9_ROW_MAXTAG; col++) {
  234. c->reqs[row][col].status = REQ_STATUS_IDLE;
  235. c->reqs[row][col].tc = NULL;
  236. }
  237. c->max_tag += P9_ROW_MAXTAG;
  238. }
  239. spin_unlock_irqrestore(&c->lock, flags);
  240. }
  241. row = tag / P9_ROW_MAXTAG;
  242. col = tag % P9_ROW_MAXTAG;
  243. req = &c->reqs[row][col];
  244. if (!req->wq) {
  245. req->wq = kmalloc(sizeof(wait_queue_head_t), GFP_NOFS);
  246. if (!req->wq)
  247. goto grow_failed;
  248. init_waitqueue_head(req->wq);
  249. }
  250. if (!req->tc)
  251. req->tc = p9_fcall_alloc(alloc_msize);
  252. if (!req->rc)
  253. req->rc = p9_fcall_alloc(alloc_msize);
  254. if (!req->tc || !req->rc)
  255. goto grow_failed;
  256. p9pdu_reset(req->tc);
  257. p9pdu_reset(req->rc);
  258. req->tc->tag = tag-1;
  259. req->status = REQ_STATUS_ALLOC;
  260. return req;
  261. grow_failed:
  262. pr_err("Couldn't grow tag array\n");
  263. kfree(req->tc);
  264. kfree(req->rc);
  265. kfree(req->wq);
  266. req->tc = req->rc = NULL;
  267. req->wq = NULL;
  268. return ERR_PTR(-ENOMEM);
  269. }
  270. /**
  271. * p9_tag_lookup - lookup a request by tag
  272. * @c: client session to lookup tag within
  273. * @tag: numeric id for transaction
  274. *
  275. */
  276. struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag)
  277. {
  278. int row, col;
  279. /* This looks up the original request by tag so we know which
  280. * buffer to read the data into */
  281. tag++;
  282. if(tag >= c->max_tag)
  283. return NULL;
  284. row = tag / P9_ROW_MAXTAG;
  285. col = tag % P9_ROW_MAXTAG;
  286. return &c->reqs[row][col];
  287. }
  288. EXPORT_SYMBOL(p9_tag_lookup);
  289. /**
  290. * p9_tag_init - setup tags structure and contents
  291. * @c: v9fs client struct
  292. *
  293. * This initializes the tags structure for each client instance.
  294. *
  295. */
  296. static int p9_tag_init(struct p9_client *c)
  297. {
  298. int err = 0;
  299. c->tagpool = p9_idpool_create();
  300. if (IS_ERR(c->tagpool)) {
  301. err = PTR_ERR(c->tagpool);
  302. goto error;
  303. }
  304. err = p9_idpool_get(c->tagpool); /* reserve tag 0 */
  305. if (err < 0) {
  306. p9_idpool_destroy(c->tagpool);
  307. goto error;
  308. }
  309. c->max_tag = 0;
  310. error:
  311. return err;
  312. }
  313. /**
  314. * p9_tag_cleanup - cleans up tags structure and reclaims resources
  315. * @c: v9fs client struct
  316. *
  317. * This frees resources associated with the tags structure
  318. *
  319. */
  320. static void p9_tag_cleanup(struct p9_client *c)
  321. {
  322. int row, col;
  323. /* check to insure all requests are idle */
  324. for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
  325. for (col = 0; col < P9_ROW_MAXTAG; col++) {
  326. if (c->reqs[row][col].status != REQ_STATUS_IDLE) {
  327. p9_debug(P9_DEBUG_MUX,
  328. "Attempting to cleanup non-free tag %d,%d\n",
  329. row, col);
  330. /* TODO: delay execution of cleanup */
  331. return;
  332. }
  333. }
  334. }
  335. if (c->tagpool) {
  336. p9_idpool_put(0, c->tagpool); /* free reserved tag 0 */
  337. p9_idpool_destroy(c->tagpool);
  338. }
  339. /* free requests associated with tags */
  340. for (row = 0; row < (c->max_tag/P9_ROW_MAXTAG); row++) {
  341. for (col = 0; col < P9_ROW_MAXTAG; col++) {
  342. kfree(c->reqs[row][col].wq);
  343. kfree(c->reqs[row][col].tc);
  344. kfree(c->reqs[row][col].rc);
  345. }
  346. kfree(c->reqs[row]);
  347. }
  348. c->max_tag = 0;
  349. }
  350. /**
  351. * p9_free_req - free a request and clean-up as necessary
  352. * c: client state
  353. * r: request to release
  354. *
  355. */
  356. static void p9_free_req(struct p9_client *c, struct p9_req_t *r)
  357. {
  358. int tag = r->tc->tag;
  359. p9_debug(P9_DEBUG_MUX, "clnt %p req %p tag: %d\n", c, r, tag);
  360. r->status = REQ_STATUS_IDLE;
  361. if (tag != P9_NOTAG && p9_idpool_check(tag, c->tagpool))
  362. p9_idpool_put(tag, c->tagpool);
  363. }
  364. /**
  365. * p9_client_cb - call back from transport to client
  366. * c: client state
  367. * req: request received
  368. *
  369. */
  370. void p9_client_cb(struct p9_client *c, struct p9_req_t *req, int status)
  371. {
  372. p9_debug(P9_DEBUG_MUX, " tag %d\n", req->tc->tag);
  373. /*
  374. * This barrier is needed to make sure any change made to req before
  375. * the other thread wakes up will indeed be seen by the waiting side.
  376. */
  377. smp_wmb();
  378. req->status = status;
  379. wake_up(req->wq);
  380. p9_debug(P9_DEBUG_MUX, "wakeup: %d\n", req->tc->tag);
  381. }
  382. EXPORT_SYMBOL(p9_client_cb);
  383. /**
  384. * p9_parse_header - parse header arguments out of a packet
  385. * @pdu: packet to parse
  386. * @size: size of packet
  387. * @type: type of request
  388. * @tag: tag of packet
  389. * @rewind: set if we need to rewind offset afterwards
  390. */
  391. int
  392. p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type, int16_t *tag,
  393. int rewind)
  394. {
  395. int8_t r_type;
  396. int16_t r_tag;
  397. int32_t r_size;
  398. int offset = pdu->offset;
  399. int err;
  400. pdu->offset = 0;
  401. if (pdu->size == 0)
  402. pdu->size = 7;
  403. err = p9pdu_readf(pdu, 0, "dbw", &r_size, &r_type, &r_tag);
  404. if (err)
  405. goto rewind_and_exit;
  406. pdu->size = r_size;
  407. pdu->id = r_type;
  408. pdu->tag = r_tag;
  409. p9_debug(P9_DEBUG_9P, "<<< size=%d type: %d tag: %d\n",
  410. pdu->size, pdu->id, pdu->tag);
  411. if (type)
  412. *type = r_type;
  413. if (tag)
  414. *tag = r_tag;
  415. if (size)
  416. *size = r_size;
  417. rewind_and_exit:
  418. if (rewind)
  419. pdu->offset = offset;
  420. return err;
  421. }
  422. EXPORT_SYMBOL(p9_parse_header);
  423. /**
  424. * p9_check_errors - check 9p packet for error return and process it
  425. * @c: current client instance
  426. * @req: request to parse and check for error conditions
  427. *
  428. * returns error code if one is discovered, otherwise returns 0
  429. *
  430. * this will have to be more complicated if we have multiple
  431. * error packet types
  432. */
  433. static int p9_check_errors(struct p9_client *c, struct p9_req_t *req)
  434. {
  435. int8_t type;
  436. int err;
  437. int ecode;
  438. err = p9_parse_header(req->rc, NULL, &type, NULL, 0);
  439. /*
  440. * dump the response from server
  441. * This should be after check errors which poplulate pdu_fcall.
  442. */
  443. trace_9p_protocol_dump(c, req->rc);
  444. if (err) {
  445. p9_debug(P9_DEBUG_ERROR, "couldn't parse header %d\n", err);
  446. return err;
  447. }
  448. if (type != P9_RERROR && type != P9_RLERROR)
  449. return 0;
  450. if (!p9_is_proto_dotl(c)) {
  451. char *ename;
  452. err = p9pdu_readf(req->rc, c->proto_version, "s?d",
  453. &ename, &ecode);
  454. if (err)
  455. goto out_err;
  456. if (p9_is_proto_dotu(c) && ecode < 512)
  457. err = -ecode;
  458. if (!err) {
  459. err = p9_errstr2errno(ename, strlen(ename));
  460. p9_debug(P9_DEBUG_9P, "<<< RERROR (%d) %s\n",
  461. -ecode, ename);
  462. }
  463. kfree(ename);
  464. } else {
  465. err = p9pdu_readf(req->rc, c->proto_version, "d", &ecode);
  466. err = -ecode;
  467. p9_debug(P9_DEBUG_9P, "<<< RLERROR (%d)\n", -ecode);
  468. }
  469. return err;
  470. out_err:
  471. p9_debug(P9_DEBUG_ERROR, "couldn't parse error%d\n", err);
  472. return err;
  473. }
  474. /**
  475. * p9_check_zc_errors - check 9p packet for error return and process it
  476. * @c: current client instance
  477. * @req: request to parse and check for error conditions
  478. * @in_hdrlen: Size of response protocol buffer.
  479. *
  480. * returns error code if one is discovered, otherwise returns 0
  481. *
  482. * this will have to be more complicated if we have multiple
  483. * error packet types
  484. */
  485. static int p9_check_zc_errors(struct p9_client *c, struct p9_req_t *req,
  486. struct iov_iter *uidata, int in_hdrlen)
  487. {
  488. int err;
  489. int ecode;
  490. int8_t type;
  491. char *ename = NULL;
  492. err = p9_parse_header(req->rc, NULL, &type, NULL, 0);
  493. /*
  494. * dump the response from server
  495. * This should be after parse_header which poplulate pdu_fcall.
  496. */
  497. trace_9p_protocol_dump(c, req->rc);
  498. if (err) {
  499. p9_debug(P9_DEBUG_ERROR, "couldn't parse header %d\n", err);
  500. return err;
  501. }
  502. if (type != P9_RERROR && type != P9_RLERROR)
  503. return 0;
  504. if (!p9_is_proto_dotl(c)) {
  505. /* Error is reported in string format */
  506. int len;
  507. /* 7 = header size for RERROR; */
  508. int inline_len = in_hdrlen - 7;
  509. len = req->rc->size - req->rc->offset;
  510. if (len > (P9_ZC_HDR_SZ - 7)) {
  511. err = -EFAULT;
  512. goto out_err;
  513. }
  514. ename = &req->rc->sdata[req->rc->offset];
  515. if (len > inline_len) {
  516. /* We have error in external buffer */
  517. err = copy_from_iter(ename + inline_len,
  518. len - inline_len, uidata);
  519. if (err != len - inline_len) {
  520. err = -EFAULT;
  521. goto out_err;
  522. }
  523. }
  524. ename = NULL;
  525. err = p9pdu_readf(req->rc, c->proto_version, "s?d",
  526. &ename, &ecode);
  527. if (err)
  528. goto out_err;
  529. if (p9_is_proto_dotu(c) && ecode < 512)
  530. err = -ecode;
  531. if (!err) {
  532. err = p9_errstr2errno(ename, strlen(ename));
  533. p9_debug(P9_DEBUG_9P, "<<< RERROR (%d) %s\n",
  534. -ecode, ename);
  535. }
  536. kfree(ename);
  537. } else {
  538. err = p9pdu_readf(req->rc, c->proto_version, "d", &ecode);
  539. err = -ecode;
  540. p9_debug(P9_DEBUG_9P, "<<< RLERROR (%d)\n", -ecode);
  541. }
  542. return err;
  543. out_err:
  544. p9_debug(P9_DEBUG_ERROR, "couldn't parse error%d\n", err);
  545. return err;
  546. }
  547. static struct p9_req_t *
  548. p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...);
  549. /**
  550. * p9_client_flush - flush (cancel) a request
  551. * @c: client state
  552. * @oldreq: request to cancel
  553. *
  554. * This sents a flush for a particular request and links
  555. * the flush request to the original request. The current
  556. * code only supports a single flush request although the protocol
  557. * allows for multiple flush requests to be sent for a single request.
  558. *
  559. */
  560. static int p9_client_flush(struct p9_client *c, struct p9_req_t *oldreq)
  561. {
  562. struct p9_req_t *req;
  563. int16_t oldtag;
  564. int err;
  565. err = p9_parse_header(oldreq->tc, NULL, NULL, &oldtag, 1);
  566. if (err)
  567. return err;
  568. p9_debug(P9_DEBUG_9P, ">>> TFLUSH tag %d\n", oldtag);
  569. req = p9_client_rpc(c, P9_TFLUSH, "w", oldtag);
  570. if (IS_ERR(req))
  571. return PTR_ERR(req);
  572. /*
  573. * if we haven't received a response for oldreq,
  574. * remove it from the list
  575. */
  576. if (oldreq->status == REQ_STATUS_SENT)
  577. if (c->trans_mod->cancelled)
  578. c->trans_mod->cancelled(c, oldreq);
  579. p9_free_req(c, req);
  580. return 0;
  581. }
  582. static struct p9_req_t *p9_client_prepare_req(struct p9_client *c,
  583. int8_t type, int req_size,
  584. const char *fmt, va_list ap)
  585. {
  586. int tag, err;
  587. struct p9_req_t *req;
  588. p9_debug(P9_DEBUG_MUX, "client %p op %d\n", c, type);
  589. /* we allow for any status other than disconnected */
  590. if (c->status == Disconnected)
  591. return ERR_PTR(-EIO);
  592. /* if status is begin_disconnected we allow only clunk request */
  593. if ((c->status == BeginDisconnect) && (type != P9_TCLUNK))
  594. return ERR_PTR(-EIO);
  595. tag = P9_NOTAG;
  596. if (type != P9_TVERSION) {
  597. tag = p9_idpool_get(c->tagpool);
  598. if (tag < 0)
  599. return ERR_PTR(-ENOMEM);
  600. }
  601. req = p9_tag_alloc(c, tag, req_size);
  602. if (IS_ERR(req))
  603. return req;
  604. /* marshall the data */
  605. p9pdu_prepare(req->tc, tag, type);
  606. err = p9pdu_vwritef(req->tc, c->proto_version, fmt, ap);
  607. if (err)
  608. goto reterr;
  609. p9pdu_finalize(c, req->tc);
  610. trace_9p_client_req(c, type, tag);
  611. return req;
  612. reterr:
  613. p9_free_req(c, req);
  614. return ERR_PTR(err);
  615. }
  616. /**
  617. * p9_client_rpc - issue a request and wait for a response
  618. * @c: client session
  619. * @type: type of request
  620. * @fmt: protocol format string (see protocol.c)
  621. *
  622. * Returns request structure (which client must free using p9_free_req)
  623. */
  624. static struct p9_req_t *
  625. p9_client_rpc(struct p9_client *c, int8_t type, const char *fmt, ...)
  626. {
  627. va_list ap;
  628. int sigpending, err;
  629. unsigned long flags;
  630. struct p9_req_t *req;
  631. va_start(ap, fmt);
  632. req = p9_client_prepare_req(c, type, c->msize, fmt, ap);
  633. va_end(ap);
  634. if (IS_ERR(req))
  635. return req;
  636. if (signal_pending(current)) {
  637. sigpending = 1;
  638. clear_thread_flag(TIF_SIGPENDING);
  639. } else
  640. sigpending = 0;
  641. err = c->trans_mod->request(c, req);
  642. if (err < 0) {
  643. if (err != -ERESTARTSYS && err != -EFAULT)
  644. c->status = Disconnected;
  645. goto reterr;
  646. }
  647. again:
  648. /* Wait for the response */
  649. err = wait_event_killable(*req->wq, req->status >= REQ_STATUS_RCVD);
  650. /*
  651. * Make sure our req is coherent with regard to updates in other
  652. * threads - echoes to wmb() in the callback
  653. */
  654. smp_rmb();
  655. if ((err == -ERESTARTSYS) && (c->status == Connected)
  656. && (type == P9_TFLUSH)) {
  657. sigpending = 1;
  658. clear_thread_flag(TIF_SIGPENDING);
  659. goto again;
  660. }
  661. if (req->status == REQ_STATUS_ERROR) {
  662. p9_debug(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err);
  663. err = req->t_err;
  664. }
  665. if ((err == -ERESTARTSYS) && (c->status == Connected)) {
  666. p9_debug(P9_DEBUG_MUX, "flushing\n");
  667. sigpending = 1;
  668. clear_thread_flag(TIF_SIGPENDING);
  669. if (c->trans_mod->cancel(c, req))
  670. p9_client_flush(c, req);
  671. /* if we received the response anyway, don't signal error */
  672. if (req->status == REQ_STATUS_RCVD)
  673. err = 0;
  674. }
  675. if (sigpending) {
  676. spin_lock_irqsave(&current->sighand->siglock, flags);
  677. recalc_sigpending();
  678. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  679. }
  680. if (err < 0)
  681. goto reterr;
  682. err = p9_check_errors(c, req);
  683. trace_9p_client_res(c, type, req->rc->tag, err);
  684. if (!err)
  685. return req;
  686. reterr:
  687. p9_free_req(c, req);
  688. return ERR_PTR(safe_errno(err));
  689. }
  690. /**
  691. * p9_client_zc_rpc - issue a request and wait for a response
  692. * @c: client session
  693. * @type: type of request
  694. * @uidata: destination for zero copy read
  695. * @uodata: source for zero copy write
  696. * @inlen: read buffer size
  697. * @olen: write buffer size
  698. * @hdrlen: reader header size, This is the size of response protocol data
  699. * @fmt: protocol format string (see protocol.c)
  700. *
  701. * Returns request structure (which client must free using p9_free_req)
  702. */
  703. static struct p9_req_t *p9_client_zc_rpc(struct p9_client *c, int8_t type,
  704. struct iov_iter *uidata,
  705. struct iov_iter *uodata,
  706. int inlen, int olen, int in_hdrlen,
  707. const char *fmt, ...)
  708. {
  709. va_list ap;
  710. int sigpending, err;
  711. unsigned long flags;
  712. struct p9_req_t *req;
  713. va_start(ap, fmt);
  714. /*
  715. * We allocate a inline protocol data of only 4k bytes.
  716. * The actual content is passed in zero-copy fashion.
  717. */
  718. req = p9_client_prepare_req(c, type, P9_ZC_HDR_SZ, fmt, ap);
  719. va_end(ap);
  720. if (IS_ERR(req))
  721. return req;
  722. if (signal_pending(current)) {
  723. sigpending = 1;
  724. clear_thread_flag(TIF_SIGPENDING);
  725. } else
  726. sigpending = 0;
  727. err = c->trans_mod->zc_request(c, req, uidata, uodata,
  728. inlen, olen, in_hdrlen);
  729. if (err < 0) {
  730. if (err == -EIO)
  731. c->status = Disconnected;
  732. if (err != -ERESTARTSYS)
  733. goto reterr;
  734. }
  735. if (req->status == REQ_STATUS_ERROR) {
  736. p9_debug(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err);
  737. err = req->t_err;
  738. }
  739. if ((err == -ERESTARTSYS) && (c->status == Connected)) {
  740. p9_debug(P9_DEBUG_MUX, "flushing\n");
  741. sigpending = 1;
  742. clear_thread_flag(TIF_SIGPENDING);
  743. if (c->trans_mod->cancel(c, req))
  744. p9_client_flush(c, req);
  745. /* if we received the response anyway, don't signal error */
  746. if (req->status == REQ_STATUS_RCVD)
  747. err = 0;
  748. }
  749. if (sigpending) {
  750. spin_lock_irqsave(&current->sighand->siglock, flags);
  751. recalc_sigpending();
  752. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  753. }
  754. if (err < 0)
  755. goto reterr;
  756. err = p9_check_zc_errors(c, req, uidata, in_hdrlen);
  757. trace_9p_client_res(c, type, req->rc->tag, err);
  758. if (!err)
  759. return req;
  760. reterr:
  761. p9_free_req(c, req);
  762. return ERR_PTR(safe_errno(err));
  763. }
  764. static struct p9_fid *p9_fid_create(struct p9_client *clnt)
  765. {
  766. int ret;
  767. struct p9_fid *fid;
  768. unsigned long flags;
  769. p9_debug(P9_DEBUG_FID, "clnt %p\n", clnt);
  770. fid = kmalloc(sizeof(struct p9_fid), GFP_KERNEL);
  771. if (!fid)
  772. return ERR_PTR(-ENOMEM);
  773. ret = p9_idpool_get(clnt->fidpool);
  774. if (ret < 0) {
  775. ret = -ENOSPC;
  776. goto error;
  777. }
  778. fid->fid = ret;
  779. memset(&fid->qid, 0, sizeof(struct p9_qid));
  780. fid->mode = -1;
  781. fid->uid = current_fsuid();
  782. fid->clnt = clnt;
  783. fid->rdir = NULL;
  784. spin_lock_irqsave(&clnt->lock, flags);
  785. list_add(&fid->flist, &clnt->fidlist);
  786. spin_unlock_irqrestore(&clnt->lock, flags);
  787. return fid;
  788. error:
  789. kfree(fid);
  790. return ERR_PTR(ret);
  791. }
  792. static void p9_fid_destroy(struct p9_fid *fid)
  793. {
  794. struct p9_client *clnt;
  795. unsigned long flags;
  796. p9_debug(P9_DEBUG_FID, "fid %d\n", fid->fid);
  797. clnt = fid->clnt;
  798. p9_idpool_put(fid->fid, clnt->fidpool);
  799. spin_lock_irqsave(&clnt->lock, flags);
  800. list_del(&fid->flist);
  801. spin_unlock_irqrestore(&clnt->lock, flags);
  802. kfree(fid->rdir);
  803. kfree(fid);
  804. }
  805. static int p9_client_version(struct p9_client *c)
  806. {
  807. int err = 0;
  808. struct p9_req_t *req;
  809. char *version;
  810. int msize;
  811. p9_debug(P9_DEBUG_9P, ">>> TVERSION msize %d protocol %d\n",
  812. c->msize, c->proto_version);
  813. switch (c->proto_version) {
  814. case p9_proto_2000L:
  815. req = p9_client_rpc(c, P9_TVERSION, "ds",
  816. c->msize, "9P2000.L");
  817. break;
  818. case p9_proto_2000u:
  819. req = p9_client_rpc(c, P9_TVERSION, "ds",
  820. c->msize, "9P2000.u");
  821. break;
  822. case p9_proto_legacy:
  823. req = p9_client_rpc(c, P9_TVERSION, "ds",
  824. c->msize, "9P2000");
  825. break;
  826. default:
  827. return -EINVAL;
  828. }
  829. if (IS_ERR(req))
  830. return PTR_ERR(req);
  831. err = p9pdu_readf(req->rc, c->proto_version, "ds", &msize, &version);
  832. if (err) {
  833. p9_debug(P9_DEBUG_9P, "version error %d\n", err);
  834. trace_9p_protocol_dump(c, req->rc);
  835. goto error;
  836. }
  837. p9_debug(P9_DEBUG_9P, "<<< RVERSION msize %d %s\n", msize, version);
  838. if (!strncmp(version, "9P2000.L", 8))
  839. c->proto_version = p9_proto_2000L;
  840. else if (!strncmp(version, "9P2000.u", 8))
  841. c->proto_version = p9_proto_2000u;
  842. else if (!strncmp(version, "9P2000", 6))
  843. c->proto_version = p9_proto_legacy;
  844. else {
  845. err = -EREMOTEIO;
  846. goto error;
  847. }
  848. if (msize < c->msize)
  849. c->msize = msize;
  850. error:
  851. kfree(version);
  852. p9_free_req(c, req);
  853. return err;
  854. }
  855. struct p9_client *p9_client_create(const char *dev_name, char *options)
  856. {
  857. int err;
  858. struct p9_client *clnt;
  859. char *client_id;
  860. err = 0;
  861. clnt = kmalloc(sizeof(struct p9_client), GFP_KERNEL);
  862. if (!clnt)
  863. return ERR_PTR(-ENOMEM);
  864. clnt->trans_mod = NULL;
  865. clnt->trans = NULL;
  866. client_id = utsname()->nodename;
  867. memcpy(clnt->name, client_id, strlen(client_id) + 1);
  868. spin_lock_init(&clnt->lock);
  869. INIT_LIST_HEAD(&clnt->fidlist);
  870. err = p9_tag_init(clnt);
  871. if (err < 0)
  872. goto free_client;
  873. err = parse_opts(options, clnt);
  874. if (err < 0)
  875. goto destroy_tagpool;
  876. if (!clnt->trans_mod)
  877. clnt->trans_mod = v9fs_get_default_trans();
  878. if (clnt->trans_mod == NULL) {
  879. err = -EPROTONOSUPPORT;
  880. p9_debug(P9_DEBUG_ERROR,
  881. "No transport defined or default transport\n");
  882. goto destroy_tagpool;
  883. }
  884. clnt->fidpool = p9_idpool_create();
  885. if (IS_ERR(clnt->fidpool)) {
  886. err = PTR_ERR(clnt->fidpool);
  887. goto put_trans;
  888. }
  889. p9_debug(P9_DEBUG_MUX, "clnt %p trans %p msize %d protocol %d\n",
  890. clnt, clnt->trans_mod, clnt->msize, clnt->proto_version);
  891. err = clnt->trans_mod->create(clnt, dev_name, options);
  892. if (err)
  893. goto destroy_fidpool;
  894. if (clnt->msize > clnt->trans_mod->maxsize)
  895. clnt->msize = clnt->trans_mod->maxsize;
  896. err = p9_client_version(clnt);
  897. if (err)
  898. goto close_trans;
  899. return clnt;
  900. close_trans:
  901. clnt->trans_mod->close(clnt);
  902. destroy_fidpool:
  903. p9_idpool_destroy(clnt->fidpool);
  904. put_trans:
  905. v9fs_put_trans(clnt->trans_mod);
  906. destroy_tagpool:
  907. p9_idpool_destroy(clnt->tagpool);
  908. free_client:
  909. kfree(clnt);
  910. return ERR_PTR(err);
  911. }
  912. EXPORT_SYMBOL(p9_client_create);
  913. void p9_client_destroy(struct p9_client *clnt)
  914. {
  915. struct p9_fid *fid, *fidptr;
  916. p9_debug(P9_DEBUG_MUX, "clnt %p\n", clnt);
  917. if (clnt->trans_mod)
  918. clnt->trans_mod->close(clnt);
  919. v9fs_put_trans(clnt->trans_mod);
  920. list_for_each_entry_safe(fid, fidptr, &clnt->fidlist, flist) {
  921. pr_info("Found fid %d not clunked\n", fid->fid);
  922. p9_fid_destroy(fid);
  923. }
  924. if (clnt->fidpool)
  925. p9_idpool_destroy(clnt->fidpool);
  926. p9_tag_cleanup(clnt);
  927. kfree(clnt);
  928. }
  929. EXPORT_SYMBOL(p9_client_destroy);
  930. void p9_client_disconnect(struct p9_client *clnt)
  931. {
  932. p9_debug(P9_DEBUG_9P, "clnt %p\n", clnt);
  933. clnt->status = Disconnected;
  934. }
  935. EXPORT_SYMBOL(p9_client_disconnect);
  936. void p9_client_begin_disconnect(struct p9_client *clnt)
  937. {
  938. p9_debug(P9_DEBUG_9P, "clnt %p\n", clnt);
  939. clnt->status = BeginDisconnect;
  940. }
  941. EXPORT_SYMBOL(p9_client_begin_disconnect);
  942. struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
  943. char *uname, kuid_t n_uname, char *aname)
  944. {
  945. int err = 0;
  946. struct p9_req_t *req;
  947. struct p9_fid *fid;
  948. struct p9_qid qid;
  949. p9_debug(P9_DEBUG_9P, ">>> TATTACH afid %d uname %s aname %s\n",
  950. afid ? afid->fid : -1, uname, aname);
  951. fid = p9_fid_create(clnt);
  952. if (IS_ERR(fid)) {
  953. err = PTR_ERR(fid);
  954. fid = NULL;
  955. goto error;
  956. }
  957. fid->uid = n_uname;
  958. req = p9_client_rpc(clnt, P9_TATTACH, "ddss?u", fid->fid,
  959. afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
  960. if (IS_ERR(req)) {
  961. err = PTR_ERR(req);
  962. goto error;
  963. }
  964. err = p9pdu_readf(req->rc, clnt->proto_version, "Q", &qid);
  965. if (err) {
  966. trace_9p_protocol_dump(clnt, req->rc);
  967. p9_free_req(clnt, req);
  968. goto error;
  969. }
  970. p9_debug(P9_DEBUG_9P, "<<< RATTACH qid %x.%llx.%x\n",
  971. qid.type, (unsigned long long)qid.path, qid.version);
  972. memmove(&fid->qid, &qid, sizeof(struct p9_qid));
  973. p9_free_req(clnt, req);
  974. return fid;
  975. error:
  976. if (fid)
  977. p9_fid_destroy(fid);
  978. return ERR_PTR(err);
  979. }
  980. EXPORT_SYMBOL(p9_client_attach);
  981. struct p9_fid *p9_client_walk(struct p9_fid *oldfid, uint16_t nwname,
  982. char **wnames, int clone)
  983. {
  984. int err;
  985. struct p9_client *clnt;
  986. struct p9_fid *fid;
  987. struct p9_qid *wqids;
  988. struct p9_req_t *req;
  989. uint16_t nwqids, count;
  990. err = 0;
  991. wqids = NULL;
  992. clnt = oldfid->clnt;
  993. if (clone) {
  994. fid = p9_fid_create(clnt);
  995. if (IS_ERR(fid)) {
  996. err = PTR_ERR(fid);
  997. fid = NULL;
  998. goto error;
  999. }
  1000. fid->uid = oldfid->uid;
  1001. } else
  1002. fid = oldfid;
  1003. p9_debug(P9_DEBUG_9P, ">>> TWALK fids %d,%d nwname %ud wname[0] %s\n",
  1004. oldfid->fid, fid->fid, nwname, wnames ? wnames[0] : NULL);
  1005. req = p9_client_rpc(clnt, P9_TWALK, "ddT", oldfid->fid, fid->fid,
  1006. nwname, wnames);
  1007. if (IS_ERR(req)) {
  1008. err = PTR_ERR(req);
  1009. goto error;
  1010. }
  1011. err = p9pdu_readf(req->rc, clnt->proto_version, "R", &nwqids, &wqids);
  1012. if (err) {
  1013. trace_9p_protocol_dump(clnt, req->rc);
  1014. p9_free_req(clnt, req);
  1015. goto clunk_fid;
  1016. }
  1017. p9_free_req(clnt, req);
  1018. p9_debug(P9_DEBUG_9P, "<<< RWALK nwqid %d:\n", nwqids);
  1019. if (nwqids != nwname) {
  1020. err = -ENOENT;
  1021. goto clunk_fid;
  1022. }
  1023. for (count = 0; count < nwqids; count++)
  1024. p9_debug(P9_DEBUG_9P, "<<< [%d] %x.%llx.%x\n",
  1025. count, wqids[count].type,
  1026. (unsigned long long)wqids[count].path,
  1027. wqids[count].version);
  1028. if (nwname)
  1029. memmove(&fid->qid, &wqids[nwqids - 1], sizeof(struct p9_qid));
  1030. else
  1031. fid->qid = oldfid->qid;
  1032. kfree(wqids);
  1033. return fid;
  1034. clunk_fid:
  1035. kfree(wqids);
  1036. p9_client_clunk(fid);
  1037. fid = NULL;
  1038. error:
  1039. if (fid && (fid != oldfid))
  1040. p9_fid_destroy(fid);
  1041. return ERR_PTR(err);
  1042. }
  1043. EXPORT_SYMBOL(p9_client_walk);
  1044. int p9_client_open(struct p9_fid *fid, int mode)
  1045. {
  1046. int err;
  1047. struct p9_client *clnt;
  1048. struct p9_req_t *req;
  1049. struct p9_qid qid;
  1050. int iounit;
  1051. clnt = fid->clnt;
  1052. p9_debug(P9_DEBUG_9P, ">>> %s fid %d mode %d\n",
  1053. p9_is_proto_dotl(clnt) ? "TLOPEN" : "TOPEN", fid->fid, mode);
  1054. err = 0;
  1055. if (fid->mode != -1)
  1056. return -EINVAL;
  1057. if (p9_is_proto_dotl(clnt))
  1058. req = p9_client_rpc(clnt, P9_TLOPEN, "dd", fid->fid, mode);
  1059. else
  1060. req = p9_client_rpc(clnt, P9_TOPEN, "db", fid->fid, mode);
  1061. if (IS_ERR(req)) {
  1062. err = PTR_ERR(req);
  1063. goto error;
  1064. }
  1065. err = p9pdu_readf(req->rc, clnt->proto_version, "Qd", &qid, &iounit);
  1066. if (err) {
  1067. trace_9p_protocol_dump(clnt, req->rc);
  1068. goto free_and_error;
  1069. }
  1070. p9_debug(P9_DEBUG_9P, "<<< %s qid %x.%llx.%x iounit %x\n",
  1071. p9_is_proto_dotl(clnt) ? "RLOPEN" : "ROPEN", qid.type,
  1072. (unsigned long long)qid.path, qid.version, iounit);
  1073. fid->mode = mode;
  1074. fid->iounit = iounit;
  1075. free_and_error:
  1076. p9_free_req(clnt, req);
  1077. error:
  1078. return err;
  1079. }
  1080. EXPORT_SYMBOL(p9_client_open);
  1081. int p9_client_create_dotl(struct p9_fid *ofid, char *name, u32 flags, u32 mode,
  1082. kgid_t gid, struct p9_qid *qid)
  1083. {
  1084. int err = 0;
  1085. struct p9_client *clnt;
  1086. struct p9_req_t *req;
  1087. int iounit;
  1088. p9_debug(P9_DEBUG_9P,
  1089. ">>> TLCREATE fid %d name %s flags %d mode %d gid %d\n",
  1090. ofid->fid, name, flags, mode,
  1091. from_kgid(&init_user_ns, gid));
  1092. clnt = ofid->clnt;
  1093. if (ofid->mode != -1)
  1094. return -EINVAL;
  1095. req = p9_client_rpc(clnt, P9_TLCREATE, "dsddg", ofid->fid, name, flags,
  1096. mode, gid);
  1097. if (IS_ERR(req)) {
  1098. err = PTR_ERR(req);
  1099. goto error;
  1100. }
  1101. err = p9pdu_readf(req->rc, clnt->proto_version, "Qd", qid, &iounit);
  1102. if (err) {
  1103. trace_9p_protocol_dump(clnt, req->rc);
  1104. goto free_and_error;
  1105. }
  1106. p9_debug(P9_DEBUG_9P, "<<< RLCREATE qid %x.%llx.%x iounit %x\n",
  1107. qid->type,
  1108. (unsigned long long)qid->path,
  1109. qid->version, iounit);
  1110. ofid->mode = mode;
  1111. ofid->iounit = iounit;
  1112. free_and_error:
  1113. p9_free_req(clnt, req);
  1114. error:
  1115. return err;
  1116. }
  1117. EXPORT_SYMBOL(p9_client_create_dotl);
  1118. int p9_client_fcreate(struct p9_fid *fid, char *name, u32 perm, int mode,
  1119. char *extension)
  1120. {
  1121. int err;
  1122. struct p9_client *clnt;
  1123. struct p9_req_t *req;
  1124. struct p9_qid qid;
  1125. int iounit;
  1126. p9_debug(P9_DEBUG_9P, ">>> TCREATE fid %d name %s perm %d mode %d\n",
  1127. fid->fid, name, perm, mode);
  1128. err = 0;
  1129. clnt = fid->clnt;
  1130. if (fid->mode != -1)
  1131. return -EINVAL;
  1132. req = p9_client_rpc(clnt, P9_TCREATE, "dsdb?s", fid->fid, name, perm,
  1133. mode, extension);
  1134. if (IS_ERR(req)) {
  1135. err = PTR_ERR(req);
  1136. goto error;
  1137. }
  1138. err = p9pdu_readf(req->rc, clnt->proto_version, "Qd", &qid, &iounit);
  1139. if (err) {
  1140. trace_9p_protocol_dump(clnt, req->rc);
  1141. goto free_and_error;
  1142. }
  1143. p9_debug(P9_DEBUG_9P, "<<< RCREATE qid %x.%llx.%x iounit %x\n",
  1144. qid.type,
  1145. (unsigned long long)qid.path,
  1146. qid.version, iounit);
  1147. fid->mode = mode;
  1148. fid->iounit = iounit;
  1149. free_and_error:
  1150. p9_free_req(clnt, req);
  1151. error:
  1152. return err;
  1153. }
  1154. EXPORT_SYMBOL(p9_client_fcreate);
  1155. int p9_client_symlink(struct p9_fid *dfid, char *name, char *symtgt, kgid_t gid,
  1156. struct p9_qid *qid)
  1157. {
  1158. int err = 0;
  1159. struct p9_client *clnt;
  1160. struct p9_req_t *req;
  1161. p9_debug(P9_DEBUG_9P, ">>> TSYMLINK dfid %d name %s symtgt %s\n",
  1162. dfid->fid, name, symtgt);
  1163. clnt = dfid->clnt;
  1164. req = p9_client_rpc(clnt, P9_TSYMLINK, "dssg", dfid->fid, name, symtgt,
  1165. gid);
  1166. if (IS_ERR(req)) {
  1167. err = PTR_ERR(req);
  1168. goto error;
  1169. }
  1170. err = p9pdu_readf(req->rc, clnt->proto_version, "Q", qid);
  1171. if (err) {
  1172. trace_9p_protocol_dump(clnt, req->rc);
  1173. goto free_and_error;
  1174. }
  1175. p9_debug(P9_DEBUG_9P, "<<< RSYMLINK qid %x.%llx.%x\n",
  1176. qid->type, (unsigned long long)qid->path, qid->version);
  1177. free_and_error:
  1178. p9_free_req(clnt, req);
  1179. error:
  1180. return err;
  1181. }
  1182. EXPORT_SYMBOL(p9_client_symlink);
  1183. int p9_client_link(struct p9_fid *dfid, struct p9_fid *oldfid, char *newname)
  1184. {
  1185. struct p9_client *clnt;
  1186. struct p9_req_t *req;
  1187. p9_debug(P9_DEBUG_9P, ">>> TLINK dfid %d oldfid %d newname %s\n",
  1188. dfid->fid, oldfid->fid, newname);
  1189. clnt = dfid->clnt;
  1190. req = p9_client_rpc(clnt, P9_TLINK, "dds", dfid->fid, oldfid->fid,
  1191. newname);
  1192. if (IS_ERR(req))
  1193. return PTR_ERR(req);
  1194. p9_debug(P9_DEBUG_9P, "<<< RLINK\n");
  1195. p9_free_req(clnt, req);
  1196. return 0;
  1197. }
  1198. EXPORT_SYMBOL(p9_client_link);
  1199. int p9_client_fsync(struct p9_fid *fid, int datasync)
  1200. {
  1201. int err;
  1202. struct p9_client *clnt;
  1203. struct p9_req_t *req;
  1204. p9_debug(P9_DEBUG_9P, ">>> TFSYNC fid %d datasync:%d\n",
  1205. fid->fid, datasync);
  1206. err = 0;
  1207. clnt = fid->clnt;
  1208. req = p9_client_rpc(clnt, P9_TFSYNC, "dd", fid->fid, datasync);
  1209. if (IS_ERR(req)) {
  1210. err = PTR_ERR(req);
  1211. goto error;
  1212. }
  1213. p9_debug(P9_DEBUG_9P, "<<< RFSYNC fid %d\n", fid->fid);
  1214. p9_free_req(clnt, req);
  1215. error:
  1216. return err;
  1217. }
  1218. EXPORT_SYMBOL(p9_client_fsync);
  1219. int p9_client_clunk(struct p9_fid *fid)
  1220. {
  1221. int err;
  1222. struct p9_client *clnt;
  1223. struct p9_req_t *req;
  1224. int retries = 0;
  1225. if (!fid) {
  1226. pr_warn("%s (%d): Trying to clunk with NULL fid\n",
  1227. __func__, task_pid_nr(current));
  1228. dump_stack();
  1229. return 0;
  1230. }
  1231. again:
  1232. p9_debug(P9_DEBUG_9P, ">>> TCLUNK fid %d (try %d)\n", fid->fid,
  1233. retries);
  1234. err = 0;
  1235. clnt = fid->clnt;
  1236. req = p9_client_rpc(clnt, P9_TCLUNK, "d", fid->fid);
  1237. if (IS_ERR(req)) {
  1238. err = PTR_ERR(req);
  1239. goto error;
  1240. }
  1241. p9_debug(P9_DEBUG_9P, "<<< RCLUNK fid %d\n", fid->fid);
  1242. p9_free_req(clnt, req);
  1243. error:
  1244. /*
  1245. * Fid is not valid even after a failed clunk
  1246. * If interrupted, retry once then give up and
  1247. * leak fid until umount.
  1248. */
  1249. if (err == -ERESTARTSYS) {
  1250. if (retries++ == 0)
  1251. goto again;
  1252. } else
  1253. p9_fid_destroy(fid);
  1254. return err;
  1255. }
  1256. EXPORT_SYMBOL(p9_client_clunk);
  1257. int p9_client_remove(struct p9_fid *fid)
  1258. {
  1259. int err;
  1260. struct p9_client *clnt;
  1261. struct p9_req_t *req;
  1262. p9_debug(P9_DEBUG_9P, ">>> TREMOVE fid %d\n", fid->fid);
  1263. err = 0;
  1264. clnt = fid->clnt;
  1265. req = p9_client_rpc(clnt, P9_TREMOVE, "d", fid->fid);
  1266. if (IS_ERR(req)) {
  1267. err = PTR_ERR(req);
  1268. goto error;
  1269. }
  1270. p9_debug(P9_DEBUG_9P, "<<< RREMOVE fid %d\n", fid->fid);
  1271. p9_free_req(clnt, req);
  1272. error:
  1273. if (err == -ERESTARTSYS)
  1274. p9_client_clunk(fid);
  1275. else
  1276. p9_fid_destroy(fid);
  1277. return err;
  1278. }
  1279. EXPORT_SYMBOL(p9_client_remove);
  1280. int p9_client_unlinkat(struct p9_fid *dfid, const char *name, int flags)
  1281. {
  1282. int err = 0;
  1283. struct p9_req_t *req;
  1284. struct p9_client *clnt;
  1285. p9_debug(P9_DEBUG_9P, ">>> TUNLINKAT fid %d %s %d\n",
  1286. dfid->fid, name, flags);
  1287. clnt = dfid->clnt;
  1288. req = p9_client_rpc(clnt, P9_TUNLINKAT, "dsd", dfid->fid, name, flags);
  1289. if (IS_ERR(req)) {
  1290. err = PTR_ERR(req);
  1291. goto error;
  1292. }
  1293. p9_debug(P9_DEBUG_9P, "<<< RUNLINKAT fid %d %s\n", dfid->fid, name);
  1294. p9_free_req(clnt, req);
  1295. error:
  1296. return err;
  1297. }
  1298. EXPORT_SYMBOL(p9_client_unlinkat);
  1299. int
  1300. p9_client_read(struct p9_fid *fid, u64 offset, struct iov_iter *to, int *err)
  1301. {
  1302. struct p9_client *clnt = fid->clnt;
  1303. struct p9_req_t *req;
  1304. int total = 0;
  1305. *err = 0;
  1306. p9_debug(P9_DEBUG_9P, ">>> TREAD fid %d offset %llu %d\n",
  1307. fid->fid, (unsigned long long) offset, (int)iov_iter_count(to));
  1308. while (iov_iter_count(to)) {
  1309. int count = iov_iter_count(to);
  1310. int rsize, non_zc = 0;
  1311. char *dataptr;
  1312. rsize = fid->iounit;
  1313. if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
  1314. rsize = clnt->msize - P9_IOHDRSZ;
  1315. if (count < rsize)
  1316. rsize = count;
  1317. /* Don't bother zerocopy for small IO (< 1024) */
  1318. if (clnt->trans_mod->zc_request && rsize > 1024) {
  1319. /*
  1320. * response header len is 11
  1321. * PDU Header(7) + IO Size (4)
  1322. */
  1323. req = p9_client_zc_rpc(clnt, P9_TREAD, to, NULL, rsize,
  1324. 0, 11, "dqd", fid->fid,
  1325. offset, rsize);
  1326. } else {
  1327. non_zc = 1;
  1328. req = p9_client_rpc(clnt, P9_TREAD, "dqd", fid->fid, offset,
  1329. rsize);
  1330. }
  1331. if (IS_ERR(req)) {
  1332. *err = PTR_ERR(req);
  1333. break;
  1334. }
  1335. *err = p9pdu_readf(req->rc, clnt->proto_version,
  1336. "D", &count, &dataptr);
  1337. if (*err) {
  1338. trace_9p_protocol_dump(clnt, req->rc);
  1339. p9_free_req(clnt, req);
  1340. break;
  1341. }
  1342. if (rsize < count) {
  1343. pr_err("bogus RREAD count (%d > %d)\n", count, rsize);
  1344. count = rsize;
  1345. }
  1346. p9_debug(P9_DEBUG_9P, "<<< RREAD count %d\n", count);
  1347. if (!count) {
  1348. p9_free_req(clnt, req);
  1349. break;
  1350. }
  1351. if (non_zc) {
  1352. int n = copy_to_iter(dataptr, count, to);
  1353. total += n;
  1354. offset += n;
  1355. if (n != count) {
  1356. *err = -EFAULT;
  1357. p9_free_req(clnt, req);
  1358. break;
  1359. }
  1360. } else {
  1361. iov_iter_advance(to, count);
  1362. total += count;
  1363. offset += count;
  1364. }
  1365. p9_free_req(clnt, req);
  1366. }
  1367. return total;
  1368. }
  1369. EXPORT_SYMBOL(p9_client_read);
  1370. int
  1371. p9_client_write(struct p9_fid *fid, u64 offset, struct iov_iter *from, int *err)
  1372. {
  1373. struct p9_client *clnt = fid->clnt;
  1374. struct p9_req_t *req;
  1375. int total = 0;
  1376. *err = 0;
  1377. p9_debug(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %zd\n",
  1378. fid->fid, (unsigned long long) offset,
  1379. iov_iter_count(from));
  1380. while (iov_iter_count(from)) {
  1381. int count = iov_iter_count(from);
  1382. int rsize = fid->iounit;
  1383. if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
  1384. rsize = clnt->msize - P9_IOHDRSZ;
  1385. if (count < rsize)
  1386. rsize = count;
  1387. /* Don't bother zerocopy for small IO (< 1024) */
  1388. if (clnt->trans_mod->zc_request && rsize > 1024) {
  1389. req = p9_client_zc_rpc(clnt, P9_TWRITE, NULL, from, 0,
  1390. rsize, P9_ZC_HDR_SZ, "dqd",
  1391. fid->fid, offset, rsize);
  1392. } else {
  1393. req = p9_client_rpc(clnt, P9_TWRITE, "dqV", fid->fid,
  1394. offset, rsize, from);
  1395. }
  1396. if (IS_ERR(req)) {
  1397. *err = PTR_ERR(req);
  1398. break;
  1399. }
  1400. *err = p9pdu_readf(req->rc, clnt->proto_version, "d", &count);
  1401. if (*err) {
  1402. trace_9p_protocol_dump(clnt, req->rc);
  1403. p9_free_req(clnt, req);
  1404. break;
  1405. }
  1406. if (rsize < count) {
  1407. pr_err("bogus RWRITE count (%d > %d)\n", count, rsize);
  1408. count = rsize;
  1409. }
  1410. p9_debug(P9_DEBUG_9P, "<<< RWRITE count %d\n", count);
  1411. p9_free_req(clnt, req);
  1412. iov_iter_advance(from, count);
  1413. total += count;
  1414. offset += count;
  1415. }
  1416. return total;
  1417. }
  1418. EXPORT_SYMBOL(p9_client_write);
  1419. struct p9_wstat *p9_client_stat(struct p9_fid *fid)
  1420. {
  1421. int err;
  1422. struct p9_client *clnt;
  1423. struct p9_wstat *ret = kmalloc(sizeof(struct p9_wstat), GFP_KERNEL);
  1424. struct p9_req_t *req;
  1425. u16 ignored;
  1426. p9_debug(P9_DEBUG_9P, ">>> TSTAT fid %d\n", fid->fid);
  1427. if (!ret)
  1428. return ERR_PTR(-ENOMEM);
  1429. err = 0;
  1430. clnt = fid->clnt;
  1431. req = p9_client_rpc(clnt, P9_TSTAT, "d", fid->fid);
  1432. if (IS_ERR(req)) {
  1433. err = PTR_ERR(req);
  1434. goto error;
  1435. }
  1436. err = p9pdu_readf(req->rc, clnt->proto_version, "wS", &ignored, ret);
  1437. if (err) {
  1438. trace_9p_protocol_dump(clnt, req->rc);
  1439. p9_free_req(clnt, req);
  1440. goto error;
  1441. }
  1442. p9_debug(P9_DEBUG_9P,
  1443. "<<< RSTAT sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
  1444. "<<< mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
  1445. "<<< name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
  1446. "<<< uid=%d gid=%d n_muid=%d\n",
  1447. ret->size, ret->type, ret->dev, ret->qid.type,
  1448. (unsigned long long)ret->qid.path, ret->qid.version, ret->mode,
  1449. ret->atime, ret->mtime, (unsigned long long)ret->length,
  1450. ret->name, ret->uid, ret->gid, ret->muid, ret->extension,
  1451. from_kuid(&init_user_ns, ret->n_uid),
  1452. from_kgid(&init_user_ns, ret->n_gid),
  1453. from_kuid(&init_user_ns, ret->n_muid));
  1454. p9_free_req(clnt, req);
  1455. return ret;
  1456. error:
  1457. kfree(ret);
  1458. return ERR_PTR(err);
  1459. }
  1460. EXPORT_SYMBOL(p9_client_stat);
  1461. struct p9_stat_dotl *p9_client_getattr_dotl(struct p9_fid *fid,
  1462. u64 request_mask)
  1463. {
  1464. int err;
  1465. struct p9_client *clnt;
  1466. struct p9_stat_dotl *ret = kmalloc(sizeof(struct p9_stat_dotl),
  1467. GFP_KERNEL);
  1468. struct p9_req_t *req;
  1469. p9_debug(P9_DEBUG_9P, ">>> TGETATTR fid %d, request_mask %lld\n",
  1470. fid->fid, request_mask);
  1471. if (!ret)
  1472. return ERR_PTR(-ENOMEM);
  1473. err = 0;
  1474. clnt = fid->clnt;
  1475. req = p9_client_rpc(clnt, P9_TGETATTR, "dq", fid->fid, request_mask);
  1476. if (IS_ERR(req)) {
  1477. err = PTR_ERR(req);
  1478. goto error;
  1479. }
  1480. err = p9pdu_readf(req->rc, clnt->proto_version, "A", ret);
  1481. if (err) {
  1482. trace_9p_protocol_dump(clnt, req->rc);
  1483. p9_free_req(clnt, req);
  1484. goto error;
  1485. }
  1486. p9_debug(P9_DEBUG_9P,
  1487. "<<< RGETATTR st_result_mask=%lld\n"
  1488. "<<< qid=%x.%llx.%x\n"
  1489. "<<< st_mode=%8.8x st_nlink=%llu\n"
  1490. "<<< st_uid=%d st_gid=%d\n"
  1491. "<<< st_rdev=%llx st_size=%llx st_blksize=%llu st_blocks=%llu\n"
  1492. "<<< st_atime_sec=%lld st_atime_nsec=%lld\n"
  1493. "<<< st_mtime_sec=%lld st_mtime_nsec=%lld\n"
  1494. "<<< st_ctime_sec=%lld st_ctime_nsec=%lld\n"
  1495. "<<< st_btime_sec=%lld st_btime_nsec=%lld\n"
  1496. "<<< st_gen=%lld st_data_version=%lld",
  1497. ret->st_result_mask, ret->qid.type, ret->qid.path,
  1498. ret->qid.version, ret->st_mode, ret->st_nlink,
  1499. from_kuid(&init_user_ns, ret->st_uid),
  1500. from_kgid(&init_user_ns, ret->st_gid),
  1501. ret->st_rdev, ret->st_size, ret->st_blksize,
  1502. ret->st_blocks, ret->st_atime_sec, ret->st_atime_nsec,
  1503. ret->st_mtime_sec, ret->st_mtime_nsec, ret->st_ctime_sec,
  1504. ret->st_ctime_nsec, ret->st_btime_sec, ret->st_btime_nsec,
  1505. ret->st_gen, ret->st_data_version);
  1506. p9_free_req(clnt, req);
  1507. return ret;
  1508. error:
  1509. kfree(ret);
  1510. return ERR_PTR(err);
  1511. }
  1512. EXPORT_SYMBOL(p9_client_getattr_dotl);
  1513. static int p9_client_statsize(struct p9_wstat *wst, int proto_version)
  1514. {
  1515. int ret;
  1516. /* NOTE: size shouldn't include its own length */
  1517. /* size[2] type[2] dev[4] qid[13] */
  1518. /* mode[4] atime[4] mtime[4] length[8]*/
  1519. /* name[s] uid[s] gid[s] muid[s] */
  1520. ret = 2+4+13+4+4+4+8+2+2+2+2;
  1521. if (wst->name)
  1522. ret += strlen(wst->name);
  1523. if (wst->uid)
  1524. ret += strlen(wst->uid);
  1525. if (wst->gid)
  1526. ret += strlen(wst->gid);
  1527. if (wst->muid)
  1528. ret += strlen(wst->muid);
  1529. if ((proto_version == p9_proto_2000u) ||
  1530. (proto_version == p9_proto_2000L)) {
  1531. ret += 2+4+4+4; /* extension[s] n_uid[4] n_gid[4] n_muid[4] */
  1532. if (wst->extension)
  1533. ret += strlen(wst->extension);
  1534. }
  1535. return ret;
  1536. }
  1537. int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst)
  1538. {
  1539. int err;
  1540. struct p9_req_t *req;
  1541. struct p9_client *clnt;
  1542. err = 0;
  1543. clnt = fid->clnt;
  1544. wst->size = p9_client_statsize(wst, clnt->proto_version);
  1545. p9_debug(P9_DEBUG_9P, ">>> TWSTAT fid %d\n", fid->fid);
  1546. p9_debug(P9_DEBUG_9P,
  1547. " sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
  1548. " mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
  1549. " name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
  1550. " uid=%d gid=%d n_muid=%d\n",
  1551. wst->size, wst->type, wst->dev, wst->qid.type,
  1552. (unsigned long long)wst->qid.path, wst->qid.version, wst->mode,
  1553. wst->atime, wst->mtime, (unsigned long long)wst->length,
  1554. wst->name, wst->uid, wst->gid, wst->muid, wst->extension,
  1555. from_kuid(&init_user_ns, wst->n_uid),
  1556. from_kgid(&init_user_ns, wst->n_gid),
  1557. from_kuid(&init_user_ns, wst->n_muid));
  1558. req = p9_client_rpc(clnt, P9_TWSTAT, "dwS", fid->fid, wst->size+2, wst);
  1559. if (IS_ERR(req)) {
  1560. err = PTR_ERR(req);
  1561. goto error;
  1562. }
  1563. p9_debug(P9_DEBUG_9P, "<<< RWSTAT fid %d\n", fid->fid);
  1564. p9_free_req(clnt, req);
  1565. error:
  1566. return err;
  1567. }
  1568. EXPORT_SYMBOL(p9_client_wstat);
  1569. int p9_client_setattr(struct p9_fid *fid, struct p9_iattr_dotl *p9attr)
  1570. {
  1571. int err;
  1572. struct p9_req_t *req;
  1573. struct p9_client *clnt;
  1574. err = 0;
  1575. clnt = fid->clnt;
  1576. p9_debug(P9_DEBUG_9P, ">>> TSETATTR fid %d\n", fid->fid);
  1577. p9_debug(P9_DEBUG_9P,
  1578. " valid=%x mode=%x uid=%d gid=%d size=%lld\n"
  1579. " atime_sec=%lld atime_nsec=%lld\n"
  1580. " mtime_sec=%lld mtime_nsec=%lld\n",
  1581. p9attr->valid, p9attr->mode,
  1582. from_kuid(&init_user_ns, p9attr->uid),
  1583. from_kgid(&init_user_ns, p9attr->gid),
  1584. p9attr->size, p9attr->atime_sec, p9attr->atime_nsec,
  1585. p9attr->mtime_sec, p9attr->mtime_nsec);
  1586. req = p9_client_rpc(clnt, P9_TSETATTR, "dI", fid->fid, p9attr);
  1587. if (IS_ERR(req)) {
  1588. err = PTR_ERR(req);
  1589. goto error;
  1590. }
  1591. p9_debug(P9_DEBUG_9P, "<<< RSETATTR fid %d\n", fid->fid);
  1592. p9_free_req(clnt, req);
  1593. error:
  1594. return err;
  1595. }
  1596. EXPORT_SYMBOL(p9_client_setattr);
  1597. int p9_client_statfs(struct p9_fid *fid, struct p9_rstatfs *sb)
  1598. {
  1599. int err;
  1600. struct p9_req_t *req;
  1601. struct p9_client *clnt;
  1602. err = 0;
  1603. clnt = fid->clnt;
  1604. p9_debug(P9_DEBUG_9P, ">>> TSTATFS fid %d\n", fid->fid);
  1605. req = p9_client_rpc(clnt, P9_TSTATFS, "d", fid->fid);
  1606. if (IS_ERR(req)) {
  1607. err = PTR_ERR(req);
  1608. goto error;
  1609. }
  1610. err = p9pdu_readf(req->rc, clnt->proto_version, "ddqqqqqqd", &sb->type,
  1611. &sb->bsize, &sb->blocks, &sb->bfree, &sb->bavail,
  1612. &sb->files, &sb->ffree, &sb->fsid, &sb->namelen);
  1613. if (err) {
  1614. trace_9p_protocol_dump(clnt, req->rc);
  1615. p9_free_req(clnt, req);
  1616. goto error;
  1617. }
  1618. p9_debug(P9_DEBUG_9P, "<<< RSTATFS fid %d type 0x%lx bsize %ld "
  1619. "blocks %llu bfree %llu bavail %llu files %llu ffree %llu "
  1620. "fsid %llu namelen %ld\n",
  1621. fid->fid, (long unsigned int)sb->type, (long int)sb->bsize,
  1622. sb->blocks, sb->bfree, sb->bavail, sb->files, sb->ffree,
  1623. sb->fsid, (long int)sb->namelen);
  1624. p9_free_req(clnt, req);
  1625. error:
  1626. return err;
  1627. }
  1628. EXPORT_SYMBOL(p9_client_statfs);
  1629. int p9_client_rename(struct p9_fid *fid,
  1630. struct p9_fid *newdirfid, const char *name)
  1631. {
  1632. int err;
  1633. struct p9_req_t *req;
  1634. struct p9_client *clnt;
  1635. err = 0;
  1636. clnt = fid->clnt;
  1637. p9_debug(P9_DEBUG_9P, ">>> TRENAME fid %d newdirfid %d name %s\n",
  1638. fid->fid, newdirfid->fid, name);
  1639. req = p9_client_rpc(clnt, P9_TRENAME, "dds", fid->fid,
  1640. newdirfid->fid, name);
  1641. if (IS_ERR(req)) {
  1642. err = PTR_ERR(req);
  1643. goto error;
  1644. }
  1645. p9_debug(P9_DEBUG_9P, "<<< RRENAME fid %d\n", fid->fid);
  1646. p9_free_req(clnt, req);
  1647. error:
  1648. return err;
  1649. }
  1650. EXPORT_SYMBOL(p9_client_rename);
  1651. int p9_client_renameat(struct p9_fid *olddirfid, const char *old_name,
  1652. struct p9_fid *newdirfid, const char *new_name)
  1653. {
  1654. int err;
  1655. struct p9_req_t *req;
  1656. struct p9_client *clnt;
  1657. err = 0;
  1658. clnt = olddirfid->clnt;
  1659. p9_debug(P9_DEBUG_9P, ">>> TRENAMEAT olddirfid %d old name %s"
  1660. " newdirfid %d new name %s\n", olddirfid->fid, old_name,
  1661. newdirfid->fid, new_name);
  1662. req = p9_client_rpc(clnt, P9_TRENAMEAT, "dsds", olddirfid->fid,
  1663. old_name, newdirfid->fid, new_name);
  1664. if (IS_ERR(req)) {
  1665. err = PTR_ERR(req);
  1666. goto error;
  1667. }
  1668. p9_debug(P9_DEBUG_9P, "<<< RRENAMEAT newdirfid %d new name %s\n",
  1669. newdirfid->fid, new_name);
  1670. p9_free_req(clnt, req);
  1671. error:
  1672. return err;
  1673. }
  1674. EXPORT_SYMBOL(p9_client_renameat);
  1675. /*
  1676. * An xattrwalk without @attr_name gives the fid for the lisxattr namespace
  1677. */
  1678. struct p9_fid *p9_client_xattrwalk(struct p9_fid *file_fid,
  1679. const char *attr_name, u64 *attr_size)
  1680. {
  1681. int err;
  1682. struct p9_req_t *req;
  1683. struct p9_client *clnt;
  1684. struct p9_fid *attr_fid;
  1685. err = 0;
  1686. clnt = file_fid->clnt;
  1687. attr_fid = p9_fid_create(clnt);
  1688. if (IS_ERR(attr_fid)) {
  1689. err = PTR_ERR(attr_fid);
  1690. attr_fid = NULL;
  1691. goto error;
  1692. }
  1693. p9_debug(P9_DEBUG_9P,
  1694. ">>> TXATTRWALK file_fid %d, attr_fid %d name %s\n",
  1695. file_fid->fid, attr_fid->fid, attr_name);
  1696. req = p9_client_rpc(clnt, P9_TXATTRWALK, "dds",
  1697. file_fid->fid, attr_fid->fid, attr_name);
  1698. if (IS_ERR(req)) {
  1699. err = PTR_ERR(req);
  1700. goto error;
  1701. }
  1702. err = p9pdu_readf(req->rc, clnt->proto_version, "q", attr_size);
  1703. if (err) {
  1704. trace_9p_protocol_dump(clnt, req->rc);
  1705. p9_free_req(clnt, req);
  1706. goto clunk_fid;
  1707. }
  1708. p9_free_req(clnt, req);
  1709. p9_debug(P9_DEBUG_9P, "<<< RXATTRWALK fid %d size %llu\n",
  1710. attr_fid->fid, *attr_size);
  1711. return attr_fid;
  1712. clunk_fid:
  1713. p9_client_clunk(attr_fid);
  1714. attr_fid = NULL;
  1715. error:
  1716. if (attr_fid && (attr_fid != file_fid))
  1717. p9_fid_destroy(attr_fid);
  1718. return ERR_PTR(err);
  1719. }
  1720. EXPORT_SYMBOL_GPL(p9_client_xattrwalk);
  1721. int p9_client_xattrcreate(struct p9_fid *fid, const char *name,
  1722. u64 attr_size, int flags)
  1723. {
  1724. int err;
  1725. struct p9_req_t *req;
  1726. struct p9_client *clnt;
  1727. p9_debug(P9_DEBUG_9P,
  1728. ">>> TXATTRCREATE fid %d name %s size %lld flag %d\n",
  1729. fid->fid, name, (long long)attr_size, flags);
  1730. err = 0;
  1731. clnt = fid->clnt;
  1732. req = p9_client_rpc(clnt, P9_TXATTRCREATE, "dsqd",
  1733. fid->fid, name, attr_size, flags);
  1734. if (IS_ERR(req)) {
  1735. err = PTR_ERR(req);
  1736. goto error;
  1737. }
  1738. p9_debug(P9_DEBUG_9P, "<<< RXATTRCREATE fid %d\n", fid->fid);
  1739. p9_free_req(clnt, req);
  1740. error:
  1741. return err;
  1742. }
  1743. EXPORT_SYMBOL_GPL(p9_client_xattrcreate);
  1744. int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset)
  1745. {
  1746. int err, rsize, non_zc = 0;
  1747. struct p9_client *clnt;
  1748. struct p9_req_t *req;
  1749. char *dataptr;
  1750. struct kvec kv = {.iov_base = data, .iov_len = count};
  1751. struct iov_iter to;
  1752. iov_iter_kvec(&to, READ | ITER_KVEC, &kv, 1, count);
  1753. p9_debug(P9_DEBUG_9P, ">>> TREADDIR fid %d offset %llu count %d\n",
  1754. fid->fid, (unsigned long long) offset, count);
  1755. err = 0;
  1756. clnt = fid->clnt;
  1757. rsize = fid->iounit;
  1758. if (!rsize || rsize > clnt->msize-P9_READDIRHDRSZ)
  1759. rsize = clnt->msize - P9_READDIRHDRSZ;
  1760. if (count < rsize)
  1761. rsize = count;
  1762. /* Don't bother zerocopy for small IO (< 1024) */
  1763. if (clnt->trans_mod->zc_request && rsize > 1024) {
  1764. /*
  1765. * response header len is 11
  1766. * PDU Header(7) + IO Size (4)
  1767. */
  1768. req = p9_client_zc_rpc(clnt, P9_TREADDIR, &to, NULL, rsize, 0,
  1769. 11, "dqd", fid->fid, offset, rsize);
  1770. } else {
  1771. non_zc = 1;
  1772. req = p9_client_rpc(clnt, P9_TREADDIR, "dqd", fid->fid,
  1773. offset, rsize);
  1774. }
  1775. if (IS_ERR(req)) {
  1776. err = PTR_ERR(req);
  1777. goto error;
  1778. }
  1779. err = p9pdu_readf(req->rc, clnt->proto_version, "D", &count, &dataptr);
  1780. if (err) {
  1781. trace_9p_protocol_dump(clnt, req->rc);
  1782. goto free_and_error;
  1783. }
  1784. if (rsize < count) {
  1785. pr_err("bogus RREADDIR count (%d > %d)\n", count, rsize);
  1786. count = rsize;
  1787. }
  1788. p9_debug(P9_DEBUG_9P, "<<< RREADDIR count %d\n", count);
  1789. if (non_zc)
  1790. memmove(data, dataptr, count);
  1791. p9_free_req(clnt, req);
  1792. return count;
  1793. free_and_error:
  1794. p9_free_req(clnt, req);
  1795. error:
  1796. return err;
  1797. }
  1798. EXPORT_SYMBOL(p9_client_readdir);
  1799. int p9_client_mknod_dotl(struct p9_fid *fid, char *name, int mode,
  1800. dev_t rdev, kgid_t gid, struct p9_qid *qid)
  1801. {
  1802. int err;
  1803. struct p9_client *clnt;
  1804. struct p9_req_t *req;
  1805. err = 0;
  1806. clnt = fid->clnt;
  1807. p9_debug(P9_DEBUG_9P, ">>> TMKNOD fid %d name %s mode %d major %d "
  1808. "minor %d\n", fid->fid, name, mode, MAJOR(rdev), MINOR(rdev));
  1809. req = p9_client_rpc(clnt, P9_TMKNOD, "dsdddg", fid->fid, name, mode,
  1810. MAJOR(rdev), MINOR(rdev), gid);
  1811. if (IS_ERR(req))
  1812. return PTR_ERR(req);
  1813. err = p9pdu_readf(req->rc, clnt->proto_version, "Q", qid);
  1814. if (err) {
  1815. trace_9p_protocol_dump(clnt, req->rc);
  1816. goto error;
  1817. }
  1818. p9_debug(P9_DEBUG_9P, "<<< RMKNOD qid %x.%llx.%x\n", qid->type,
  1819. (unsigned long long)qid->path, qid->version);
  1820. error:
  1821. p9_free_req(clnt, req);
  1822. return err;
  1823. }
  1824. EXPORT_SYMBOL(p9_client_mknod_dotl);
  1825. int p9_client_mkdir_dotl(struct p9_fid *fid, char *name, int mode,
  1826. kgid_t gid, struct p9_qid *qid)
  1827. {
  1828. int err;
  1829. struct p9_client *clnt;
  1830. struct p9_req_t *req;
  1831. err = 0;
  1832. clnt = fid->clnt;
  1833. p9_debug(P9_DEBUG_9P, ">>> TMKDIR fid %d name %s mode %d gid %d\n",
  1834. fid->fid, name, mode, from_kgid(&init_user_ns, gid));
  1835. req = p9_client_rpc(clnt, P9_TMKDIR, "dsdg", fid->fid, name, mode,
  1836. gid);
  1837. if (IS_ERR(req))
  1838. return PTR_ERR(req);
  1839. err = p9pdu_readf(req->rc, clnt->proto_version, "Q", qid);
  1840. if (err) {
  1841. trace_9p_protocol_dump(clnt, req->rc);
  1842. goto error;
  1843. }
  1844. p9_debug(P9_DEBUG_9P, "<<< RMKDIR qid %x.%llx.%x\n", qid->type,
  1845. (unsigned long long)qid->path, qid->version);
  1846. error:
  1847. p9_free_req(clnt, req);
  1848. return err;
  1849. }
  1850. EXPORT_SYMBOL(p9_client_mkdir_dotl);
  1851. int p9_client_lock_dotl(struct p9_fid *fid, struct p9_flock *flock, u8 *status)
  1852. {
  1853. int err;
  1854. struct p9_client *clnt;
  1855. struct p9_req_t *req;
  1856. err = 0;
  1857. clnt = fid->clnt;
  1858. p9_debug(P9_DEBUG_9P, ">>> TLOCK fid %d type %i flags %d "
  1859. "start %lld length %lld proc_id %d client_id %s\n",
  1860. fid->fid, flock->type, flock->flags, flock->start,
  1861. flock->length, flock->proc_id, flock->client_id);
  1862. req = p9_client_rpc(clnt, P9_TLOCK, "dbdqqds", fid->fid, flock->type,
  1863. flock->flags, flock->start, flock->length,
  1864. flock->proc_id, flock->client_id);
  1865. if (IS_ERR(req))
  1866. return PTR_ERR(req);
  1867. err = p9pdu_readf(req->rc, clnt->proto_version, "b", status);
  1868. if (err) {
  1869. trace_9p_protocol_dump(clnt, req->rc);
  1870. goto error;
  1871. }
  1872. p9_debug(P9_DEBUG_9P, "<<< RLOCK status %i\n", *status);
  1873. error:
  1874. p9_free_req(clnt, req);
  1875. return err;
  1876. }
  1877. EXPORT_SYMBOL(p9_client_lock_dotl);
  1878. int p9_client_getlock_dotl(struct p9_fid *fid, struct p9_getlock *glock)
  1879. {
  1880. int err;
  1881. struct p9_client *clnt;
  1882. struct p9_req_t *req;
  1883. err = 0;
  1884. clnt = fid->clnt;
  1885. p9_debug(P9_DEBUG_9P, ">>> TGETLOCK fid %d, type %i start %lld "
  1886. "length %lld proc_id %d client_id %s\n", fid->fid, glock->type,
  1887. glock->start, glock->length, glock->proc_id, glock->client_id);
  1888. req = p9_client_rpc(clnt, P9_TGETLOCK, "dbqqds", fid->fid, glock->type,
  1889. glock->start, glock->length, glock->proc_id, glock->client_id);
  1890. if (IS_ERR(req))
  1891. return PTR_ERR(req);
  1892. err = p9pdu_readf(req->rc, clnt->proto_version, "bqqds", &glock->type,
  1893. &glock->start, &glock->length, &glock->proc_id,
  1894. &glock->client_id);
  1895. if (err) {
  1896. trace_9p_protocol_dump(clnt, req->rc);
  1897. goto error;
  1898. }
  1899. p9_debug(P9_DEBUG_9P, "<<< RGETLOCK type %i start %lld length %lld "
  1900. "proc_id %d client_id %s\n", glock->type, glock->start,
  1901. glock->length, glock->proc_id, glock->client_id);
  1902. error:
  1903. p9_free_req(clnt, req);
  1904. return err;
  1905. }
  1906. EXPORT_SYMBOL(p9_client_getlock_dotl);
  1907. int p9_client_readlink(struct p9_fid *fid, char **target)
  1908. {
  1909. int err;
  1910. struct p9_client *clnt;
  1911. struct p9_req_t *req;
  1912. err = 0;
  1913. clnt = fid->clnt;
  1914. p9_debug(P9_DEBUG_9P, ">>> TREADLINK fid %d\n", fid->fid);
  1915. req = p9_client_rpc(clnt, P9_TREADLINK, "d", fid->fid);
  1916. if (IS_ERR(req))
  1917. return PTR_ERR(req);
  1918. err = p9pdu_readf(req->rc, clnt->proto_version, "s", target);
  1919. if (err) {
  1920. trace_9p_protocol_dump(clnt, req->rc);
  1921. goto error;
  1922. }
  1923. p9_debug(P9_DEBUG_9P, "<<< RREADLINK target %s\n", *target);
  1924. error:
  1925. p9_free_req(clnt, req);
  1926. return err;
  1927. }
  1928. EXPORT_SYMBOL(p9_client_readlink);