client.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272
  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))
  457. err = -ecode;
  458. if (!err || !IS_ERR_VALUE(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))
  530. err = -ecode;
  531. if (!err || !IS_ERR_VALUE(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_interruptible(*req->wq,
  650. req->status >= REQ_STATUS_RCVD);
  651. /*
  652. * Make sure our req is coherent with regard to updates in other
  653. * threads - echoes to wmb() in the callback
  654. */
  655. smp_rmb();
  656. if ((err == -ERESTARTSYS) && (c->status == Connected)
  657. && (type == P9_TFLUSH)) {
  658. sigpending = 1;
  659. clear_thread_flag(TIF_SIGPENDING);
  660. goto again;
  661. }
  662. if (req->status == REQ_STATUS_ERROR) {
  663. p9_debug(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err);
  664. err = req->t_err;
  665. }
  666. if ((err == -ERESTARTSYS) && (c->status == Connected)) {
  667. p9_debug(P9_DEBUG_MUX, "flushing\n");
  668. sigpending = 1;
  669. clear_thread_flag(TIF_SIGPENDING);
  670. if (c->trans_mod->cancel(c, req))
  671. p9_client_flush(c, req);
  672. /* if we received the response anyway, don't signal error */
  673. if (req->status == REQ_STATUS_RCVD)
  674. err = 0;
  675. }
  676. if (sigpending) {
  677. spin_lock_irqsave(&current->sighand->siglock, flags);
  678. recalc_sigpending();
  679. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  680. }
  681. if (err < 0)
  682. goto reterr;
  683. err = p9_check_errors(c, req);
  684. trace_9p_client_res(c, type, req->rc->tag, err);
  685. if (!err)
  686. return req;
  687. reterr:
  688. p9_free_req(c, req);
  689. return ERR_PTR(safe_errno(err));
  690. }
  691. /**
  692. * p9_client_zc_rpc - issue a request and wait for a response
  693. * @c: client session
  694. * @type: type of request
  695. * @uidata: destination for zero copy read
  696. * @uodata: source for zero copy write
  697. * @inlen: read buffer size
  698. * @olen: write buffer size
  699. * @hdrlen: reader header size, This is the size of response protocol data
  700. * @fmt: protocol format string (see protocol.c)
  701. *
  702. * Returns request structure (which client must free using p9_free_req)
  703. */
  704. static struct p9_req_t *p9_client_zc_rpc(struct p9_client *c, int8_t type,
  705. struct iov_iter *uidata,
  706. struct iov_iter *uodata,
  707. int inlen, int olen, int in_hdrlen,
  708. const char *fmt, ...)
  709. {
  710. va_list ap;
  711. int sigpending, err;
  712. unsigned long flags;
  713. struct p9_req_t *req;
  714. va_start(ap, fmt);
  715. /*
  716. * We allocate a inline protocol data of only 4k bytes.
  717. * The actual content is passed in zero-copy fashion.
  718. */
  719. req = p9_client_prepare_req(c, type, P9_ZC_HDR_SZ, fmt, ap);
  720. va_end(ap);
  721. if (IS_ERR(req))
  722. return req;
  723. if (signal_pending(current)) {
  724. sigpending = 1;
  725. clear_thread_flag(TIF_SIGPENDING);
  726. } else
  727. sigpending = 0;
  728. err = c->trans_mod->zc_request(c, req, uidata, uodata,
  729. inlen, olen, in_hdrlen);
  730. if (err < 0) {
  731. if (err == -EIO)
  732. c->status = Disconnected;
  733. if (err != -ERESTARTSYS)
  734. goto reterr;
  735. }
  736. if (req->status == REQ_STATUS_ERROR) {
  737. p9_debug(P9_DEBUG_ERROR, "req_status error %d\n", req->t_err);
  738. err = req->t_err;
  739. }
  740. if ((err == -ERESTARTSYS) && (c->status == Connected)) {
  741. p9_debug(P9_DEBUG_MUX, "flushing\n");
  742. sigpending = 1;
  743. clear_thread_flag(TIF_SIGPENDING);
  744. if (c->trans_mod->cancel(c, req))
  745. p9_client_flush(c, req);
  746. /* if we received the response anyway, don't signal error */
  747. if (req->status == REQ_STATUS_RCVD)
  748. err = 0;
  749. }
  750. if (sigpending) {
  751. spin_lock_irqsave(&current->sighand->siglock, flags);
  752. recalc_sigpending();
  753. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  754. }
  755. if (err < 0)
  756. goto reterr;
  757. err = p9_check_zc_errors(c, req, uidata, in_hdrlen);
  758. trace_9p_client_res(c, type, req->rc->tag, err);
  759. if (!err)
  760. return req;
  761. reterr:
  762. p9_free_req(c, req);
  763. return ERR_PTR(safe_errno(err));
  764. }
  765. static struct p9_fid *p9_fid_create(struct p9_client *clnt)
  766. {
  767. int ret;
  768. struct p9_fid *fid;
  769. unsigned long flags;
  770. p9_debug(P9_DEBUG_FID, "clnt %p\n", clnt);
  771. fid = kmalloc(sizeof(struct p9_fid), GFP_KERNEL);
  772. if (!fid)
  773. return ERR_PTR(-ENOMEM);
  774. ret = p9_idpool_get(clnt->fidpool);
  775. if (ret < 0) {
  776. ret = -ENOSPC;
  777. goto error;
  778. }
  779. fid->fid = ret;
  780. memset(&fid->qid, 0, sizeof(struct p9_qid));
  781. fid->mode = -1;
  782. fid->uid = current_fsuid();
  783. fid->clnt = clnt;
  784. fid->rdir = NULL;
  785. spin_lock_irqsave(&clnt->lock, flags);
  786. list_add(&fid->flist, &clnt->fidlist);
  787. spin_unlock_irqrestore(&clnt->lock, flags);
  788. return fid;
  789. error:
  790. kfree(fid);
  791. return ERR_PTR(ret);
  792. }
  793. static void p9_fid_destroy(struct p9_fid *fid)
  794. {
  795. struct p9_client *clnt;
  796. unsigned long flags;
  797. p9_debug(P9_DEBUG_FID, "fid %d\n", fid->fid);
  798. clnt = fid->clnt;
  799. p9_idpool_put(fid->fid, clnt->fidpool);
  800. spin_lock_irqsave(&clnt->lock, flags);
  801. list_del(&fid->flist);
  802. spin_unlock_irqrestore(&clnt->lock, flags);
  803. kfree(fid->rdir);
  804. kfree(fid);
  805. }
  806. static int p9_client_version(struct p9_client *c)
  807. {
  808. int err = 0;
  809. struct p9_req_t *req;
  810. char *version;
  811. int msize;
  812. p9_debug(P9_DEBUG_9P, ">>> TVERSION msize %d protocol %d\n",
  813. c->msize, c->proto_version);
  814. switch (c->proto_version) {
  815. case p9_proto_2000L:
  816. req = p9_client_rpc(c, P9_TVERSION, "ds",
  817. c->msize, "9P2000.L");
  818. break;
  819. case p9_proto_2000u:
  820. req = p9_client_rpc(c, P9_TVERSION, "ds",
  821. c->msize, "9P2000.u");
  822. break;
  823. case p9_proto_legacy:
  824. req = p9_client_rpc(c, P9_TVERSION, "ds",
  825. c->msize, "9P2000");
  826. break;
  827. default:
  828. return -EINVAL;
  829. }
  830. if (IS_ERR(req))
  831. return PTR_ERR(req);
  832. err = p9pdu_readf(req->rc, c->proto_version, "ds", &msize, &version);
  833. if (err) {
  834. p9_debug(P9_DEBUG_9P, "version error %d\n", err);
  835. trace_9p_protocol_dump(c, req->rc);
  836. goto error;
  837. }
  838. p9_debug(P9_DEBUG_9P, "<<< RVERSION msize %d %s\n", msize, version);
  839. if (!strncmp(version, "9P2000.L", 8))
  840. c->proto_version = p9_proto_2000L;
  841. else if (!strncmp(version, "9P2000.u", 8))
  842. c->proto_version = p9_proto_2000u;
  843. else if (!strncmp(version, "9P2000", 6))
  844. c->proto_version = p9_proto_legacy;
  845. else {
  846. err = -EREMOTEIO;
  847. goto error;
  848. }
  849. if (msize < c->msize)
  850. c->msize = msize;
  851. error:
  852. kfree(version);
  853. p9_free_req(c, req);
  854. return err;
  855. }
  856. struct p9_client *p9_client_create(const char *dev_name, char *options)
  857. {
  858. int err;
  859. struct p9_client *clnt;
  860. char *client_id;
  861. err = 0;
  862. clnt = kmalloc(sizeof(struct p9_client), GFP_KERNEL);
  863. if (!clnt)
  864. return ERR_PTR(-ENOMEM);
  865. clnt->trans_mod = NULL;
  866. clnt->trans = NULL;
  867. client_id = utsname()->nodename;
  868. memcpy(clnt->name, client_id, strlen(client_id) + 1);
  869. spin_lock_init(&clnt->lock);
  870. INIT_LIST_HEAD(&clnt->fidlist);
  871. err = p9_tag_init(clnt);
  872. if (err < 0)
  873. goto free_client;
  874. err = parse_opts(options, clnt);
  875. if (err < 0)
  876. goto destroy_tagpool;
  877. if (!clnt->trans_mod)
  878. clnt->trans_mod = v9fs_get_default_trans();
  879. if (clnt->trans_mod == NULL) {
  880. err = -EPROTONOSUPPORT;
  881. p9_debug(P9_DEBUG_ERROR,
  882. "No transport defined or default transport\n");
  883. goto destroy_tagpool;
  884. }
  885. clnt->fidpool = p9_idpool_create();
  886. if (IS_ERR(clnt->fidpool)) {
  887. err = PTR_ERR(clnt->fidpool);
  888. goto put_trans;
  889. }
  890. p9_debug(P9_DEBUG_MUX, "clnt %p trans %p msize %d protocol %d\n",
  891. clnt, clnt->trans_mod, clnt->msize, clnt->proto_version);
  892. err = clnt->trans_mod->create(clnt, dev_name, options);
  893. if (err)
  894. goto destroy_fidpool;
  895. if (clnt->msize > clnt->trans_mod->maxsize)
  896. clnt->msize = clnt->trans_mod->maxsize;
  897. err = p9_client_version(clnt);
  898. if (err)
  899. goto close_trans;
  900. return clnt;
  901. close_trans:
  902. clnt->trans_mod->close(clnt);
  903. destroy_fidpool:
  904. p9_idpool_destroy(clnt->fidpool);
  905. put_trans:
  906. v9fs_put_trans(clnt->trans_mod);
  907. destroy_tagpool:
  908. p9_idpool_destroy(clnt->tagpool);
  909. free_client:
  910. kfree(clnt);
  911. return ERR_PTR(err);
  912. }
  913. EXPORT_SYMBOL(p9_client_create);
  914. void p9_client_destroy(struct p9_client *clnt)
  915. {
  916. struct p9_fid *fid, *fidptr;
  917. p9_debug(P9_DEBUG_MUX, "clnt %p\n", clnt);
  918. if (clnt->trans_mod)
  919. clnt->trans_mod->close(clnt);
  920. v9fs_put_trans(clnt->trans_mod);
  921. list_for_each_entry_safe(fid, fidptr, &clnt->fidlist, flist) {
  922. pr_info("Found fid %d not clunked\n", fid->fid);
  923. p9_fid_destroy(fid);
  924. }
  925. if (clnt->fidpool)
  926. p9_idpool_destroy(clnt->fidpool);
  927. p9_tag_cleanup(clnt);
  928. kfree(clnt);
  929. }
  930. EXPORT_SYMBOL(p9_client_destroy);
  931. void p9_client_disconnect(struct p9_client *clnt)
  932. {
  933. p9_debug(P9_DEBUG_9P, "clnt %p\n", clnt);
  934. clnt->status = Disconnected;
  935. }
  936. EXPORT_SYMBOL(p9_client_disconnect);
  937. void p9_client_begin_disconnect(struct p9_client *clnt)
  938. {
  939. p9_debug(P9_DEBUG_9P, "clnt %p\n", clnt);
  940. clnt->status = BeginDisconnect;
  941. }
  942. EXPORT_SYMBOL(p9_client_begin_disconnect);
  943. struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
  944. char *uname, kuid_t n_uname, char *aname)
  945. {
  946. int err = 0;
  947. struct p9_req_t *req;
  948. struct p9_fid *fid;
  949. struct p9_qid qid;
  950. p9_debug(P9_DEBUG_9P, ">>> TATTACH afid %d uname %s aname %s\n",
  951. afid ? afid->fid : -1, uname, aname);
  952. fid = p9_fid_create(clnt);
  953. if (IS_ERR(fid)) {
  954. err = PTR_ERR(fid);
  955. fid = NULL;
  956. goto error;
  957. }
  958. fid->uid = n_uname;
  959. req = p9_client_rpc(clnt, P9_TATTACH, "ddss?u", fid->fid,
  960. afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
  961. if (IS_ERR(req)) {
  962. err = PTR_ERR(req);
  963. goto error;
  964. }
  965. err = p9pdu_readf(req->rc, clnt->proto_version, "Q", &qid);
  966. if (err) {
  967. trace_9p_protocol_dump(clnt, req->rc);
  968. p9_free_req(clnt, req);
  969. goto error;
  970. }
  971. p9_debug(P9_DEBUG_9P, "<<< RATTACH qid %x.%llx.%x\n",
  972. qid.type, (unsigned long long)qid.path, qid.version);
  973. memmove(&fid->qid, &qid, sizeof(struct p9_qid));
  974. p9_free_req(clnt, req);
  975. return fid;
  976. error:
  977. if (fid)
  978. p9_fid_destroy(fid);
  979. return ERR_PTR(err);
  980. }
  981. EXPORT_SYMBOL(p9_client_attach);
  982. struct p9_fid *p9_client_walk(struct p9_fid *oldfid, uint16_t nwname,
  983. char **wnames, int clone)
  984. {
  985. int err;
  986. struct p9_client *clnt;
  987. struct p9_fid *fid;
  988. struct p9_qid *wqids;
  989. struct p9_req_t *req;
  990. uint16_t nwqids, count;
  991. err = 0;
  992. wqids = NULL;
  993. clnt = oldfid->clnt;
  994. if (clone) {
  995. fid = p9_fid_create(clnt);
  996. if (IS_ERR(fid)) {
  997. err = PTR_ERR(fid);
  998. fid = NULL;
  999. goto error;
  1000. }
  1001. fid->uid = oldfid->uid;
  1002. } else
  1003. fid = oldfid;
  1004. p9_debug(P9_DEBUG_9P, ">>> TWALK fids %d,%d nwname %ud wname[0] %s\n",
  1005. oldfid->fid, fid->fid, nwname, wnames ? wnames[0] : NULL);
  1006. req = p9_client_rpc(clnt, P9_TWALK, "ddT", oldfid->fid, fid->fid,
  1007. nwname, wnames);
  1008. if (IS_ERR(req)) {
  1009. err = PTR_ERR(req);
  1010. goto error;
  1011. }
  1012. err = p9pdu_readf(req->rc, clnt->proto_version, "R", &nwqids, &wqids);
  1013. if (err) {
  1014. trace_9p_protocol_dump(clnt, req->rc);
  1015. p9_free_req(clnt, req);
  1016. goto clunk_fid;
  1017. }
  1018. p9_free_req(clnt, req);
  1019. p9_debug(P9_DEBUG_9P, "<<< RWALK nwqid %d:\n", nwqids);
  1020. if (nwqids != nwname) {
  1021. err = -ENOENT;
  1022. goto clunk_fid;
  1023. }
  1024. for (count = 0; count < nwqids; count++)
  1025. p9_debug(P9_DEBUG_9P, "<<< [%d] %x.%llx.%x\n",
  1026. count, wqids[count].type,
  1027. (unsigned long long)wqids[count].path,
  1028. wqids[count].version);
  1029. if (nwname)
  1030. memmove(&fid->qid, &wqids[nwqids - 1], sizeof(struct p9_qid));
  1031. else
  1032. fid->qid = oldfid->qid;
  1033. kfree(wqids);
  1034. return fid;
  1035. clunk_fid:
  1036. kfree(wqids);
  1037. p9_client_clunk(fid);
  1038. fid = NULL;
  1039. error:
  1040. if (fid && (fid != oldfid))
  1041. p9_fid_destroy(fid);
  1042. return ERR_PTR(err);
  1043. }
  1044. EXPORT_SYMBOL(p9_client_walk);
  1045. int p9_client_open(struct p9_fid *fid, int mode)
  1046. {
  1047. int err;
  1048. struct p9_client *clnt;
  1049. struct p9_req_t *req;
  1050. struct p9_qid qid;
  1051. int iounit;
  1052. clnt = fid->clnt;
  1053. p9_debug(P9_DEBUG_9P, ">>> %s fid %d mode %d\n",
  1054. p9_is_proto_dotl(clnt) ? "TLOPEN" : "TOPEN", fid->fid, mode);
  1055. err = 0;
  1056. if (fid->mode != -1)
  1057. return -EINVAL;
  1058. if (p9_is_proto_dotl(clnt))
  1059. req = p9_client_rpc(clnt, P9_TLOPEN, "dd", fid->fid, mode);
  1060. else
  1061. req = p9_client_rpc(clnt, P9_TOPEN, "db", fid->fid, mode);
  1062. if (IS_ERR(req)) {
  1063. err = PTR_ERR(req);
  1064. goto error;
  1065. }
  1066. err = p9pdu_readf(req->rc, clnt->proto_version, "Qd", &qid, &iounit);
  1067. if (err) {
  1068. trace_9p_protocol_dump(clnt, req->rc);
  1069. goto free_and_error;
  1070. }
  1071. p9_debug(P9_DEBUG_9P, "<<< %s qid %x.%llx.%x iounit %x\n",
  1072. p9_is_proto_dotl(clnt) ? "RLOPEN" : "ROPEN", qid.type,
  1073. (unsigned long long)qid.path, qid.version, iounit);
  1074. fid->mode = mode;
  1075. fid->iounit = iounit;
  1076. free_and_error:
  1077. p9_free_req(clnt, req);
  1078. error:
  1079. return err;
  1080. }
  1081. EXPORT_SYMBOL(p9_client_open);
  1082. int p9_client_create_dotl(struct p9_fid *ofid, char *name, u32 flags, u32 mode,
  1083. kgid_t gid, struct p9_qid *qid)
  1084. {
  1085. int err = 0;
  1086. struct p9_client *clnt;
  1087. struct p9_req_t *req;
  1088. int iounit;
  1089. p9_debug(P9_DEBUG_9P,
  1090. ">>> TLCREATE fid %d name %s flags %d mode %d gid %d\n",
  1091. ofid->fid, name, flags, mode,
  1092. from_kgid(&init_user_ns, gid));
  1093. clnt = ofid->clnt;
  1094. if (ofid->mode != -1)
  1095. return -EINVAL;
  1096. req = p9_client_rpc(clnt, P9_TLCREATE, "dsddg", ofid->fid, name, flags,
  1097. mode, gid);
  1098. if (IS_ERR(req)) {
  1099. err = PTR_ERR(req);
  1100. goto error;
  1101. }
  1102. err = p9pdu_readf(req->rc, clnt->proto_version, "Qd", qid, &iounit);
  1103. if (err) {
  1104. trace_9p_protocol_dump(clnt, req->rc);
  1105. goto free_and_error;
  1106. }
  1107. p9_debug(P9_DEBUG_9P, "<<< RLCREATE qid %x.%llx.%x iounit %x\n",
  1108. qid->type,
  1109. (unsigned long long)qid->path,
  1110. qid->version, iounit);
  1111. ofid->mode = mode;
  1112. ofid->iounit = iounit;
  1113. free_and_error:
  1114. p9_free_req(clnt, req);
  1115. error:
  1116. return err;
  1117. }
  1118. EXPORT_SYMBOL(p9_client_create_dotl);
  1119. int p9_client_fcreate(struct p9_fid *fid, char *name, u32 perm, int mode,
  1120. char *extension)
  1121. {
  1122. int err;
  1123. struct p9_client *clnt;
  1124. struct p9_req_t *req;
  1125. struct p9_qid qid;
  1126. int iounit;
  1127. p9_debug(P9_DEBUG_9P, ">>> TCREATE fid %d name %s perm %d mode %d\n",
  1128. fid->fid, name, perm, mode);
  1129. err = 0;
  1130. clnt = fid->clnt;
  1131. if (fid->mode != -1)
  1132. return -EINVAL;
  1133. req = p9_client_rpc(clnt, P9_TCREATE, "dsdb?s", fid->fid, name, perm,
  1134. mode, extension);
  1135. if (IS_ERR(req)) {
  1136. err = PTR_ERR(req);
  1137. goto error;
  1138. }
  1139. err = p9pdu_readf(req->rc, clnt->proto_version, "Qd", &qid, &iounit);
  1140. if (err) {
  1141. trace_9p_protocol_dump(clnt, req->rc);
  1142. goto free_and_error;
  1143. }
  1144. p9_debug(P9_DEBUG_9P, "<<< RCREATE qid %x.%llx.%x iounit %x\n",
  1145. qid.type,
  1146. (unsigned long long)qid.path,
  1147. qid.version, iounit);
  1148. fid->mode = mode;
  1149. fid->iounit = iounit;
  1150. free_and_error:
  1151. p9_free_req(clnt, req);
  1152. error:
  1153. return err;
  1154. }
  1155. EXPORT_SYMBOL(p9_client_fcreate);
  1156. int p9_client_symlink(struct p9_fid *dfid, char *name, char *symtgt, kgid_t gid,
  1157. struct p9_qid *qid)
  1158. {
  1159. int err = 0;
  1160. struct p9_client *clnt;
  1161. struct p9_req_t *req;
  1162. p9_debug(P9_DEBUG_9P, ">>> TSYMLINK dfid %d name %s symtgt %s\n",
  1163. dfid->fid, name, symtgt);
  1164. clnt = dfid->clnt;
  1165. req = p9_client_rpc(clnt, P9_TSYMLINK, "dssg", dfid->fid, name, symtgt,
  1166. gid);
  1167. if (IS_ERR(req)) {
  1168. err = PTR_ERR(req);
  1169. goto error;
  1170. }
  1171. err = p9pdu_readf(req->rc, clnt->proto_version, "Q", qid);
  1172. if (err) {
  1173. trace_9p_protocol_dump(clnt, req->rc);
  1174. goto free_and_error;
  1175. }
  1176. p9_debug(P9_DEBUG_9P, "<<< RSYMLINK qid %x.%llx.%x\n",
  1177. qid->type, (unsigned long long)qid->path, qid->version);
  1178. free_and_error:
  1179. p9_free_req(clnt, req);
  1180. error:
  1181. return err;
  1182. }
  1183. EXPORT_SYMBOL(p9_client_symlink);
  1184. int p9_client_link(struct p9_fid *dfid, struct p9_fid *oldfid, char *newname)
  1185. {
  1186. struct p9_client *clnt;
  1187. struct p9_req_t *req;
  1188. p9_debug(P9_DEBUG_9P, ">>> TLINK dfid %d oldfid %d newname %s\n",
  1189. dfid->fid, oldfid->fid, newname);
  1190. clnt = dfid->clnt;
  1191. req = p9_client_rpc(clnt, P9_TLINK, "dds", dfid->fid, oldfid->fid,
  1192. newname);
  1193. if (IS_ERR(req))
  1194. return PTR_ERR(req);
  1195. p9_debug(P9_DEBUG_9P, "<<< RLINK\n");
  1196. p9_free_req(clnt, req);
  1197. return 0;
  1198. }
  1199. EXPORT_SYMBOL(p9_client_link);
  1200. int p9_client_fsync(struct p9_fid *fid, int datasync)
  1201. {
  1202. int err;
  1203. struct p9_client *clnt;
  1204. struct p9_req_t *req;
  1205. p9_debug(P9_DEBUG_9P, ">>> TFSYNC fid %d datasync:%d\n",
  1206. fid->fid, datasync);
  1207. err = 0;
  1208. clnt = fid->clnt;
  1209. req = p9_client_rpc(clnt, P9_TFSYNC, "dd", fid->fid, datasync);
  1210. if (IS_ERR(req)) {
  1211. err = PTR_ERR(req);
  1212. goto error;
  1213. }
  1214. p9_debug(P9_DEBUG_9P, "<<< RFSYNC fid %d\n", fid->fid);
  1215. p9_free_req(clnt, req);
  1216. error:
  1217. return err;
  1218. }
  1219. EXPORT_SYMBOL(p9_client_fsync);
  1220. int p9_client_clunk(struct p9_fid *fid)
  1221. {
  1222. int err;
  1223. struct p9_client *clnt;
  1224. struct p9_req_t *req;
  1225. int retries = 0;
  1226. if (!fid) {
  1227. pr_warn("%s (%d): Trying to clunk with NULL fid\n",
  1228. __func__, task_pid_nr(current));
  1229. dump_stack();
  1230. return 0;
  1231. }
  1232. again:
  1233. p9_debug(P9_DEBUG_9P, ">>> TCLUNK fid %d (try %d)\n", fid->fid,
  1234. retries);
  1235. err = 0;
  1236. clnt = fid->clnt;
  1237. req = p9_client_rpc(clnt, P9_TCLUNK, "d", fid->fid);
  1238. if (IS_ERR(req)) {
  1239. err = PTR_ERR(req);
  1240. goto error;
  1241. }
  1242. p9_debug(P9_DEBUG_9P, "<<< RCLUNK fid %d\n", fid->fid);
  1243. p9_free_req(clnt, req);
  1244. error:
  1245. /*
  1246. * Fid is not valid even after a failed clunk
  1247. * If interrupted, retry once then give up and
  1248. * leak fid until umount.
  1249. */
  1250. if (err == -ERESTARTSYS) {
  1251. if (retries++ == 0)
  1252. goto again;
  1253. } else
  1254. p9_fid_destroy(fid);
  1255. return err;
  1256. }
  1257. EXPORT_SYMBOL(p9_client_clunk);
  1258. int p9_client_remove(struct p9_fid *fid)
  1259. {
  1260. int err;
  1261. struct p9_client *clnt;
  1262. struct p9_req_t *req;
  1263. p9_debug(P9_DEBUG_9P, ">>> TREMOVE fid %d\n", fid->fid);
  1264. err = 0;
  1265. clnt = fid->clnt;
  1266. req = p9_client_rpc(clnt, P9_TREMOVE, "d", fid->fid);
  1267. if (IS_ERR(req)) {
  1268. err = PTR_ERR(req);
  1269. goto error;
  1270. }
  1271. p9_debug(P9_DEBUG_9P, "<<< RREMOVE fid %d\n", fid->fid);
  1272. p9_free_req(clnt, req);
  1273. error:
  1274. if (err == -ERESTARTSYS)
  1275. p9_client_clunk(fid);
  1276. else
  1277. p9_fid_destroy(fid);
  1278. return err;
  1279. }
  1280. EXPORT_SYMBOL(p9_client_remove);
  1281. int p9_client_unlinkat(struct p9_fid *dfid, const char *name, int flags)
  1282. {
  1283. int err = 0;
  1284. struct p9_req_t *req;
  1285. struct p9_client *clnt;
  1286. p9_debug(P9_DEBUG_9P, ">>> TUNLINKAT fid %d %s %d\n",
  1287. dfid->fid, name, flags);
  1288. clnt = dfid->clnt;
  1289. req = p9_client_rpc(clnt, P9_TUNLINKAT, "dsd", dfid->fid, name, flags);
  1290. if (IS_ERR(req)) {
  1291. err = PTR_ERR(req);
  1292. goto error;
  1293. }
  1294. p9_debug(P9_DEBUG_9P, "<<< RUNLINKAT fid %d %s\n", dfid->fid, name);
  1295. p9_free_req(clnt, req);
  1296. error:
  1297. return err;
  1298. }
  1299. EXPORT_SYMBOL(p9_client_unlinkat);
  1300. int
  1301. p9_client_read(struct p9_fid *fid, u64 offset, struct iov_iter *to, int *err)
  1302. {
  1303. struct p9_client *clnt = fid->clnt;
  1304. struct p9_req_t *req;
  1305. int total = 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. p9_debug(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %zd\n",
  1377. fid->fid, (unsigned long long) offset,
  1378. iov_iter_count(from));
  1379. while (iov_iter_count(from)) {
  1380. int count = iov_iter_count(from);
  1381. int rsize = fid->iounit;
  1382. if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
  1383. rsize = clnt->msize - P9_IOHDRSZ;
  1384. if (count < rsize)
  1385. rsize = count;
  1386. /* Don't bother zerocopy for small IO (< 1024) */
  1387. if (clnt->trans_mod->zc_request && rsize > 1024) {
  1388. req = p9_client_zc_rpc(clnt, P9_TWRITE, NULL, from, 0,
  1389. rsize, P9_ZC_HDR_SZ, "dqd",
  1390. fid->fid, offset, rsize);
  1391. } else {
  1392. req = p9_client_rpc(clnt, P9_TWRITE, "dqV", fid->fid,
  1393. offset, rsize, from);
  1394. }
  1395. if (IS_ERR(req)) {
  1396. *err = PTR_ERR(req);
  1397. break;
  1398. }
  1399. *err = p9pdu_readf(req->rc, clnt->proto_version, "d", &count);
  1400. if (*err) {
  1401. trace_9p_protocol_dump(clnt, req->rc);
  1402. p9_free_req(clnt, req);
  1403. break;
  1404. }
  1405. if (rsize < count) {
  1406. pr_err("bogus RWRITE count (%d > %d)\n", count, rsize);
  1407. count = rsize;
  1408. }
  1409. p9_debug(P9_DEBUG_9P, "<<< RWRITE count %d\n", count);
  1410. p9_free_req(clnt, req);
  1411. iov_iter_advance(from, count);
  1412. total += count;
  1413. offset += count;
  1414. }
  1415. return total;
  1416. }
  1417. EXPORT_SYMBOL(p9_client_write);
  1418. struct p9_wstat *p9_client_stat(struct p9_fid *fid)
  1419. {
  1420. int err;
  1421. struct p9_client *clnt;
  1422. struct p9_wstat *ret = kmalloc(sizeof(struct p9_wstat), GFP_KERNEL);
  1423. struct p9_req_t *req;
  1424. u16 ignored;
  1425. p9_debug(P9_DEBUG_9P, ">>> TSTAT fid %d\n", fid->fid);
  1426. if (!ret)
  1427. return ERR_PTR(-ENOMEM);
  1428. err = 0;
  1429. clnt = fid->clnt;
  1430. req = p9_client_rpc(clnt, P9_TSTAT, "d", fid->fid);
  1431. if (IS_ERR(req)) {
  1432. err = PTR_ERR(req);
  1433. goto error;
  1434. }
  1435. err = p9pdu_readf(req->rc, clnt->proto_version, "wS", &ignored, ret);
  1436. if (err) {
  1437. trace_9p_protocol_dump(clnt, req->rc);
  1438. p9_free_req(clnt, req);
  1439. goto error;
  1440. }
  1441. p9_debug(P9_DEBUG_9P,
  1442. "<<< RSTAT sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
  1443. "<<< mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
  1444. "<<< name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
  1445. "<<< uid=%d gid=%d n_muid=%d\n",
  1446. ret->size, ret->type, ret->dev, ret->qid.type,
  1447. (unsigned long long)ret->qid.path, ret->qid.version, ret->mode,
  1448. ret->atime, ret->mtime, (unsigned long long)ret->length,
  1449. ret->name, ret->uid, ret->gid, ret->muid, ret->extension,
  1450. from_kuid(&init_user_ns, ret->n_uid),
  1451. from_kgid(&init_user_ns, ret->n_gid),
  1452. from_kuid(&init_user_ns, ret->n_muid));
  1453. p9_free_req(clnt, req);
  1454. return ret;
  1455. error:
  1456. kfree(ret);
  1457. return ERR_PTR(err);
  1458. }
  1459. EXPORT_SYMBOL(p9_client_stat);
  1460. struct p9_stat_dotl *p9_client_getattr_dotl(struct p9_fid *fid,
  1461. u64 request_mask)
  1462. {
  1463. int err;
  1464. struct p9_client *clnt;
  1465. struct p9_stat_dotl *ret = kmalloc(sizeof(struct p9_stat_dotl),
  1466. GFP_KERNEL);
  1467. struct p9_req_t *req;
  1468. p9_debug(P9_DEBUG_9P, ">>> TGETATTR fid %d, request_mask %lld\n",
  1469. fid->fid, request_mask);
  1470. if (!ret)
  1471. return ERR_PTR(-ENOMEM);
  1472. err = 0;
  1473. clnt = fid->clnt;
  1474. req = p9_client_rpc(clnt, P9_TGETATTR, "dq", fid->fid, request_mask);
  1475. if (IS_ERR(req)) {
  1476. err = PTR_ERR(req);
  1477. goto error;
  1478. }
  1479. err = p9pdu_readf(req->rc, clnt->proto_version, "A", ret);
  1480. if (err) {
  1481. trace_9p_protocol_dump(clnt, req->rc);
  1482. p9_free_req(clnt, req);
  1483. goto error;
  1484. }
  1485. p9_debug(P9_DEBUG_9P,
  1486. "<<< RGETATTR st_result_mask=%lld\n"
  1487. "<<< qid=%x.%llx.%x\n"
  1488. "<<< st_mode=%8.8x st_nlink=%llu\n"
  1489. "<<< st_uid=%d st_gid=%d\n"
  1490. "<<< st_rdev=%llx st_size=%llx st_blksize=%llu st_blocks=%llu\n"
  1491. "<<< st_atime_sec=%lld st_atime_nsec=%lld\n"
  1492. "<<< st_mtime_sec=%lld st_mtime_nsec=%lld\n"
  1493. "<<< st_ctime_sec=%lld st_ctime_nsec=%lld\n"
  1494. "<<< st_btime_sec=%lld st_btime_nsec=%lld\n"
  1495. "<<< st_gen=%lld st_data_version=%lld",
  1496. ret->st_result_mask, ret->qid.type, ret->qid.path,
  1497. ret->qid.version, ret->st_mode, ret->st_nlink,
  1498. from_kuid(&init_user_ns, ret->st_uid),
  1499. from_kgid(&init_user_ns, ret->st_gid),
  1500. ret->st_rdev, ret->st_size, ret->st_blksize,
  1501. ret->st_blocks, ret->st_atime_sec, ret->st_atime_nsec,
  1502. ret->st_mtime_sec, ret->st_mtime_nsec, ret->st_ctime_sec,
  1503. ret->st_ctime_nsec, ret->st_btime_sec, ret->st_btime_nsec,
  1504. ret->st_gen, ret->st_data_version);
  1505. p9_free_req(clnt, req);
  1506. return ret;
  1507. error:
  1508. kfree(ret);
  1509. return ERR_PTR(err);
  1510. }
  1511. EXPORT_SYMBOL(p9_client_getattr_dotl);
  1512. static int p9_client_statsize(struct p9_wstat *wst, int proto_version)
  1513. {
  1514. int ret;
  1515. /* NOTE: size shouldn't include its own length */
  1516. /* size[2] type[2] dev[4] qid[13] */
  1517. /* mode[4] atime[4] mtime[4] length[8]*/
  1518. /* name[s] uid[s] gid[s] muid[s] */
  1519. ret = 2+4+13+4+4+4+8+2+2+2+2;
  1520. if (wst->name)
  1521. ret += strlen(wst->name);
  1522. if (wst->uid)
  1523. ret += strlen(wst->uid);
  1524. if (wst->gid)
  1525. ret += strlen(wst->gid);
  1526. if (wst->muid)
  1527. ret += strlen(wst->muid);
  1528. if ((proto_version == p9_proto_2000u) ||
  1529. (proto_version == p9_proto_2000L)) {
  1530. ret += 2+4+4+4; /* extension[s] n_uid[4] n_gid[4] n_muid[4] */
  1531. if (wst->extension)
  1532. ret += strlen(wst->extension);
  1533. }
  1534. return ret;
  1535. }
  1536. int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst)
  1537. {
  1538. int err;
  1539. struct p9_req_t *req;
  1540. struct p9_client *clnt;
  1541. err = 0;
  1542. clnt = fid->clnt;
  1543. wst->size = p9_client_statsize(wst, clnt->proto_version);
  1544. p9_debug(P9_DEBUG_9P, ">>> TWSTAT fid %d\n", fid->fid);
  1545. p9_debug(P9_DEBUG_9P,
  1546. " sz=%x type=%x dev=%x qid=%x.%llx.%x\n"
  1547. " mode=%8.8x atime=%8.8x mtime=%8.8x length=%llx\n"
  1548. " name=%s uid=%s gid=%s muid=%s extension=(%s)\n"
  1549. " uid=%d gid=%d n_muid=%d\n",
  1550. wst->size, wst->type, wst->dev, wst->qid.type,
  1551. (unsigned long long)wst->qid.path, wst->qid.version, wst->mode,
  1552. wst->atime, wst->mtime, (unsigned long long)wst->length,
  1553. wst->name, wst->uid, wst->gid, wst->muid, wst->extension,
  1554. from_kuid(&init_user_ns, wst->n_uid),
  1555. from_kgid(&init_user_ns, wst->n_gid),
  1556. from_kuid(&init_user_ns, wst->n_muid));
  1557. req = p9_client_rpc(clnt, P9_TWSTAT, "dwS", fid->fid, wst->size+2, wst);
  1558. if (IS_ERR(req)) {
  1559. err = PTR_ERR(req);
  1560. goto error;
  1561. }
  1562. p9_debug(P9_DEBUG_9P, "<<< RWSTAT fid %d\n", fid->fid);
  1563. p9_free_req(clnt, req);
  1564. error:
  1565. return err;
  1566. }
  1567. EXPORT_SYMBOL(p9_client_wstat);
  1568. int p9_client_setattr(struct p9_fid *fid, struct p9_iattr_dotl *p9attr)
  1569. {
  1570. int err;
  1571. struct p9_req_t *req;
  1572. struct p9_client *clnt;
  1573. err = 0;
  1574. clnt = fid->clnt;
  1575. p9_debug(P9_DEBUG_9P, ">>> TSETATTR fid %d\n", fid->fid);
  1576. p9_debug(P9_DEBUG_9P,
  1577. " valid=%x mode=%x uid=%d gid=%d size=%lld\n"
  1578. " atime_sec=%lld atime_nsec=%lld\n"
  1579. " mtime_sec=%lld mtime_nsec=%lld\n",
  1580. p9attr->valid, p9attr->mode,
  1581. from_kuid(&init_user_ns, p9attr->uid),
  1582. from_kgid(&init_user_ns, p9attr->gid),
  1583. p9attr->size, p9attr->atime_sec, p9attr->atime_nsec,
  1584. p9attr->mtime_sec, p9attr->mtime_nsec);
  1585. req = p9_client_rpc(clnt, P9_TSETATTR, "dI", fid->fid, p9attr);
  1586. if (IS_ERR(req)) {
  1587. err = PTR_ERR(req);
  1588. goto error;
  1589. }
  1590. p9_debug(P9_DEBUG_9P, "<<< RSETATTR fid %d\n", fid->fid);
  1591. p9_free_req(clnt, req);
  1592. error:
  1593. return err;
  1594. }
  1595. EXPORT_SYMBOL(p9_client_setattr);
  1596. int p9_client_statfs(struct p9_fid *fid, struct p9_rstatfs *sb)
  1597. {
  1598. int err;
  1599. struct p9_req_t *req;
  1600. struct p9_client *clnt;
  1601. err = 0;
  1602. clnt = fid->clnt;
  1603. p9_debug(P9_DEBUG_9P, ">>> TSTATFS fid %d\n", fid->fid);
  1604. req = p9_client_rpc(clnt, P9_TSTATFS, "d", fid->fid);
  1605. if (IS_ERR(req)) {
  1606. err = PTR_ERR(req);
  1607. goto error;
  1608. }
  1609. err = p9pdu_readf(req->rc, clnt->proto_version, "ddqqqqqqd", &sb->type,
  1610. &sb->bsize, &sb->blocks, &sb->bfree, &sb->bavail,
  1611. &sb->files, &sb->ffree, &sb->fsid, &sb->namelen);
  1612. if (err) {
  1613. trace_9p_protocol_dump(clnt, req->rc);
  1614. p9_free_req(clnt, req);
  1615. goto error;
  1616. }
  1617. p9_debug(P9_DEBUG_9P, "<<< RSTATFS fid %d type 0x%lx bsize %ld "
  1618. "blocks %llu bfree %llu bavail %llu files %llu ffree %llu "
  1619. "fsid %llu namelen %ld\n",
  1620. fid->fid, (long unsigned int)sb->type, (long int)sb->bsize,
  1621. sb->blocks, sb->bfree, sb->bavail, sb->files, sb->ffree,
  1622. sb->fsid, (long int)sb->namelen);
  1623. p9_free_req(clnt, req);
  1624. error:
  1625. return err;
  1626. }
  1627. EXPORT_SYMBOL(p9_client_statfs);
  1628. int p9_client_rename(struct p9_fid *fid,
  1629. struct p9_fid *newdirfid, const char *name)
  1630. {
  1631. int err;
  1632. struct p9_req_t *req;
  1633. struct p9_client *clnt;
  1634. err = 0;
  1635. clnt = fid->clnt;
  1636. p9_debug(P9_DEBUG_9P, ">>> TRENAME fid %d newdirfid %d name %s\n",
  1637. fid->fid, newdirfid->fid, name);
  1638. req = p9_client_rpc(clnt, P9_TRENAME, "dds", fid->fid,
  1639. newdirfid->fid, name);
  1640. if (IS_ERR(req)) {
  1641. err = PTR_ERR(req);
  1642. goto error;
  1643. }
  1644. p9_debug(P9_DEBUG_9P, "<<< RRENAME fid %d\n", fid->fid);
  1645. p9_free_req(clnt, req);
  1646. error:
  1647. return err;
  1648. }
  1649. EXPORT_SYMBOL(p9_client_rename);
  1650. int p9_client_renameat(struct p9_fid *olddirfid, const char *old_name,
  1651. struct p9_fid *newdirfid, const char *new_name)
  1652. {
  1653. int err;
  1654. struct p9_req_t *req;
  1655. struct p9_client *clnt;
  1656. err = 0;
  1657. clnt = olddirfid->clnt;
  1658. p9_debug(P9_DEBUG_9P, ">>> TRENAMEAT olddirfid %d old name %s"
  1659. " newdirfid %d new name %s\n", olddirfid->fid, old_name,
  1660. newdirfid->fid, new_name);
  1661. req = p9_client_rpc(clnt, P9_TRENAMEAT, "dsds", olddirfid->fid,
  1662. old_name, newdirfid->fid, new_name);
  1663. if (IS_ERR(req)) {
  1664. err = PTR_ERR(req);
  1665. goto error;
  1666. }
  1667. p9_debug(P9_DEBUG_9P, "<<< RRENAMEAT newdirfid %d new name %s\n",
  1668. newdirfid->fid, new_name);
  1669. p9_free_req(clnt, req);
  1670. error:
  1671. return err;
  1672. }
  1673. EXPORT_SYMBOL(p9_client_renameat);
  1674. /*
  1675. * An xattrwalk without @attr_name gives the fid for the lisxattr namespace
  1676. */
  1677. struct p9_fid *p9_client_xattrwalk(struct p9_fid *file_fid,
  1678. const char *attr_name, u64 *attr_size)
  1679. {
  1680. int err;
  1681. struct p9_req_t *req;
  1682. struct p9_client *clnt;
  1683. struct p9_fid *attr_fid;
  1684. err = 0;
  1685. clnt = file_fid->clnt;
  1686. attr_fid = p9_fid_create(clnt);
  1687. if (IS_ERR(attr_fid)) {
  1688. err = PTR_ERR(attr_fid);
  1689. attr_fid = NULL;
  1690. goto error;
  1691. }
  1692. p9_debug(P9_DEBUG_9P,
  1693. ">>> TXATTRWALK file_fid %d, attr_fid %d name %s\n",
  1694. file_fid->fid, attr_fid->fid, attr_name);
  1695. req = p9_client_rpc(clnt, P9_TXATTRWALK, "dds",
  1696. file_fid->fid, attr_fid->fid, attr_name);
  1697. if (IS_ERR(req)) {
  1698. err = PTR_ERR(req);
  1699. goto error;
  1700. }
  1701. err = p9pdu_readf(req->rc, clnt->proto_version, "q", attr_size);
  1702. if (err) {
  1703. trace_9p_protocol_dump(clnt, req->rc);
  1704. p9_free_req(clnt, req);
  1705. goto clunk_fid;
  1706. }
  1707. p9_free_req(clnt, req);
  1708. p9_debug(P9_DEBUG_9P, "<<< RXATTRWALK fid %d size %llu\n",
  1709. attr_fid->fid, *attr_size);
  1710. return attr_fid;
  1711. clunk_fid:
  1712. p9_client_clunk(attr_fid);
  1713. attr_fid = NULL;
  1714. error:
  1715. if (attr_fid && (attr_fid != file_fid))
  1716. p9_fid_destroy(attr_fid);
  1717. return ERR_PTR(err);
  1718. }
  1719. EXPORT_SYMBOL_GPL(p9_client_xattrwalk);
  1720. int p9_client_xattrcreate(struct p9_fid *fid, const char *name,
  1721. u64 attr_size, int flags)
  1722. {
  1723. int err;
  1724. struct p9_req_t *req;
  1725. struct p9_client *clnt;
  1726. p9_debug(P9_DEBUG_9P,
  1727. ">>> TXATTRCREATE fid %d name %s size %lld flag %d\n",
  1728. fid->fid, name, (long long)attr_size, flags);
  1729. err = 0;
  1730. clnt = fid->clnt;
  1731. req = p9_client_rpc(clnt, P9_TXATTRCREATE, "dsqd",
  1732. fid->fid, name, attr_size, flags);
  1733. if (IS_ERR(req)) {
  1734. err = PTR_ERR(req);
  1735. goto error;
  1736. }
  1737. p9_debug(P9_DEBUG_9P, "<<< RXATTRCREATE fid %d\n", fid->fid);
  1738. p9_free_req(clnt, req);
  1739. error:
  1740. return err;
  1741. }
  1742. EXPORT_SYMBOL_GPL(p9_client_xattrcreate);
  1743. int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset)
  1744. {
  1745. int err, rsize, non_zc = 0;
  1746. struct p9_client *clnt;
  1747. struct p9_req_t *req;
  1748. char *dataptr;
  1749. struct kvec kv = {.iov_base = data, .iov_len = count};
  1750. struct iov_iter to;
  1751. iov_iter_kvec(&to, READ | ITER_KVEC, &kv, 1, count);
  1752. p9_debug(P9_DEBUG_9P, ">>> TREADDIR fid %d offset %llu count %d\n",
  1753. fid->fid, (unsigned long long) offset, count);
  1754. err = 0;
  1755. clnt = fid->clnt;
  1756. rsize = fid->iounit;
  1757. if (!rsize || rsize > clnt->msize-P9_READDIRHDRSZ)
  1758. rsize = clnt->msize - P9_READDIRHDRSZ;
  1759. if (count < rsize)
  1760. rsize = count;
  1761. /* Don't bother zerocopy for small IO (< 1024) */
  1762. if (clnt->trans_mod->zc_request && rsize > 1024) {
  1763. /*
  1764. * response header len is 11
  1765. * PDU Header(7) + IO Size (4)
  1766. */
  1767. req = p9_client_zc_rpc(clnt, P9_TREADDIR, &to, NULL, rsize, 0,
  1768. 11, "dqd", fid->fid, offset, rsize);
  1769. } else {
  1770. non_zc = 1;
  1771. req = p9_client_rpc(clnt, P9_TREADDIR, "dqd", fid->fid,
  1772. offset, rsize);
  1773. }
  1774. if (IS_ERR(req)) {
  1775. err = PTR_ERR(req);
  1776. goto error;
  1777. }
  1778. err = p9pdu_readf(req->rc, clnt->proto_version, "D", &count, &dataptr);
  1779. if (err) {
  1780. trace_9p_protocol_dump(clnt, req->rc);
  1781. goto free_and_error;
  1782. }
  1783. p9_debug(P9_DEBUG_9P, "<<< RREADDIR count %d\n", count);
  1784. if (non_zc)
  1785. memmove(data, dataptr, count);
  1786. p9_free_req(clnt, req);
  1787. return count;
  1788. free_and_error:
  1789. p9_free_req(clnt, req);
  1790. error:
  1791. return err;
  1792. }
  1793. EXPORT_SYMBOL(p9_client_readdir);
  1794. int p9_client_mknod_dotl(struct p9_fid *fid, char *name, int mode,
  1795. dev_t rdev, kgid_t gid, struct p9_qid *qid)
  1796. {
  1797. int err;
  1798. struct p9_client *clnt;
  1799. struct p9_req_t *req;
  1800. err = 0;
  1801. clnt = fid->clnt;
  1802. p9_debug(P9_DEBUG_9P, ">>> TMKNOD fid %d name %s mode %d major %d "
  1803. "minor %d\n", fid->fid, name, mode, MAJOR(rdev), MINOR(rdev));
  1804. req = p9_client_rpc(clnt, P9_TMKNOD, "dsdddg", fid->fid, name, mode,
  1805. MAJOR(rdev), MINOR(rdev), gid);
  1806. if (IS_ERR(req))
  1807. return PTR_ERR(req);
  1808. err = p9pdu_readf(req->rc, clnt->proto_version, "Q", qid);
  1809. if (err) {
  1810. trace_9p_protocol_dump(clnt, req->rc);
  1811. goto error;
  1812. }
  1813. p9_debug(P9_DEBUG_9P, "<<< RMKNOD qid %x.%llx.%x\n", qid->type,
  1814. (unsigned long long)qid->path, qid->version);
  1815. error:
  1816. p9_free_req(clnt, req);
  1817. return err;
  1818. }
  1819. EXPORT_SYMBOL(p9_client_mknod_dotl);
  1820. int p9_client_mkdir_dotl(struct p9_fid *fid, char *name, int mode,
  1821. kgid_t gid, struct p9_qid *qid)
  1822. {
  1823. int err;
  1824. struct p9_client *clnt;
  1825. struct p9_req_t *req;
  1826. err = 0;
  1827. clnt = fid->clnt;
  1828. p9_debug(P9_DEBUG_9P, ">>> TMKDIR fid %d name %s mode %d gid %d\n",
  1829. fid->fid, name, mode, from_kgid(&init_user_ns, gid));
  1830. req = p9_client_rpc(clnt, P9_TMKDIR, "dsdg", fid->fid, name, mode,
  1831. gid);
  1832. if (IS_ERR(req))
  1833. return PTR_ERR(req);
  1834. err = p9pdu_readf(req->rc, clnt->proto_version, "Q", qid);
  1835. if (err) {
  1836. trace_9p_protocol_dump(clnt, req->rc);
  1837. goto error;
  1838. }
  1839. p9_debug(P9_DEBUG_9P, "<<< RMKDIR qid %x.%llx.%x\n", qid->type,
  1840. (unsigned long long)qid->path, qid->version);
  1841. error:
  1842. p9_free_req(clnt, req);
  1843. return err;
  1844. }
  1845. EXPORT_SYMBOL(p9_client_mkdir_dotl);
  1846. int p9_client_lock_dotl(struct p9_fid *fid, struct p9_flock *flock, u8 *status)
  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, ">>> TLOCK fid %d type %i flags %d "
  1854. "start %lld length %lld proc_id %d client_id %s\n",
  1855. fid->fid, flock->type, flock->flags, flock->start,
  1856. flock->length, flock->proc_id, flock->client_id);
  1857. req = p9_client_rpc(clnt, P9_TLOCK, "dbdqqds", fid->fid, flock->type,
  1858. flock->flags, flock->start, flock->length,
  1859. flock->proc_id, flock->client_id);
  1860. if (IS_ERR(req))
  1861. return PTR_ERR(req);
  1862. err = p9pdu_readf(req->rc, clnt->proto_version, "b", status);
  1863. if (err) {
  1864. trace_9p_protocol_dump(clnt, req->rc);
  1865. goto error;
  1866. }
  1867. p9_debug(P9_DEBUG_9P, "<<< RLOCK status %i\n", *status);
  1868. error:
  1869. p9_free_req(clnt, req);
  1870. return err;
  1871. }
  1872. EXPORT_SYMBOL(p9_client_lock_dotl);
  1873. int p9_client_getlock_dotl(struct p9_fid *fid, struct p9_getlock *glock)
  1874. {
  1875. int err;
  1876. struct p9_client *clnt;
  1877. struct p9_req_t *req;
  1878. err = 0;
  1879. clnt = fid->clnt;
  1880. p9_debug(P9_DEBUG_9P, ">>> TGETLOCK fid %d, type %i start %lld "
  1881. "length %lld proc_id %d client_id %s\n", fid->fid, glock->type,
  1882. glock->start, glock->length, glock->proc_id, glock->client_id);
  1883. req = p9_client_rpc(clnt, P9_TGETLOCK, "dbqqds", fid->fid, glock->type,
  1884. glock->start, glock->length, glock->proc_id, glock->client_id);
  1885. if (IS_ERR(req))
  1886. return PTR_ERR(req);
  1887. err = p9pdu_readf(req->rc, clnt->proto_version, "bqqds", &glock->type,
  1888. &glock->start, &glock->length, &glock->proc_id,
  1889. &glock->client_id);
  1890. if (err) {
  1891. trace_9p_protocol_dump(clnt, req->rc);
  1892. goto error;
  1893. }
  1894. p9_debug(P9_DEBUG_9P, "<<< RGETLOCK type %i start %lld length %lld "
  1895. "proc_id %d client_id %s\n", glock->type, glock->start,
  1896. glock->length, glock->proc_id, glock->client_id);
  1897. error:
  1898. p9_free_req(clnt, req);
  1899. return err;
  1900. }
  1901. EXPORT_SYMBOL(p9_client_getlock_dotl);
  1902. int p9_client_readlink(struct p9_fid *fid, char **target)
  1903. {
  1904. int err;
  1905. struct p9_client *clnt;
  1906. struct p9_req_t *req;
  1907. err = 0;
  1908. clnt = fid->clnt;
  1909. p9_debug(P9_DEBUG_9P, ">>> TREADLINK fid %d\n", fid->fid);
  1910. req = p9_client_rpc(clnt, P9_TREADLINK, "d", fid->fid);
  1911. if (IS_ERR(req))
  1912. return PTR_ERR(req);
  1913. err = p9pdu_readf(req->rc, clnt->proto_version, "s", target);
  1914. if (err) {
  1915. trace_9p_protocol_dump(clnt, req->rc);
  1916. goto error;
  1917. }
  1918. p9_debug(P9_DEBUG_9P, "<<< RREADLINK target %s\n", *target);
  1919. error:
  1920. p9_free_req(clnt, req);
  1921. return err;
  1922. }
  1923. EXPORT_SYMBOL(p9_client_readlink);