client.c 52 KB

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