clnt.c 44 KB

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