clnt.c 44 KB

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