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