clnt.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784
  1. /*
  2. * linux/net/sunrpc/clnt.c
  3. *
  4. * This file contains the high-level RPC interface.
  5. * It is modeled as a finite state machine to support both synchronous
  6. * and asynchronous requests.
  7. *
  8. * - RPC header generation and argument serialization.
  9. * - Credential refresh.
  10. * - TCP connect handling.
  11. * - Retry of operation when it is suspected the operation failed because
  12. * of uid squashing on the server, or when the credentials were stale
  13. * and need to be refreshed, or when a packet was damaged in transit.
  14. * This may be have to be moved to the VFS layer.
  15. *
  16. * NB: BSD uses a more intelligent approach to guessing when a request
  17. * or reply has been lost by keeping the RTO estimate for each procedure.
  18. * We currently make do with a constant timeout value.
  19. *
  20. * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
  21. * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
  22. */
  23. #include <asm/system.h>
  24. #include <linux/module.h>
  25. #include <linux/types.h>
  26. #include <linux/kallsyms.h>
  27. #include <linux/mm.h>
  28. #include <linux/namei.h>
  29. #include <linux/mount.h>
  30. #include <linux/slab.h>
  31. #include <linux/utsname.h>
  32. #include <linux/workqueue.h>
  33. #include <linux/in6.h>
  34. #include <linux/sunrpc/clnt.h>
  35. #include <linux/sunrpc/rpc_pipe_fs.h>
  36. #include <linux/sunrpc/metrics.h>
  37. #include <linux/sunrpc/bc_xprt.h>
  38. #include "sunrpc.h"
  39. #ifdef RPC_DEBUG
  40. # define RPCDBG_FACILITY RPCDBG_CALL
  41. #endif
  42. #define dprint_status(t) \
  43. dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
  44. __func__, t->tk_status)
  45. /*
  46. * All RPC clients are linked into this list
  47. */
  48. static LIST_HEAD(all_clients);
  49. static DEFINE_SPINLOCK(rpc_client_lock);
  50. static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
  51. static void call_start(struct rpc_task *task);
  52. static void call_reserve(struct rpc_task *task);
  53. static void call_reserveresult(struct rpc_task *task);
  54. static void call_allocate(struct rpc_task *task);
  55. static void call_decode(struct rpc_task *task);
  56. static void call_bind(struct rpc_task *task);
  57. static void call_bind_status(struct rpc_task *task);
  58. static void call_transmit(struct rpc_task *task);
  59. #if defined(CONFIG_NFS_V4_1)
  60. static void call_bc_transmit(struct rpc_task *task);
  61. #endif /* CONFIG_NFS_V4_1 */
  62. static void call_status(struct rpc_task *task);
  63. static void call_transmit_status(struct rpc_task *task);
  64. static void call_refresh(struct rpc_task *task);
  65. static void call_refreshresult(struct rpc_task *task);
  66. static void call_timeout(struct rpc_task *task);
  67. static void call_connect(struct rpc_task *task);
  68. static void call_connect_status(struct rpc_task *task);
  69. static __be32 *rpc_encode_header(struct rpc_task *task);
  70. static __be32 *rpc_verify_header(struct rpc_task *task);
  71. static int rpc_ping(struct rpc_clnt *clnt);
  72. static void rpc_register_client(struct rpc_clnt *clnt)
  73. {
  74. spin_lock(&rpc_client_lock);
  75. list_add(&clnt->cl_clients, &all_clients);
  76. spin_unlock(&rpc_client_lock);
  77. }
  78. static void rpc_unregister_client(struct rpc_clnt *clnt)
  79. {
  80. spin_lock(&rpc_client_lock);
  81. list_del(&clnt->cl_clients);
  82. spin_unlock(&rpc_client_lock);
  83. }
  84. static int
  85. rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name)
  86. {
  87. static uint32_t clntid;
  88. struct nameidata nd;
  89. struct path path;
  90. char name[15];
  91. struct qstr q = {
  92. .name = name,
  93. };
  94. int error;
  95. clnt->cl_path.mnt = ERR_PTR(-ENOENT);
  96. clnt->cl_path.dentry = ERR_PTR(-ENOENT);
  97. if (dir_name == NULL)
  98. return 0;
  99. path.mnt = rpc_get_mount();
  100. if (IS_ERR(path.mnt))
  101. return PTR_ERR(path.mnt);
  102. error = vfs_path_lookup(path.mnt->mnt_root, path.mnt, dir_name, 0, &nd);
  103. if (error)
  104. goto err;
  105. for (;;) {
  106. q.len = snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
  107. name[sizeof(name) - 1] = '\0';
  108. q.hash = full_name_hash(q.name, q.len);
  109. path.dentry = rpc_create_client_dir(nd.path.dentry, &q, clnt);
  110. if (!IS_ERR(path.dentry))
  111. break;
  112. error = PTR_ERR(path.dentry);
  113. if (error != -EEXIST) {
  114. printk(KERN_INFO "RPC: Couldn't create pipefs entry"
  115. " %s/%s, error %d\n",
  116. dir_name, name, error);
  117. goto err_path_put;
  118. }
  119. }
  120. path_put(&nd.path);
  121. clnt->cl_path = path;
  122. return 0;
  123. err_path_put:
  124. path_put(&nd.path);
  125. err:
  126. rpc_put_mount();
  127. return error;
  128. }
  129. static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args, struct rpc_xprt *xprt)
  130. {
  131. struct rpc_program *program = args->program;
  132. struct rpc_version *version;
  133. struct rpc_clnt *clnt = NULL;
  134. struct rpc_auth *auth;
  135. int err;
  136. size_t len;
  137. /* sanity check the name before trying to print it */
  138. err = -EINVAL;
  139. len = strlen(args->servername);
  140. if (len > RPC_MAXNETNAMELEN)
  141. goto out_no_rpciod;
  142. len++;
  143. dprintk("RPC: creating %s client for %s (xprt %p)\n",
  144. program->name, args->servername, xprt);
  145. err = rpciod_up();
  146. if (err)
  147. goto out_no_rpciod;
  148. err = -EINVAL;
  149. if (!xprt)
  150. goto out_no_xprt;
  151. if (args->version >= program->nrvers)
  152. goto out_err;
  153. version = program->version[args->version];
  154. if (version == NULL)
  155. goto out_err;
  156. err = -ENOMEM;
  157. clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
  158. if (!clnt)
  159. goto out_err;
  160. clnt->cl_parent = clnt;
  161. clnt->cl_server = clnt->cl_inline_name;
  162. if (len > sizeof(clnt->cl_inline_name)) {
  163. char *buf = kmalloc(len, GFP_KERNEL);
  164. if (buf != NULL)
  165. clnt->cl_server = buf;
  166. else
  167. len = sizeof(clnt->cl_inline_name);
  168. }
  169. strlcpy(clnt->cl_server, args->servername, len);
  170. clnt->cl_xprt = xprt;
  171. clnt->cl_procinfo = version->procs;
  172. clnt->cl_maxproc = version->nrprocs;
  173. clnt->cl_protname = program->name;
  174. clnt->cl_prog = args->prognumber ? : program->number;
  175. clnt->cl_vers = version->number;
  176. clnt->cl_stats = program->stats;
  177. clnt->cl_metrics = rpc_alloc_iostats(clnt);
  178. err = -ENOMEM;
  179. if (clnt->cl_metrics == NULL)
  180. goto out_no_stats;
  181. clnt->cl_program = program;
  182. INIT_LIST_HEAD(&clnt->cl_tasks);
  183. spin_lock_init(&clnt->cl_lock);
  184. if (!xprt_bound(clnt->cl_xprt))
  185. clnt->cl_autobind = 1;
  186. clnt->cl_timeout = xprt->timeout;
  187. if (args->timeout != NULL) {
  188. memcpy(&clnt->cl_timeout_default, args->timeout,
  189. sizeof(clnt->cl_timeout_default));
  190. clnt->cl_timeout = &clnt->cl_timeout_default;
  191. }
  192. clnt->cl_rtt = &clnt->cl_rtt_default;
  193. rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
  194. clnt->cl_principal = NULL;
  195. if (args->client_name) {
  196. clnt->cl_principal = kstrdup(args->client_name, GFP_KERNEL);
  197. if (!clnt->cl_principal)
  198. goto out_no_principal;
  199. }
  200. kref_init(&clnt->cl_kref);
  201. err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
  202. if (err < 0)
  203. goto out_no_path;
  204. auth = rpcauth_create(args->authflavor, clnt);
  205. if (IS_ERR(auth)) {
  206. printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
  207. args->authflavor);
  208. err = PTR_ERR(auth);
  209. goto out_no_auth;
  210. }
  211. /* save the nodename */
  212. clnt->cl_nodelen = strlen(init_utsname()->nodename);
  213. if (clnt->cl_nodelen > UNX_MAXNODENAME)
  214. clnt->cl_nodelen = UNX_MAXNODENAME;
  215. memcpy(clnt->cl_nodename, init_utsname()->nodename, clnt->cl_nodelen);
  216. rpc_register_client(clnt);
  217. return clnt;
  218. out_no_auth:
  219. if (!IS_ERR(clnt->cl_path.dentry)) {
  220. rpc_remove_client_dir(clnt->cl_path.dentry);
  221. rpc_put_mount();
  222. }
  223. out_no_path:
  224. kfree(clnt->cl_principal);
  225. out_no_principal:
  226. rpc_free_iostats(clnt->cl_metrics);
  227. out_no_stats:
  228. if (clnt->cl_server != clnt->cl_inline_name)
  229. kfree(clnt->cl_server);
  230. kfree(clnt);
  231. out_err:
  232. xprt_put(xprt);
  233. out_no_xprt:
  234. rpciod_down();
  235. out_no_rpciod:
  236. return ERR_PTR(err);
  237. }
  238. /*
  239. * rpc_create - create an RPC client and transport with one call
  240. * @args: rpc_clnt create argument structure
  241. *
  242. * Creates and initializes an RPC transport and an RPC client.
  243. *
  244. * It can ping the server in order to determine if it is up, and to see if
  245. * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
  246. * this behavior so asynchronous tasks can also use rpc_create.
  247. */
  248. struct rpc_clnt *rpc_create(struct rpc_create_args *args)
  249. {
  250. struct rpc_xprt *xprt;
  251. struct rpc_clnt *clnt;
  252. struct xprt_create xprtargs = {
  253. .ident = args->protocol,
  254. .srcaddr = args->saddress,
  255. .dstaddr = args->address,
  256. .addrlen = args->addrsize,
  257. .bc_xprt = args->bc_xprt,
  258. };
  259. char servername[48];
  260. /*
  261. * If the caller chooses not to specify a hostname, whip
  262. * up a string representation of the passed-in address.
  263. */
  264. if (args->servername == NULL) {
  265. servername[0] = '\0';
  266. switch (args->address->sa_family) {
  267. case AF_INET: {
  268. struct sockaddr_in *sin =
  269. (struct sockaddr_in *)args->address;
  270. snprintf(servername, sizeof(servername), "%pI4",
  271. &sin->sin_addr.s_addr);
  272. break;
  273. }
  274. case AF_INET6: {
  275. struct sockaddr_in6 *sin =
  276. (struct sockaddr_in6 *)args->address;
  277. snprintf(servername, sizeof(servername), "%pI6",
  278. &sin->sin6_addr);
  279. break;
  280. }
  281. default:
  282. /* caller wants default server name, but
  283. * address family isn't recognized. */
  284. return ERR_PTR(-EINVAL);
  285. }
  286. args->servername = servername;
  287. }
  288. xprt = xprt_create_transport(&xprtargs);
  289. if (IS_ERR(xprt))
  290. return (struct rpc_clnt *)xprt;
  291. /*
  292. * By default, kernel RPC client connects from a reserved port.
  293. * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
  294. * but it is always enabled for rpciod, which handles the connect
  295. * operation.
  296. */
  297. xprt->resvport = 1;
  298. if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
  299. xprt->resvport = 0;
  300. clnt = rpc_new_client(args, xprt);
  301. if (IS_ERR(clnt))
  302. return clnt;
  303. if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
  304. int err = rpc_ping(clnt);
  305. if (err != 0) {
  306. rpc_shutdown_client(clnt);
  307. return ERR_PTR(err);
  308. }
  309. }
  310. clnt->cl_softrtry = 1;
  311. if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
  312. clnt->cl_softrtry = 0;
  313. if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
  314. clnt->cl_autobind = 1;
  315. if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
  316. clnt->cl_discrtry = 1;
  317. if (!(args->flags & RPC_CLNT_CREATE_QUIET))
  318. clnt->cl_chatty = 1;
  319. return clnt;
  320. }
  321. EXPORT_SYMBOL_GPL(rpc_create);
  322. /*
  323. * This function clones the RPC client structure. It allows us to share the
  324. * same transport while varying parameters such as the authentication
  325. * flavour.
  326. */
  327. struct rpc_clnt *
  328. rpc_clone_client(struct rpc_clnt *clnt)
  329. {
  330. struct rpc_clnt *new;
  331. int err = -ENOMEM;
  332. new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
  333. if (!new)
  334. goto out_no_clnt;
  335. new->cl_parent = clnt;
  336. /* Turn off autobind on clones */
  337. new->cl_autobind = 0;
  338. INIT_LIST_HEAD(&new->cl_tasks);
  339. spin_lock_init(&new->cl_lock);
  340. rpc_init_rtt(&new->cl_rtt_default, clnt->cl_timeout->to_initval);
  341. new->cl_metrics = rpc_alloc_iostats(clnt);
  342. if (new->cl_metrics == NULL)
  343. goto out_no_stats;
  344. if (clnt->cl_principal) {
  345. new->cl_principal = kstrdup(clnt->cl_principal, GFP_KERNEL);
  346. if (new->cl_principal == NULL)
  347. goto out_no_principal;
  348. }
  349. kref_init(&new->cl_kref);
  350. err = rpc_setup_pipedir(new, clnt->cl_program->pipe_dir_name);
  351. if (err != 0)
  352. goto out_no_path;
  353. if (new->cl_auth)
  354. atomic_inc(&new->cl_auth->au_count);
  355. xprt_get(clnt->cl_xprt);
  356. kref_get(&clnt->cl_kref);
  357. rpc_register_client(new);
  358. rpciod_up();
  359. return new;
  360. out_no_path:
  361. kfree(new->cl_principal);
  362. out_no_principal:
  363. rpc_free_iostats(new->cl_metrics);
  364. out_no_stats:
  365. kfree(new);
  366. out_no_clnt:
  367. dprintk("RPC: %s: returned error %d\n", __func__, err);
  368. return ERR_PTR(err);
  369. }
  370. EXPORT_SYMBOL_GPL(rpc_clone_client);
  371. /*
  372. * Properly shut down an RPC client, terminating all outstanding
  373. * requests.
  374. */
  375. void rpc_shutdown_client(struct rpc_clnt *clnt)
  376. {
  377. dprintk("RPC: shutting down %s client for %s\n",
  378. clnt->cl_protname, clnt->cl_server);
  379. while (!list_empty(&clnt->cl_tasks)) {
  380. rpc_killall_tasks(clnt);
  381. wait_event_timeout(destroy_wait,
  382. list_empty(&clnt->cl_tasks), 1*HZ);
  383. }
  384. rpc_release_client(clnt);
  385. }
  386. EXPORT_SYMBOL_GPL(rpc_shutdown_client);
  387. /*
  388. * Free an RPC client
  389. */
  390. static void
  391. rpc_free_client(struct kref *kref)
  392. {
  393. struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
  394. dprintk("RPC: destroying %s client for %s\n",
  395. clnt->cl_protname, clnt->cl_server);
  396. if (!IS_ERR(clnt->cl_path.dentry)) {
  397. rpc_remove_client_dir(clnt->cl_path.dentry);
  398. rpc_put_mount();
  399. }
  400. if (clnt->cl_parent != clnt) {
  401. rpc_release_client(clnt->cl_parent);
  402. goto out_free;
  403. }
  404. if (clnt->cl_server != clnt->cl_inline_name)
  405. kfree(clnt->cl_server);
  406. out_free:
  407. rpc_unregister_client(clnt);
  408. rpc_free_iostats(clnt->cl_metrics);
  409. kfree(clnt->cl_principal);
  410. clnt->cl_metrics = NULL;
  411. xprt_put(clnt->cl_xprt);
  412. rpciod_down();
  413. kfree(clnt);
  414. }
  415. /*
  416. * Free an RPC client
  417. */
  418. static void
  419. rpc_free_auth(struct kref *kref)
  420. {
  421. struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
  422. if (clnt->cl_auth == NULL) {
  423. rpc_free_client(kref);
  424. return;
  425. }
  426. /*
  427. * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
  428. * release remaining GSS contexts. This mechanism ensures
  429. * that it can do so safely.
  430. */
  431. kref_init(kref);
  432. rpcauth_release(clnt->cl_auth);
  433. clnt->cl_auth = NULL;
  434. kref_put(kref, rpc_free_client);
  435. }
  436. /*
  437. * Release reference to the RPC client
  438. */
  439. void
  440. rpc_release_client(struct rpc_clnt *clnt)
  441. {
  442. dprintk("RPC: rpc_release_client(%p)\n", clnt);
  443. if (list_empty(&clnt->cl_tasks))
  444. wake_up(&destroy_wait);
  445. kref_put(&clnt->cl_kref, rpc_free_auth);
  446. }
  447. /**
  448. * rpc_bind_new_program - bind a new RPC program to an existing client
  449. * @old: old rpc_client
  450. * @program: rpc program to set
  451. * @vers: rpc program version
  452. *
  453. * Clones the rpc client and sets up a new RPC program. This is mainly
  454. * of use for enabling different RPC programs to share the same transport.
  455. * The Sun NFSv2/v3 ACL protocol can do this.
  456. */
  457. struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
  458. struct rpc_program *program,
  459. u32 vers)
  460. {
  461. struct rpc_clnt *clnt;
  462. struct rpc_version *version;
  463. int err;
  464. BUG_ON(vers >= program->nrvers || !program->version[vers]);
  465. version = program->version[vers];
  466. clnt = rpc_clone_client(old);
  467. if (IS_ERR(clnt))
  468. goto out;
  469. clnt->cl_procinfo = version->procs;
  470. clnt->cl_maxproc = version->nrprocs;
  471. clnt->cl_protname = program->name;
  472. clnt->cl_prog = program->number;
  473. clnt->cl_vers = version->number;
  474. clnt->cl_stats = program->stats;
  475. err = rpc_ping(clnt);
  476. if (err != 0) {
  477. rpc_shutdown_client(clnt);
  478. clnt = ERR_PTR(err);
  479. }
  480. out:
  481. return clnt;
  482. }
  483. EXPORT_SYMBOL_GPL(rpc_bind_new_program);
  484. /*
  485. * Default callback for async RPC calls
  486. */
  487. static void
  488. rpc_default_callback(struct rpc_task *task, void *data)
  489. {
  490. }
  491. static const struct rpc_call_ops rpc_default_ops = {
  492. .rpc_call_done = rpc_default_callback,
  493. };
  494. /**
  495. * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
  496. * @task_setup_data: pointer to task initialisation data
  497. */
  498. struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
  499. {
  500. struct rpc_task *task;
  501. task = rpc_new_task(task_setup_data);
  502. if (IS_ERR(task))
  503. goto out;
  504. atomic_inc(&task->tk_count);
  505. rpc_execute(task);
  506. out:
  507. return task;
  508. }
  509. EXPORT_SYMBOL_GPL(rpc_run_task);
  510. /**
  511. * rpc_call_sync - Perform a synchronous RPC call
  512. * @clnt: pointer to RPC client
  513. * @msg: RPC call parameters
  514. * @flags: RPC call flags
  515. */
  516. int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
  517. {
  518. struct rpc_task *task;
  519. struct rpc_task_setup task_setup_data = {
  520. .rpc_client = clnt,
  521. .rpc_message = msg,
  522. .callback_ops = &rpc_default_ops,
  523. .flags = flags,
  524. };
  525. int status;
  526. BUG_ON(flags & RPC_TASK_ASYNC);
  527. task = rpc_run_task(&task_setup_data);
  528. if (IS_ERR(task))
  529. return PTR_ERR(task);
  530. status = task->tk_status;
  531. rpc_put_task(task);
  532. return status;
  533. }
  534. EXPORT_SYMBOL_GPL(rpc_call_sync);
  535. /**
  536. * rpc_call_async - Perform an asynchronous RPC call
  537. * @clnt: pointer to RPC client
  538. * @msg: RPC call parameters
  539. * @flags: RPC call flags
  540. * @tk_ops: RPC call ops
  541. * @data: user call data
  542. */
  543. int
  544. rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
  545. const struct rpc_call_ops *tk_ops, void *data)
  546. {
  547. struct rpc_task *task;
  548. struct rpc_task_setup task_setup_data = {
  549. .rpc_client = clnt,
  550. .rpc_message = msg,
  551. .callback_ops = tk_ops,
  552. .callback_data = data,
  553. .flags = flags|RPC_TASK_ASYNC,
  554. };
  555. task = rpc_run_task(&task_setup_data);
  556. if (IS_ERR(task))
  557. return PTR_ERR(task);
  558. rpc_put_task(task);
  559. return 0;
  560. }
  561. EXPORT_SYMBOL_GPL(rpc_call_async);
  562. #if defined(CONFIG_NFS_V4_1)
  563. /**
  564. * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
  565. * rpc_execute against it
  566. * @req: RPC request
  567. * @tk_ops: RPC call ops
  568. */
  569. struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req,
  570. const struct rpc_call_ops *tk_ops)
  571. {
  572. struct rpc_task *task;
  573. struct xdr_buf *xbufp = &req->rq_snd_buf;
  574. struct rpc_task_setup task_setup_data = {
  575. .callback_ops = tk_ops,
  576. };
  577. dprintk("RPC: rpc_run_bc_task req= %p\n", req);
  578. /*
  579. * Create an rpc_task to send the data
  580. */
  581. task = rpc_new_task(&task_setup_data);
  582. if (IS_ERR(task)) {
  583. xprt_free_bc_request(req);
  584. goto out;
  585. }
  586. task->tk_rqstp = req;
  587. /*
  588. * Set up the xdr_buf length.
  589. * This also indicates that the buffer is XDR encoded already.
  590. */
  591. xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
  592. xbufp->tail[0].iov_len;
  593. task->tk_action = call_bc_transmit;
  594. atomic_inc(&task->tk_count);
  595. BUG_ON(atomic_read(&task->tk_count) != 2);
  596. rpc_execute(task);
  597. out:
  598. dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
  599. return task;
  600. }
  601. #endif /* CONFIG_NFS_V4_1 */
  602. void
  603. rpc_call_start(struct rpc_task *task)
  604. {
  605. task->tk_action = call_start;
  606. }
  607. EXPORT_SYMBOL_GPL(rpc_call_start);
  608. /**
  609. * rpc_peeraddr - extract remote peer address from clnt's xprt
  610. * @clnt: RPC client structure
  611. * @buf: target buffer
  612. * @bufsize: length of target buffer
  613. *
  614. * Returns the number of bytes that are actually in the stored address.
  615. */
  616. size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
  617. {
  618. size_t bytes;
  619. struct rpc_xprt *xprt = clnt->cl_xprt;
  620. bytes = sizeof(xprt->addr);
  621. if (bytes > bufsize)
  622. bytes = bufsize;
  623. memcpy(buf, &clnt->cl_xprt->addr, bytes);
  624. return xprt->addrlen;
  625. }
  626. EXPORT_SYMBOL_GPL(rpc_peeraddr);
  627. /**
  628. * rpc_peeraddr2str - return remote peer address in printable format
  629. * @clnt: RPC client structure
  630. * @format: address format
  631. *
  632. */
  633. const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
  634. enum rpc_display_format_t format)
  635. {
  636. struct rpc_xprt *xprt = clnt->cl_xprt;
  637. if (xprt->address_strings[format] != NULL)
  638. return xprt->address_strings[format];
  639. else
  640. return "unprintable";
  641. }
  642. EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
  643. void
  644. rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
  645. {
  646. struct rpc_xprt *xprt = clnt->cl_xprt;
  647. if (xprt->ops->set_buffer_size)
  648. xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
  649. }
  650. EXPORT_SYMBOL_GPL(rpc_setbufsize);
  651. /*
  652. * Return size of largest payload RPC client can support, in bytes
  653. *
  654. * For stream transports, this is one RPC record fragment (see RFC
  655. * 1831), as we don't support multi-record requests yet. For datagram
  656. * transports, this is the size of an IP packet minus the IP, UDP, and
  657. * RPC header sizes.
  658. */
  659. size_t rpc_max_payload(struct rpc_clnt *clnt)
  660. {
  661. return clnt->cl_xprt->max_payload;
  662. }
  663. EXPORT_SYMBOL_GPL(rpc_max_payload);
  664. /**
  665. * rpc_force_rebind - force transport to check that remote port is unchanged
  666. * @clnt: client to rebind
  667. *
  668. */
  669. void rpc_force_rebind(struct rpc_clnt *clnt)
  670. {
  671. if (clnt->cl_autobind)
  672. xprt_clear_bound(clnt->cl_xprt);
  673. }
  674. EXPORT_SYMBOL_GPL(rpc_force_rebind);
  675. /*
  676. * Restart an (async) RPC call from the call_prepare state.
  677. * Usually called from within the exit handler.
  678. */
  679. int
  680. rpc_restart_call_prepare(struct rpc_task *task)
  681. {
  682. if (RPC_ASSASSINATED(task))
  683. return 0;
  684. task->tk_action = rpc_prepare_task;
  685. return 1;
  686. }
  687. EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
  688. /*
  689. * Restart an (async) RPC call. Usually called from within the
  690. * exit handler.
  691. */
  692. int
  693. rpc_restart_call(struct rpc_task *task)
  694. {
  695. if (RPC_ASSASSINATED(task))
  696. return 0;
  697. task->tk_action = call_start;
  698. return 1;
  699. }
  700. EXPORT_SYMBOL_GPL(rpc_restart_call);
  701. #ifdef RPC_DEBUG
  702. static const char *rpc_proc_name(const struct rpc_task *task)
  703. {
  704. const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
  705. if (proc) {
  706. if (proc->p_name)
  707. return proc->p_name;
  708. else
  709. return "NULL";
  710. } else
  711. return "no proc";
  712. }
  713. #endif
  714. /*
  715. * 0. Initial state
  716. *
  717. * Other FSM states can be visited zero or more times, but
  718. * this state is visited exactly once for each RPC.
  719. */
  720. static void
  721. call_start(struct rpc_task *task)
  722. {
  723. struct rpc_clnt *clnt = task->tk_client;
  724. dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
  725. clnt->cl_protname, clnt->cl_vers,
  726. rpc_proc_name(task),
  727. (RPC_IS_ASYNC(task) ? "async" : "sync"));
  728. /* Increment call count */
  729. task->tk_msg.rpc_proc->p_count++;
  730. clnt->cl_stats->rpccnt++;
  731. task->tk_action = call_reserve;
  732. }
  733. /*
  734. * 1. Reserve an RPC call slot
  735. */
  736. static void
  737. call_reserve(struct rpc_task *task)
  738. {
  739. dprint_status(task);
  740. if (!rpcauth_uptodatecred(task)) {
  741. task->tk_action = call_refresh;
  742. return;
  743. }
  744. task->tk_status = 0;
  745. task->tk_action = call_reserveresult;
  746. xprt_reserve(task);
  747. }
  748. /*
  749. * 1b. Grok the result of xprt_reserve()
  750. */
  751. static void
  752. call_reserveresult(struct rpc_task *task)
  753. {
  754. int status = task->tk_status;
  755. dprint_status(task);
  756. /*
  757. * After a call to xprt_reserve(), we must have either
  758. * a request slot or else an error status.
  759. */
  760. task->tk_status = 0;
  761. if (status >= 0) {
  762. if (task->tk_rqstp) {
  763. task->tk_action = call_allocate;
  764. return;
  765. }
  766. printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
  767. __func__, status);
  768. rpc_exit(task, -EIO);
  769. return;
  770. }
  771. /*
  772. * Even though there was an error, we may have acquired
  773. * a request slot somehow. Make sure not to leak it.
  774. */
  775. if (task->tk_rqstp) {
  776. printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
  777. __func__, status);
  778. xprt_release(task);
  779. }
  780. switch (status) {
  781. case -EAGAIN: /* woken up; retry */
  782. task->tk_action = call_reserve;
  783. return;
  784. case -EIO: /* probably a shutdown */
  785. break;
  786. default:
  787. printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
  788. __func__, status);
  789. break;
  790. }
  791. rpc_exit(task, status);
  792. }
  793. /*
  794. * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
  795. * (Note: buffer memory is freed in xprt_release).
  796. */
  797. static void
  798. call_allocate(struct rpc_task *task)
  799. {
  800. unsigned int slack = task->tk_msg.rpc_cred->cr_auth->au_cslack;
  801. struct rpc_rqst *req = task->tk_rqstp;
  802. struct rpc_xprt *xprt = task->tk_xprt;
  803. struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
  804. dprint_status(task);
  805. task->tk_status = 0;
  806. task->tk_action = call_bind;
  807. if (req->rq_buffer)
  808. return;
  809. if (proc->p_proc != 0) {
  810. BUG_ON(proc->p_arglen == 0);
  811. if (proc->p_decode != NULL)
  812. BUG_ON(proc->p_replen == 0);
  813. }
  814. /*
  815. * Calculate the size (in quads) of the RPC call
  816. * and reply headers, and convert both values
  817. * to byte sizes.
  818. */
  819. req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
  820. req->rq_callsize <<= 2;
  821. req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
  822. req->rq_rcvsize <<= 2;
  823. req->rq_buffer = xprt->ops->buf_alloc(task,
  824. req->rq_callsize + req->rq_rcvsize);
  825. if (req->rq_buffer != NULL)
  826. return;
  827. dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
  828. if (RPC_IS_ASYNC(task) || !signalled()) {
  829. task->tk_action = call_allocate;
  830. rpc_delay(task, HZ>>4);
  831. return;
  832. }
  833. rpc_exit(task, -ERESTARTSYS);
  834. }
  835. static inline int
  836. rpc_task_need_encode(struct rpc_task *task)
  837. {
  838. return task->tk_rqstp->rq_snd_buf.len == 0;
  839. }
  840. static inline void
  841. rpc_task_force_reencode(struct rpc_task *task)
  842. {
  843. task->tk_rqstp->rq_snd_buf.len = 0;
  844. task->tk_rqstp->rq_bytes_sent = 0;
  845. }
  846. static inline void
  847. rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
  848. {
  849. buf->head[0].iov_base = start;
  850. buf->head[0].iov_len = len;
  851. buf->tail[0].iov_len = 0;
  852. buf->page_len = 0;
  853. buf->flags = 0;
  854. buf->len = 0;
  855. buf->buflen = len;
  856. }
  857. /*
  858. * 3. Encode arguments of an RPC call
  859. */
  860. static void
  861. rpc_xdr_encode(struct rpc_task *task)
  862. {
  863. struct rpc_rqst *req = task->tk_rqstp;
  864. kxdrproc_t encode;
  865. __be32 *p;
  866. dprint_status(task);
  867. rpc_xdr_buf_init(&req->rq_snd_buf,
  868. req->rq_buffer,
  869. req->rq_callsize);
  870. rpc_xdr_buf_init(&req->rq_rcv_buf,
  871. (char *)req->rq_buffer + req->rq_callsize,
  872. req->rq_rcvsize);
  873. p = rpc_encode_header(task);
  874. if (p == NULL) {
  875. printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
  876. rpc_exit(task, -EIO);
  877. return;
  878. }
  879. encode = task->tk_msg.rpc_proc->p_encode;
  880. if (encode == NULL)
  881. return;
  882. task->tk_status = rpcauth_wrap_req(task, encode, req, p,
  883. task->tk_msg.rpc_argp);
  884. }
  885. /*
  886. * 4. Get the server port number if not yet set
  887. */
  888. static void
  889. call_bind(struct rpc_task *task)
  890. {
  891. struct rpc_xprt *xprt = task->tk_xprt;
  892. dprint_status(task);
  893. task->tk_action = call_connect;
  894. if (!xprt_bound(xprt)) {
  895. task->tk_action = call_bind_status;
  896. task->tk_timeout = xprt->bind_timeout;
  897. xprt->ops->rpcbind(task);
  898. }
  899. }
  900. /*
  901. * 4a. Sort out bind result
  902. */
  903. static void
  904. call_bind_status(struct rpc_task *task)
  905. {
  906. int status = -EIO;
  907. if (task->tk_status >= 0) {
  908. dprint_status(task);
  909. task->tk_status = 0;
  910. task->tk_action = call_connect;
  911. return;
  912. }
  913. switch (task->tk_status) {
  914. case -ENOMEM:
  915. dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
  916. rpc_delay(task, HZ >> 2);
  917. goto retry_timeout;
  918. case -EACCES:
  919. dprintk("RPC: %5u remote rpcbind: RPC program/version "
  920. "unavailable\n", task->tk_pid);
  921. /* fail immediately if this is an RPC ping */
  922. if (task->tk_msg.rpc_proc->p_proc == 0) {
  923. status = -EOPNOTSUPP;
  924. break;
  925. }
  926. rpc_delay(task, 3*HZ);
  927. goto retry_timeout;
  928. case -ETIMEDOUT:
  929. dprintk("RPC: %5u rpcbind request timed out\n",
  930. task->tk_pid);
  931. goto retry_timeout;
  932. case -EPFNOSUPPORT:
  933. /* server doesn't support any rpcbind version we know of */
  934. dprintk("RPC: %5u unrecognized remote rpcbind service\n",
  935. task->tk_pid);
  936. break;
  937. case -EPROTONOSUPPORT:
  938. dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
  939. task->tk_pid);
  940. task->tk_status = 0;
  941. task->tk_action = call_bind;
  942. return;
  943. case -ECONNREFUSED: /* connection problems */
  944. case -ECONNRESET:
  945. case -ENOTCONN:
  946. case -EHOSTDOWN:
  947. case -EHOSTUNREACH:
  948. case -ENETUNREACH:
  949. case -EPIPE:
  950. dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
  951. task->tk_pid, task->tk_status);
  952. if (!RPC_IS_SOFTCONN(task)) {
  953. rpc_delay(task, 5*HZ);
  954. goto retry_timeout;
  955. }
  956. status = task->tk_status;
  957. break;
  958. default:
  959. dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
  960. task->tk_pid, -task->tk_status);
  961. }
  962. rpc_exit(task, status);
  963. return;
  964. retry_timeout:
  965. task->tk_action = call_timeout;
  966. }
  967. /*
  968. * 4b. Connect to the RPC server
  969. */
  970. static void
  971. call_connect(struct rpc_task *task)
  972. {
  973. struct rpc_xprt *xprt = task->tk_xprt;
  974. dprintk("RPC: %5u call_connect xprt %p %s connected\n",
  975. task->tk_pid, xprt,
  976. (xprt_connected(xprt) ? "is" : "is not"));
  977. task->tk_action = call_transmit;
  978. if (!xprt_connected(xprt)) {
  979. task->tk_action = call_connect_status;
  980. if (task->tk_status < 0)
  981. return;
  982. xprt_connect(task);
  983. }
  984. }
  985. /*
  986. * 4c. Sort out connect result
  987. */
  988. static void
  989. call_connect_status(struct rpc_task *task)
  990. {
  991. struct rpc_clnt *clnt = task->tk_client;
  992. int status = task->tk_status;
  993. dprint_status(task);
  994. task->tk_status = 0;
  995. if (status >= 0 || status == -EAGAIN) {
  996. clnt->cl_stats->netreconn++;
  997. task->tk_action = call_transmit;
  998. return;
  999. }
  1000. switch (status) {
  1001. /* if soft mounted, test if we've timed out */
  1002. case -ETIMEDOUT:
  1003. task->tk_action = call_timeout;
  1004. break;
  1005. default:
  1006. rpc_exit(task, -EIO);
  1007. }
  1008. }
  1009. /*
  1010. * 5. Transmit the RPC request, and wait for reply
  1011. */
  1012. static void
  1013. call_transmit(struct rpc_task *task)
  1014. {
  1015. dprint_status(task);
  1016. task->tk_action = call_status;
  1017. if (task->tk_status < 0)
  1018. return;
  1019. task->tk_status = xprt_prepare_transmit(task);
  1020. if (task->tk_status != 0)
  1021. return;
  1022. task->tk_action = call_transmit_status;
  1023. /* Encode here so that rpcsec_gss can use correct sequence number. */
  1024. if (rpc_task_need_encode(task)) {
  1025. BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
  1026. rpc_xdr_encode(task);
  1027. /* Did the encode result in an error condition? */
  1028. if (task->tk_status != 0) {
  1029. /* Was the error nonfatal? */
  1030. if (task->tk_status == -EAGAIN)
  1031. rpc_delay(task, HZ >> 4);
  1032. else
  1033. rpc_exit(task, task->tk_status);
  1034. return;
  1035. }
  1036. }
  1037. xprt_transmit(task);
  1038. if (task->tk_status < 0)
  1039. return;
  1040. /*
  1041. * On success, ensure that we call xprt_end_transmit() before sleeping
  1042. * in order to allow access to the socket to other RPC requests.
  1043. */
  1044. call_transmit_status(task);
  1045. if (rpc_reply_expected(task))
  1046. return;
  1047. task->tk_action = rpc_exit_task;
  1048. rpc_wake_up_queued_task(&task->tk_xprt->pending, task);
  1049. }
  1050. /*
  1051. * 5a. Handle cleanup after a transmission
  1052. */
  1053. static void
  1054. call_transmit_status(struct rpc_task *task)
  1055. {
  1056. task->tk_action = call_status;
  1057. /*
  1058. * Common case: success. Force the compiler to put this
  1059. * test first.
  1060. */
  1061. if (task->tk_status == 0) {
  1062. xprt_end_transmit(task);
  1063. rpc_task_force_reencode(task);
  1064. return;
  1065. }
  1066. switch (task->tk_status) {
  1067. case -EAGAIN:
  1068. break;
  1069. default:
  1070. dprint_status(task);
  1071. xprt_end_transmit(task);
  1072. rpc_task_force_reencode(task);
  1073. break;
  1074. /*
  1075. * Special cases: if we've been waiting on the
  1076. * socket's write_space() callback, or if the
  1077. * socket just returned a connection error,
  1078. * then hold onto the transport lock.
  1079. */
  1080. case -ECONNREFUSED:
  1081. case -EHOSTDOWN:
  1082. case -EHOSTUNREACH:
  1083. case -ENETUNREACH:
  1084. if (RPC_IS_SOFTCONN(task)) {
  1085. xprt_end_transmit(task);
  1086. rpc_exit(task, task->tk_status);
  1087. break;
  1088. }
  1089. case -ECONNRESET:
  1090. case -ENOTCONN:
  1091. case -EPIPE:
  1092. rpc_task_force_reencode(task);
  1093. }
  1094. }
  1095. #if defined(CONFIG_NFS_V4_1)
  1096. /*
  1097. * 5b. Send the backchannel RPC reply. On error, drop the reply. In
  1098. * addition, disconnect on connectivity errors.
  1099. */
  1100. static void
  1101. call_bc_transmit(struct rpc_task *task)
  1102. {
  1103. struct rpc_rqst *req = task->tk_rqstp;
  1104. BUG_ON(task->tk_status != 0);
  1105. task->tk_status = xprt_prepare_transmit(task);
  1106. if (task->tk_status == -EAGAIN) {
  1107. /*
  1108. * Could not reserve the transport. Try again after the
  1109. * transport is released.
  1110. */
  1111. task->tk_status = 0;
  1112. task->tk_action = call_bc_transmit;
  1113. return;
  1114. }
  1115. task->tk_action = rpc_exit_task;
  1116. if (task->tk_status < 0) {
  1117. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1118. "error: %d\n", task->tk_status);
  1119. return;
  1120. }
  1121. xprt_transmit(task);
  1122. xprt_end_transmit(task);
  1123. dprint_status(task);
  1124. switch (task->tk_status) {
  1125. case 0:
  1126. /* Success */
  1127. break;
  1128. case -EHOSTDOWN:
  1129. case -EHOSTUNREACH:
  1130. case -ENETUNREACH:
  1131. case -ETIMEDOUT:
  1132. /*
  1133. * Problem reaching the server. Disconnect and let the
  1134. * forechannel reestablish the connection. The server will
  1135. * have to retransmit the backchannel request and we'll
  1136. * reprocess it. Since these ops are idempotent, there's no
  1137. * need to cache our reply at this time.
  1138. */
  1139. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1140. "error: %d\n", task->tk_status);
  1141. xprt_conditional_disconnect(task->tk_xprt,
  1142. req->rq_connect_cookie);
  1143. break;
  1144. default:
  1145. /*
  1146. * We were unable to reply and will have to drop the
  1147. * request. The server should reconnect and retransmit.
  1148. */
  1149. BUG_ON(task->tk_status == -EAGAIN);
  1150. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1151. "error: %d\n", task->tk_status);
  1152. break;
  1153. }
  1154. rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
  1155. }
  1156. #endif /* CONFIG_NFS_V4_1 */
  1157. /*
  1158. * 6. Sort out the RPC call status
  1159. */
  1160. static void
  1161. call_status(struct rpc_task *task)
  1162. {
  1163. struct rpc_clnt *clnt = task->tk_client;
  1164. struct rpc_rqst *req = task->tk_rqstp;
  1165. int status;
  1166. if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
  1167. task->tk_status = req->rq_reply_bytes_recvd;
  1168. dprint_status(task);
  1169. status = task->tk_status;
  1170. if (status >= 0) {
  1171. task->tk_action = call_decode;
  1172. return;
  1173. }
  1174. task->tk_status = 0;
  1175. switch(status) {
  1176. case -EHOSTDOWN:
  1177. case -EHOSTUNREACH:
  1178. case -ENETUNREACH:
  1179. /*
  1180. * Delay any retries for 3 seconds, then handle as if it
  1181. * were a timeout.
  1182. */
  1183. rpc_delay(task, 3*HZ);
  1184. case -ETIMEDOUT:
  1185. task->tk_action = call_timeout;
  1186. if (task->tk_client->cl_discrtry)
  1187. xprt_conditional_disconnect(task->tk_xprt,
  1188. req->rq_connect_cookie);
  1189. break;
  1190. case -ECONNRESET:
  1191. case -ECONNREFUSED:
  1192. rpc_force_rebind(clnt);
  1193. rpc_delay(task, 3*HZ);
  1194. case -EPIPE:
  1195. case -ENOTCONN:
  1196. task->tk_action = call_bind;
  1197. break;
  1198. case -EAGAIN:
  1199. task->tk_action = call_transmit;
  1200. break;
  1201. case -EIO:
  1202. /* shutdown or soft timeout */
  1203. rpc_exit(task, status);
  1204. break;
  1205. default:
  1206. if (clnt->cl_chatty)
  1207. printk("%s: RPC call returned error %d\n",
  1208. clnt->cl_protname, -status);
  1209. rpc_exit(task, status);
  1210. }
  1211. }
  1212. /*
  1213. * 6a. Handle RPC timeout
  1214. * We do not release the request slot, so we keep using the
  1215. * same XID for all retransmits.
  1216. */
  1217. static void
  1218. call_timeout(struct rpc_task *task)
  1219. {
  1220. struct rpc_clnt *clnt = task->tk_client;
  1221. if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
  1222. dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
  1223. goto retry;
  1224. }
  1225. dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
  1226. task->tk_timeouts++;
  1227. if (RPC_IS_SOFTCONN(task)) {
  1228. rpc_exit(task, -ETIMEDOUT);
  1229. return;
  1230. }
  1231. if (RPC_IS_SOFT(task)) {
  1232. if (clnt->cl_chatty)
  1233. printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
  1234. clnt->cl_protname, clnt->cl_server);
  1235. rpc_exit(task, -EIO);
  1236. return;
  1237. }
  1238. if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
  1239. task->tk_flags |= RPC_CALL_MAJORSEEN;
  1240. if (clnt->cl_chatty)
  1241. printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
  1242. clnt->cl_protname, clnt->cl_server);
  1243. }
  1244. rpc_force_rebind(clnt);
  1245. /*
  1246. * Did our request time out due to an RPCSEC_GSS out-of-sequence
  1247. * event? RFC2203 requires the server to drop all such requests.
  1248. */
  1249. rpcauth_invalcred(task);
  1250. retry:
  1251. clnt->cl_stats->rpcretrans++;
  1252. task->tk_action = call_bind;
  1253. task->tk_status = 0;
  1254. }
  1255. /*
  1256. * 7. Decode the RPC reply
  1257. */
  1258. static void
  1259. call_decode(struct rpc_task *task)
  1260. {
  1261. struct rpc_clnt *clnt = task->tk_client;
  1262. struct rpc_rqst *req = task->tk_rqstp;
  1263. kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
  1264. __be32 *p;
  1265. dprintk("RPC: %5u call_decode (status %d)\n",
  1266. task->tk_pid, task->tk_status);
  1267. if (task->tk_flags & RPC_CALL_MAJORSEEN) {
  1268. if (clnt->cl_chatty)
  1269. printk(KERN_NOTICE "%s: server %s OK\n",
  1270. clnt->cl_protname, clnt->cl_server);
  1271. task->tk_flags &= ~RPC_CALL_MAJORSEEN;
  1272. }
  1273. /*
  1274. * Ensure that we see all writes made by xprt_complete_rqst()
  1275. * before it changed req->rq_reply_bytes_recvd.
  1276. */
  1277. smp_rmb();
  1278. req->rq_rcv_buf.len = req->rq_private_buf.len;
  1279. /* Check that the softirq receive buffer is valid */
  1280. WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
  1281. sizeof(req->rq_rcv_buf)) != 0);
  1282. if (req->rq_rcv_buf.len < 12) {
  1283. if (!RPC_IS_SOFT(task)) {
  1284. task->tk_action = call_bind;
  1285. clnt->cl_stats->rpcretrans++;
  1286. goto out_retry;
  1287. }
  1288. dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
  1289. clnt->cl_protname, task->tk_status);
  1290. task->tk_action = call_timeout;
  1291. goto out_retry;
  1292. }
  1293. p = rpc_verify_header(task);
  1294. if (IS_ERR(p)) {
  1295. if (p == ERR_PTR(-EAGAIN))
  1296. goto out_retry;
  1297. return;
  1298. }
  1299. task->tk_action = rpc_exit_task;
  1300. if (decode) {
  1301. task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
  1302. task->tk_msg.rpc_resp);
  1303. }
  1304. dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
  1305. task->tk_status);
  1306. return;
  1307. out_retry:
  1308. task->tk_status = 0;
  1309. /* Note: rpc_verify_header() may have freed the RPC slot */
  1310. if (task->tk_rqstp == req) {
  1311. req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
  1312. if (task->tk_client->cl_discrtry)
  1313. xprt_conditional_disconnect(task->tk_xprt,
  1314. req->rq_connect_cookie);
  1315. }
  1316. }
  1317. /*
  1318. * 8. Refresh the credentials if rejected by the server
  1319. */
  1320. static void
  1321. call_refresh(struct rpc_task *task)
  1322. {
  1323. dprint_status(task);
  1324. task->tk_action = call_refreshresult;
  1325. task->tk_status = 0;
  1326. task->tk_client->cl_stats->rpcauthrefresh++;
  1327. rpcauth_refreshcred(task);
  1328. }
  1329. /*
  1330. * 8a. Process the results of a credential refresh
  1331. */
  1332. static void
  1333. call_refreshresult(struct rpc_task *task)
  1334. {
  1335. int status = task->tk_status;
  1336. dprint_status(task);
  1337. task->tk_status = 0;
  1338. task->tk_action = call_reserve;
  1339. if (status >= 0 && rpcauth_uptodatecred(task))
  1340. return;
  1341. if (status == -EACCES) {
  1342. rpc_exit(task, -EACCES);
  1343. return;
  1344. }
  1345. task->tk_action = call_refresh;
  1346. if (status != -ETIMEDOUT)
  1347. rpc_delay(task, 3*HZ);
  1348. }
  1349. static __be32 *
  1350. rpc_encode_header(struct rpc_task *task)
  1351. {
  1352. struct rpc_clnt *clnt = task->tk_client;
  1353. struct rpc_rqst *req = task->tk_rqstp;
  1354. __be32 *p = req->rq_svec[0].iov_base;
  1355. /* FIXME: check buffer size? */
  1356. p = xprt_skip_transport_header(task->tk_xprt, p);
  1357. *p++ = req->rq_xid; /* XID */
  1358. *p++ = htonl(RPC_CALL); /* CALL */
  1359. *p++ = htonl(RPC_VERSION); /* RPC version */
  1360. *p++ = htonl(clnt->cl_prog); /* program number */
  1361. *p++ = htonl(clnt->cl_vers); /* program version */
  1362. *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
  1363. p = rpcauth_marshcred(task, p);
  1364. req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
  1365. return p;
  1366. }
  1367. static __be32 *
  1368. rpc_verify_header(struct rpc_task *task)
  1369. {
  1370. struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
  1371. int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
  1372. __be32 *p = iov->iov_base;
  1373. u32 n;
  1374. int error = -EACCES;
  1375. if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
  1376. /* RFC-1014 says that the representation of XDR data must be a
  1377. * multiple of four bytes
  1378. * - if it isn't pointer subtraction in the NFS client may give
  1379. * undefined results
  1380. */
  1381. dprintk("RPC: %5u %s: XDR representation not a multiple of"
  1382. " 4 bytes: 0x%x\n", task->tk_pid, __func__,
  1383. task->tk_rqstp->rq_rcv_buf.len);
  1384. goto out_eio;
  1385. }
  1386. if ((len -= 3) < 0)
  1387. goto out_overflow;
  1388. p += 1; /* skip XID */
  1389. if ((n = ntohl(*p++)) != RPC_REPLY) {
  1390. dprintk("RPC: %5u %s: not an RPC reply: %x\n",
  1391. task->tk_pid, __func__, n);
  1392. goto out_garbage;
  1393. }
  1394. if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
  1395. if (--len < 0)
  1396. goto out_overflow;
  1397. switch ((n = ntohl(*p++))) {
  1398. case RPC_AUTH_ERROR:
  1399. break;
  1400. case RPC_MISMATCH:
  1401. dprintk("RPC: %5u %s: RPC call version "
  1402. "mismatch!\n",
  1403. task->tk_pid, __func__);
  1404. error = -EPROTONOSUPPORT;
  1405. goto out_err;
  1406. default:
  1407. dprintk("RPC: %5u %s: RPC call rejected, "
  1408. "unknown error: %x\n",
  1409. task->tk_pid, __func__, n);
  1410. goto out_eio;
  1411. }
  1412. if (--len < 0)
  1413. goto out_overflow;
  1414. switch ((n = ntohl(*p++))) {
  1415. case RPC_AUTH_REJECTEDCRED:
  1416. case RPC_AUTH_REJECTEDVERF:
  1417. case RPCSEC_GSS_CREDPROBLEM:
  1418. case RPCSEC_GSS_CTXPROBLEM:
  1419. if (!task->tk_cred_retry)
  1420. break;
  1421. task->tk_cred_retry--;
  1422. dprintk("RPC: %5u %s: retry stale creds\n",
  1423. task->tk_pid, __func__);
  1424. rpcauth_invalcred(task);
  1425. /* Ensure we obtain a new XID! */
  1426. xprt_release(task);
  1427. task->tk_action = call_refresh;
  1428. goto out_retry;
  1429. case RPC_AUTH_BADCRED:
  1430. case RPC_AUTH_BADVERF:
  1431. /* possibly garbled cred/verf? */
  1432. if (!task->tk_garb_retry)
  1433. break;
  1434. task->tk_garb_retry--;
  1435. dprintk("RPC: %5u %s: retry garbled creds\n",
  1436. task->tk_pid, __func__);
  1437. task->tk_action = call_bind;
  1438. goto out_retry;
  1439. case RPC_AUTH_TOOWEAK:
  1440. printk(KERN_NOTICE "RPC: server %s requires stronger "
  1441. "authentication.\n", task->tk_client->cl_server);
  1442. break;
  1443. default:
  1444. dprintk("RPC: %5u %s: unknown auth error: %x\n",
  1445. task->tk_pid, __func__, n);
  1446. error = -EIO;
  1447. }
  1448. dprintk("RPC: %5u %s: call rejected %d\n",
  1449. task->tk_pid, __func__, n);
  1450. goto out_err;
  1451. }
  1452. if (!(p = rpcauth_checkverf(task, p))) {
  1453. dprintk("RPC: %5u %s: auth check failed\n",
  1454. task->tk_pid, __func__);
  1455. goto out_garbage; /* bad verifier, retry */
  1456. }
  1457. len = p - (__be32 *)iov->iov_base - 1;
  1458. if (len < 0)
  1459. goto out_overflow;
  1460. switch ((n = ntohl(*p++))) {
  1461. case RPC_SUCCESS:
  1462. return p;
  1463. case RPC_PROG_UNAVAIL:
  1464. dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
  1465. task->tk_pid, __func__,
  1466. (unsigned int)task->tk_client->cl_prog,
  1467. task->tk_client->cl_server);
  1468. error = -EPFNOSUPPORT;
  1469. goto out_err;
  1470. case RPC_PROG_MISMATCH:
  1471. dprintk("RPC: %5u %s: program %u, version %u unsupported by "
  1472. "server %s\n", task->tk_pid, __func__,
  1473. (unsigned int)task->tk_client->cl_prog,
  1474. (unsigned int)task->tk_client->cl_vers,
  1475. task->tk_client->cl_server);
  1476. error = -EPROTONOSUPPORT;
  1477. goto out_err;
  1478. case RPC_PROC_UNAVAIL:
  1479. dprintk("RPC: %5u %s: proc %s unsupported by program %u, "
  1480. "version %u on server %s\n",
  1481. task->tk_pid, __func__,
  1482. rpc_proc_name(task),
  1483. task->tk_client->cl_prog,
  1484. task->tk_client->cl_vers,
  1485. task->tk_client->cl_server);
  1486. error = -EOPNOTSUPP;
  1487. goto out_err;
  1488. case RPC_GARBAGE_ARGS:
  1489. dprintk("RPC: %5u %s: server saw garbage\n",
  1490. task->tk_pid, __func__);
  1491. break; /* retry */
  1492. default:
  1493. dprintk("RPC: %5u %s: server accept status: %x\n",
  1494. task->tk_pid, __func__, n);
  1495. /* Also retry */
  1496. }
  1497. out_garbage:
  1498. task->tk_client->cl_stats->rpcgarbage++;
  1499. if (task->tk_garb_retry) {
  1500. task->tk_garb_retry--;
  1501. dprintk("RPC: %5u %s: retrying\n",
  1502. task->tk_pid, __func__);
  1503. task->tk_action = call_bind;
  1504. out_retry:
  1505. return ERR_PTR(-EAGAIN);
  1506. }
  1507. out_eio:
  1508. error = -EIO;
  1509. out_err:
  1510. rpc_exit(task, error);
  1511. dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
  1512. __func__, error);
  1513. return ERR_PTR(error);
  1514. out_overflow:
  1515. dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
  1516. __func__);
  1517. goto out_garbage;
  1518. }
  1519. static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
  1520. {
  1521. return 0;
  1522. }
  1523. static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
  1524. {
  1525. return 0;
  1526. }
  1527. static struct rpc_procinfo rpcproc_null = {
  1528. .p_encode = rpcproc_encode_null,
  1529. .p_decode = rpcproc_decode_null,
  1530. };
  1531. static int rpc_ping(struct rpc_clnt *clnt)
  1532. {
  1533. struct rpc_message msg = {
  1534. .rpc_proc = &rpcproc_null,
  1535. };
  1536. int err;
  1537. msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
  1538. err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
  1539. put_rpccred(msg.rpc_cred);
  1540. return err;
  1541. }
  1542. struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
  1543. {
  1544. struct rpc_message msg = {
  1545. .rpc_proc = &rpcproc_null,
  1546. .rpc_cred = cred,
  1547. };
  1548. struct rpc_task_setup task_setup_data = {
  1549. .rpc_client = clnt,
  1550. .rpc_message = &msg,
  1551. .callback_ops = &rpc_default_ops,
  1552. .flags = flags,
  1553. };
  1554. return rpc_run_task(&task_setup_data);
  1555. }
  1556. EXPORT_SYMBOL_GPL(rpc_call_null);
  1557. #ifdef RPC_DEBUG
  1558. static void rpc_show_header(void)
  1559. {
  1560. printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
  1561. "-timeout ---ops--\n");
  1562. }
  1563. static void rpc_show_task(const struct rpc_clnt *clnt,
  1564. const struct rpc_task *task)
  1565. {
  1566. const char *rpc_waitq = "none";
  1567. char *p, action[KSYM_SYMBOL_LEN];
  1568. if (RPC_IS_QUEUED(task))
  1569. rpc_waitq = rpc_qname(task->tk_waitqueue);
  1570. /* map tk_action pointer to a function name; then trim off
  1571. * the "+0x0 [sunrpc]" */
  1572. sprint_symbol(action, (unsigned long)task->tk_action);
  1573. p = strchr(action, '+');
  1574. if (p)
  1575. *p = '\0';
  1576. printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%s q:%s\n",
  1577. task->tk_pid, task->tk_flags, task->tk_status,
  1578. clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
  1579. clnt->cl_protname, clnt->cl_vers, rpc_proc_name(task),
  1580. action, rpc_waitq);
  1581. }
  1582. void rpc_show_tasks(void)
  1583. {
  1584. struct rpc_clnt *clnt;
  1585. struct rpc_task *task;
  1586. int header = 0;
  1587. spin_lock(&rpc_client_lock);
  1588. list_for_each_entry(clnt, &all_clients, cl_clients) {
  1589. spin_lock(&clnt->cl_lock);
  1590. list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
  1591. if (!header) {
  1592. rpc_show_header();
  1593. header++;
  1594. }
  1595. rpc_show_task(clnt, task);
  1596. }
  1597. spin_unlock(&clnt->cl_lock);
  1598. }
  1599. spin_unlock(&rpc_client_lock);
  1600. }
  1601. #endif