clnt.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  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. static void
  539. rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
  540. {
  541. if (msg != NULL) {
  542. task->tk_msg.rpc_proc = msg->rpc_proc;
  543. task->tk_msg.rpc_argp = msg->rpc_argp;
  544. task->tk_msg.rpc_resp = msg->rpc_resp;
  545. if (msg->rpc_cred != NULL)
  546. task->tk_msg.rpc_cred = get_rpccred(msg->rpc_cred);
  547. }
  548. }
  549. /*
  550. * Default callback for async RPC calls
  551. */
  552. static void
  553. rpc_default_callback(struct rpc_task *task, void *data)
  554. {
  555. }
  556. static const struct rpc_call_ops rpc_default_ops = {
  557. .rpc_call_done = rpc_default_callback,
  558. };
  559. /**
  560. * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
  561. * @task_setup_data: pointer to task initialisation data
  562. */
  563. struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
  564. {
  565. struct rpc_task *task;
  566. task = rpc_new_task(task_setup_data);
  567. if (IS_ERR(task))
  568. goto out;
  569. rpc_task_set_client(task, task_setup_data->rpc_client);
  570. rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
  571. if (task->tk_status != 0) {
  572. int ret = task->tk_status;
  573. rpc_put_task(task);
  574. return ERR_PTR(ret);
  575. }
  576. if (task->tk_action == NULL)
  577. rpc_call_start(task);
  578. atomic_inc(&task->tk_count);
  579. rpc_execute(task);
  580. out:
  581. return task;
  582. }
  583. EXPORT_SYMBOL_GPL(rpc_run_task);
  584. /**
  585. * rpc_call_sync - Perform a synchronous RPC call
  586. * @clnt: pointer to RPC client
  587. * @msg: RPC call parameters
  588. * @flags: RPC call flags
  589. */
  590. int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
  591. {
  592. struct rpc_task *task;
  593. struct rpc_task_setup task_setup_data = {
  594. .rpc_client = clnt,
  595. .rpc_message = msg,
  596. .callback_ops = &rpc_default_ops,
  597. .flags = flags,
  598. };
  599. int status;
  600. BUG_ON(flags & RPC_TASK_ASYNC);
  601. task = rpc_run_task(&task_setup_data);
  602. if (IS_ERR(task))
  603. return PTR_ERR(task);
  604. status = task->tk_status;
  605. rpc_put_task(task);
  606. return status;
  607. }
  608. EXPORT_SYMBOL_GPL(rpc_call_sync);
  609. /**
  610. * rpc_call_async - Perform an asynchronous RPC call
  611. * @clnt: pointer to RPC client
  612. * @msg: RPC call parameters
  613. * @flags: RPC call flags
  614. * @tk_ops: RPC call ops
  615. * @data: user call data
  616. */
  617. int
  618. rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
  619. const struct rpc_call_ops *tk_ops, void *data)
  620. {
  621. struct rpc_task *task;
  622. struct rpc_task_setup task_setup_data = {
  623. .rpc_client = clnt,
  624. .rpc_message = msg,
  625. .callback_ops = tk_ops,
  626. .callback_data = data,
  627. .flags = flags|RPC_TASK_ASYNC,
  628. };
  629. task = rpc_run_task(&task_setup_data);
  630. if (IS_ERR(task))
  631. return PTR_ERR(task);
  632. rpc_put_task(task);
  633. return 0;
  634. }
  635. EXPORT_SYMBOL_GPL(rpc_call_async);
  636. #if defined(CONFIG_NFS_V4_1)
  637. /**
  638. * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
  639. * rpc_execute against it
  640. * @req: RPC request
  641. * @tk_ops: RPC call ops
  642. */
  643. struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req,
  644. const struct rpc_call_ops *tk_ops)
  645. {
  646. struct rpc_task *task;
  647. struct xdr_buf *xbufp = &req->rq_snd_buf;
  648. struct rpc_task_setup task_setup_data = {
  649. .callback_ops = tk_ops,
  650. };
  651. dprintk("RPC: rpc_run_bc_task req= %p\n", req);
  652. /*
  653. * Create an rpc_task to send the data
  654. */
  655. task = rpc_new_task(&task_setup_data);
  656. if (IS_ERR(task)) {
  657. xprt_free_bc_request(req);
  658. goto out;
  659. }
  660. task->tk_rqstp = req;
  661. /*
  662. * Set up the xdr_buf length.
  663. * This also indicates that the buffer is XDR encoded already.
  664. */
  665. xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
  666. xbufp->tail[0].iov_len;
  667. task->tk_action = call_bc_transmit;
  668. atomic_inc(&task->tk_count);
  669. BUG_ON(atomic_read(&task->tk_count) != 2);
  670. rpc_execute(task);
  671. out:
  672. dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
  673. return task;
  674. }
  675. #endif /* CONFIG_NFS_V4_1 */
  676. void
  677. rpc_call_start(struct rpc_task *task)
  678. {
  679. task->tk_action = call_start;
  680. }
  681. EXPORT_SYMBOL_GPL(rpc_call_start);
  682. /**
  683. * rpc_peeraddr - extract remote peer address from clnt's xprt
  684. * @clnt: RPC client structure
  685. * @buf: target buffer
  686. * @bufsize: length of target buffer
  687. *
  688. * Returns the number of bytes that are actually in the stored address.
  689. */
  690. size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
  691. {
  692. size_t bytes;
  693. struct rpc_xprt *xprt = clnt->cl_xprt;
  694. bytes = sizeof(xprt->addr);
  695. if (bytes > bufsize)
  696. bytes = bufsize;
  697. memcpy(buf, &clnt->cl_xprt->addr, bytes);
  698. return xprt->addrlen;
  699. }
  700. EXPORT_SYMBOL_GPL(rpc_peeraddr);
  701. /**
  702. * rpc_peeraddr2str - return remote peer address in printable format
  703. * @clnt: RPC client structure
  704. * @format: address format
  705. *
  706. */
  707. const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
  708. enum rpc_display_format_t format)
  709. {
  710. struct rpc_xprt *xprt = clnt->cl_xprt;
  711. if (xprt->address_strings[format] != NULL)
  712. return xprt->address_strings[format];
  713. else
  714. return "unprintable";
  715. }
  716. EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
  717. void
  718. rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
  719. {
  720. struct rpc_xprt *xprt = clnt->cl_xprt;
  721. if (xprt->ops->set_buffer_size)
  722. xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
  723. }
  724. EXPORT_SYMBOL_GPL(rpc_setbufsize);
  725. /*
  726. * Return size of largest payload RPC client can support, in bytes
  727. *
  728. * For stream transports, this is one RPC record fragment (see RFC
  729. * 1831), as we don't support multi-record requests yet. For datagram
  730. * transports, this is the size of an IP packet minus the IP, UDP, and
  731. * RPC header sizes.
  732. */
  733. size_t rpc_max_payload(struct rpc_clnt *clnt)
  734. {
  735. return clnt->cl_xprt->max_payload;
  736. }
  737. EXPORT_SYMBOL_GPL(rpc_max_payload);
  738. /**
  739. * rpc_force_rebind - force transport to check that remote port is unchanged
  740. * @clnt: client to rebind
  741. *
  742. */
  743. void rpc_force_rebind(struct rpc_clnt *clnt)
  744. {
  745. if (clnt->cl_autobind)
  746. xprt_clear_bound(clnt->cl_xprt);
  747. }
  748. EXPORT_SYMBOL_GPL(rpc_force_rebind);
  749. /*
  750. * Restart an (async) RPC call from the call_prepare state.
  751. * Usually called from within the exit handler.
  752. */
  753. int
  754. rpc_restart_call_prepare(struct rpc_task *task)
  755. {
  756. if (RPC_ASSASSINATED(task))
  757. return 0;
  758. task->tk_action = rpc_prepare_task;
  759. return 1;
  760. }
  761. EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
  762. /*
  763. * Restart an (async) RPC call. Usually called from within the
  764. * exit handler.
  765. */
  766. int
  767. rpc_restart_call(struct rpc_task *task)
  768. {
  769. if (RPC_ASSASSINATED(task))
  770. return 0;
  771. task->tk_action = call_start;
  772. return 1;
  773. }
  774. EXPORT_SYMBOL_GPL(rpc_restart_call);
  775. #ifdef RPC_DEBUG
  776. static const char *rpc_proc_name(const struct rpc_task *task)
  777. {
  778. const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
  779. if (proc) {
  780. if (proc->p_name)
  781. return proc->p_name;
  782. else
  783. return "NULL";
  784. } else
  785. return "no proc";
  786. }
  787. #endif
  788. /*
  789. * 0. Initial state
  790. *
  791. * Other FSM states can be visited zero or more times, but
  792. * this state is visited exactly once for each RPC.
  793. */
  794. static void
  795. call_start(struct rpc_task *task)
  796. {
  797. struct rpc_clnt *clnt = task->tk_client;
  798. dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
  799. clnt->cl_protname, clnt->cl_vers,
  800. rpc_proc_name(task),
  801. (RPC_IS_ASYNC(task) ? "async" : "sync"));
  802. /* Increment call count */
  803. task->tk_msg.rpc_proc->p_count++;
  804. clnt->cl_stats->rpccnt++;
  805. task->tk_action = call_reserve;
  806. }
  807. /*
  808. * 1. Reserve an RPC call slot
  809. */
  810. static void
  811. call_reserve(struct rpc_task *task)
  812. {
  813. dprint_status(task);
  814. task->tk_status = 0;
  815. task->tk_action = call_reserveresult;
  816. xprt_reserve(task);
  817. }
  818. /*
  819. * 1b. Grok the result of xprt_reserve()
  820. */
  821. static void
  822. call_reserveresult(struct rpc_task *task)
  823. {
  824. int status = task->tk_status;
  825. dprint_status(task);
  826. /*
  827. * After a call to xprt_reserve(), we must have either
  828. * a request slot or else an error status.
  829. */
  830. task->tk_status = 0;
  831. if (status >= 0) {
  832. if (task->tk_rqstp) {
  833. task->tk_action = call_refresh;
  834. return;
  835. }
  836. printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
  837. __func__, status);
  838. rpc_exit(task, -EIO);
  839. return;
  840. }
  841. /*
  842. * Even though there was an error, we may have acquired
  843. * a request slot somehow. Make sure not to leak it.
  844. */
  845. if (task->tk_rqstp) {
  846. printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
  847. __func__, status);
  848. xprt_release(task);
  849. }
  850. switch (status) {
  851. case -EAGAIN: /* woken up; retry */
  852. task->tk_action = call_reserve;
  853. return;
  854. case -EIO: /* probably a shutdown */
  855. break;
  856. default:
  857. printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
  858. __func__, status);
  859. break;
  860. }
  861. rpc_exit(task, status);
  862. }
  863. /*
  864. * 2. Bind and/or refresh the credentials
  865. */
  866. static void
  867. call_refresh(struct rpc_task *task)
  868. {
  869. dprint_status(task);
  870. task->tk_action = call_refreshresult;
  871. task->tk_status = 0;
  872. task->tk_client->cl_stats->rpcauthrefresh++;
  873. rpcauth_refreshcred(task);
  874. }
  875. /*
  876. * 2a. Process the results of a credential refresh
  877. */
  878. static void
  879. call_refreshresult(struct rpc_task *task)
  880. {
  881. int status = task->tk_status;
  882. dprint_status(task);
  883. task->tk_status = 0;
  884. task->tk_action = call_refresh;
  885. switch (status) {
  886. case 0:
  887. if (rpcauth_uptodatecred(task))
  888. task->tk_action = call_allocate;
  889. return;
  890. case -ETIMEDOUT:
  891. rpc_delay(task, 3*HZ);
  892. case -EAGAIN:
  893. status = -EACCES;
  894. if (!task->tk_cred_retry)
  895. break;
  896. task->tk_cred_retry--;
  897. dprintk("RPC: %5u %s: retry refresh creds\n",
  898. task->tk_pid, __func__);
  899. return;
  900. }
  901. dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
  902. task->tk_pid, __func__, status);
  903. rpc_exit(task, status);
  904. }
  905. /*
  906. * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
  907. * (Note: buffer memory is freed in xprt_release).
  908. */
  909. static void
  910. call_allocate(struct rpc_task *task)
  911. {
  912. unsigned int slack = task->tk_rqstp->rq_cred->cr_auth->au_cslack;
  913. struct rpc_rqst *req = task->tk_rqstp;
  914. struct rpc_xprt *xprt = task->tk_xprt;
  915. struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
  916. dprint_status(task);
  917. task->tk_status = 0;
  918. task->tk_action = call_bind;
  919. if (req->rq_buffer)
  920. return;
  921. if (proc->p_proc != 0) {
  922. BUG_ON(proc->p_arglen == 0);
  923. if (proc->p_decode != NULL)
  924. BUG_ON(proc->p_replen == 0);
  925. }
  926. /*
  927. * Calculate the size (in quads) of the RPC call
  928. * and reply headers, and convert both values
  929. * to byte sizes.
  930. */
  931. req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
  932. req->rq_callsize <<= 2;
  933. req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
  934. req->rq_rcvsize <<= 2;
  935. req->rq_buffer = xprt->ops->buf_alloc(task,
  936. req->rq_callsize + req->rq_rcvsize);
  937. if (req->rq_buffer != NULL)
  938. return;
  939. dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
  940. if (RPC_IS_ASYNC(task) || !signalled()) {
  941. task->tk_action = call_allocate;
  942. rpc_delay(task, HZ>>4);
  943. return;
  944. }
  945. rpc_exit(task, -ERESTARTSYS);
  946. }
  947. static inline int
  948. rpc_task_need_encode(struct rpc_task *task)
  949. {
  950. return task->tk_rqstp->rq_snd_buf.len == 0;
  951. }
  952. static inline void
  953. rpc_task_force_reencode(struct rpc_task *task)
  954. {
  955. task->tk_rqstp->rq_snd_buf.len = 0;
  956. task->tk_rqstp->rq_bytes_sent = 0;
  957. }
  958. static inline void
  959. rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
  960. {
  961. buf->head[0].iov_base = start;
  962. buf->head[0].iov_len = len;
  963. buf->tail[0].iov_len = 0;
  964. buf->page_len = 0;
  965. buf->flags = 0;
  966. buf->len = 0;
  967. buf->buflen = len;
  968. }
  969. /*
  970. * 3. Encode arguments of an RPC call
  971. */
  972. static void
  973. rpc_xdr_encode(struct rpc_task *task)
  974. {
  975. struct rpc_rqst *req = task->tk_rqstp;
  976. kxdreproc_t encode;
  977. __be32 *p;
  978. dprint_status(task);
  979. rpc_xdr_buf_init(&req->rq_snd_buf,
  980. req->rq_buffer,
  981. req->rq_callsize);
  982. rpc_xdr_buf_init(&req->rq_rcv_buf,
  983. (char *)req->rq_buffer + req->rq_callsize,
  984. req->rq_rcvsize);
  985. p = rpc_encode_header(task);
  986. if (p == NULL) {
  987. printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
  988. rpc_exit(task, -EIO);
  989. return;
  990. }
  991. encode = task->tk_msg.rpc_proc->p_encode;
  992. if (encode == NULL)
  993. return;
  994. task->tk_status = rpcauth_wrap_req(task, encode, req, p,
  995. task->tk_msg.rpc_argp);
  996. }
  997. /*
  998. * 4. Get the server port number if not yet set
  999. */
  1000. static void
  1001. call_bind(struct rpc_task *task)
  1002. {
  1003. struct rpc_xprt *xprt = task->tk_xprt;
  1004. dprint_status(task);
  1005. task->tk_action = call_connect;
  1006. if (!xprt_bound(xprt)) {
  1007. task->tk_action = call_bind_status;
  1008. task->tk_timeout = xprt->bind_timeout;
  1009. xprt->ops->rpcbind(task);
  1010. }
  1011. }
  1012. /*
  1013. * 4a. Sort out bind result
  1014. */
  1015. static void
  1016. call_bind_status(struct rpc_task *task)
  1017. {
  1018. int status = -EIO;
  1019. if (task->tk_status >= 0) {
  1020. dprint_status(task);
  1021. task->tk_status = 0;
  1022. task->tk_action = call_connect;
  1023. return;
  1024. }
  1025. switch (task->tk_status) {
  1026. case -ENOMEM:
  1027. dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
  1028. rpc_delay(task, HZ >> 2);
  1029. goto retry_timeout;
  1030. case -EACCES:
  1031. dprintk("RPC: %5u remote rpcbind: RPC program/version "
  1032. "unavailable\n", task->tk_pid);
  1033. /* fail immediately if this is an RPC ping */
  1034. if (task->tk_msg.rpc_proc->p_proc == 0) {
  1035. status = -EOPNOTSUPP;
  1036. break;
  1037. }
  1038. rpc_delay(task, 3*HZ);
  1039. goto retry_timeout;
  1040. case -ETIMEDOUT:
  1041. dprintk("RPC: %5u rpcbind request timed out\n",
  1042. task->tk_pid);
  1043. goto retry_timeout;
  1044. case -EPFNOSUPPORT:
  1045. /* server doesn't support any rpcbind version we know of */
  1046. dprintk("RPC: %5u unrecognized remote rpcbind service\n",
  1047. task->tk_pid);
  1048. break;
  1049. case -EPROTONOSUPPORT:
  1050. dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
  1051. task->tk_pid);
  1052. task->tk_status = 0;
  1053. task->tk_action = call_bind;
  1054. return;
  1055. case -ECONNREFUSED: /* connection problems */
  1056. case -ECONNRESET:
  1057. case -ENOTCONN:
  1058. case -EHOSTDOWN:
  1059. case -EHOSTUNREACH:
  1060. case -ENETUNREACH:
  1061. case -EPIPE:
  1062. dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
  1063. task->tk_pid, task->tk_status);
  1064. if (!RPC_IS_SOFTCONN(task)) {
  1065. rpc_delay(task, 5*HZ);
  1066. goto retry_timeout;
  1067. }
  1068. status = task->tk_status;
  1069. break;
  1070. default:
  1071. dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
  1072. task->tk_pid, -task->tk_status);
  1073. }
  1074. rpc_exit(task, status);
  1075. return;
  1076. retry_timeout:
  1077. task->tk_action = call_timeout;
  1078. }
  1079. /*
  1080. * 4b. Connect to the RPC server
  1081. */
  1082. static void
  1083. call_connect(struct rpc_task *task)
  1084. {
  1085. struct rpc_xprt *xprt = task->tk_xprt;
  1086. dprintk("RPC: %5u call_connect xprt %p %s connected\n",
  1087. task->tk_pid, xprt,
  1088. (xprt_connected(xprt) ? "is" : "is not"));
  1089. task->tk_action = call_transmit;
  1090. if (!xprt_connected(xprt)) {
  1091. task->tk_action = call_connect_status;
  1092. if (task->tk_status < 0)
  1093. return;
  1094. xprt_connect(task);
  1095. }
  1096. }
  1097. /*
  1098. * 4c. Sort out connect result
  1099. */
  1100. static void
  1101. call_connect_status(struct rpc_task *task)
  1102. {
  1103. struct rpc_clnt *clnt = task->tk_client;
  1104. int status = task->tk_status;
  1105. dprint_status(task);
  1106. task->tk_status = 0;
  1107. if (status >= 0 || status == -EAGAIN) {
  1108. clnt->cl_stats->netreconn++;
  1109. task->tk_action = call_transmit;
  1110. return;
  1111. }
  1112. switch (status) {
  1113. /* if soft mounted, test if we've timed out */
  1114. case -ETIMEDOUT:
  1115. task->tk_action = call_timeout;
  1116. break;
  1117. default:
  1118. rpc_exit(task, -EIO);
  1119. }
  1120. }
  1121. /*
  1122. * 5. Transmit the RPC request, and wait for reply
  1123. */
  1124. static void
  1125. call_transmit(struct rpc_task *task)
  1126. {
  1127. dprint_status(task);
  1128. task->tk_action = call_status;
  1129. if (task->tk_status < 0)
  1130. return;
  1131. task->tk_status = xprt_prepare_transmit(task);
  1132. if (task->tk_status != 0)
  1133. return;
  1134. task->tk_action = call_transmit_status;
  1135. /* Encode here so that rpcsec_gss can use correct sequence number. */
  1136. if (rpc_task_need_encode(task)) {
  1137. BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
  1138. rpc_xdr_encode(task);
  1139. /* Did the encode result in an error condition? */
  1140. if (task->tk_status != 0) {
  1141. /* Was the error nonfatal? */
  1142. if (task->tk_status == -EAGAIN)
  1143. rpc_delay(task, HZ >> 4);
  1144. else
  1145. rpc_exit(task, task->tk_status);
  1146. return;
  1147. }
  1148. }
  1149. xprt_transmit(task);
  1150. if (task->tk_status < 0)
  1151. return;
  1152. /*
  1153. * On success, ensure that we call xprt_end_transmit() before sleeping
  1154. * in order to allow access to the socket to other RPC requests.
  1155. */
  1156. call_transmit_status(task);
  1157. if (rpc_reply_expected(task))
  1158. return;
  1159. task->tk_action = rpc_exit_task;
  1160. rpc_wake_up_queued_task(&task->tk_xprt->pending, task);
  1161. }
  1162. /*
  1163. * 5a. Handle cleanup after a transmission
  1164. */
  1165. static void
  1166. call_transmit_status(struct rpc_task *task)
  1167. {
  1168. task->tk_action = call_status;
  1169. /*
  1170. * Common case: success. Force the compiler to put this
  1171. * test first.
  1172. */
  1173. if (task->tk_status == 0) {
  1174. xprt_end_transmit(task);
  1175. rpc_task_force_reencode(task);
  1176. return;
  1177. }
  1178. switch (task->tk_status) {
  1179. case -EAGAIN:
  1180. break;
  1181. default:
  1182. dprint_status(task);
  1183. xprt_end_transmit(task);
  1184. rpc_task_force_reencode(task);
  1185. break;
  1186. /*
  1187. * Special cases: if we've been waiting on the
  1188. * socket's write_space() callback, or if the
  1189. * socket just returned a connection error,
  1190. * then hold onto the transport lock.
  1191. */
  1192. case -ECONNREFUSED:
  1193. case -EHOSTDOWN:
  1194. case -EHOSTUNREACH:
  1195. case -ENETUNREACH:
  1196. if (RPC_IS_SOFTCONN(task)) {
  1197. xprt_end_transmit(task);
  1198. rpc_exit(task, task->tk_status);
  1199. break;
  1200. }
  1201. case -ECONNRESET:
  1202. case -ENOTCONN:
  1203. case -EPIPE:
  1204. rpc_task_force_reencode(task);
  1205. }
  1206. }
  1207. #if defined(CONFIG_NFS_V4_1)
  1208. /*
  1209. * 5b. Send the backchannel RPC reply. On error, drop the reply. In
  1210. * addition, disconnect on connectivity errors.
  1211. */
  1212. static void
  1213. call_bc_transmit(struct rpc_task *task)
  1214. {
  1215. struct rpc_rqst *req = task->tk_rqstp;
  1216. BUG_ON(task->tk_status != 0);
  1217. task->tk_status = xprt_prepare_transmit(task);
  1218. if (task->tk_status == -EAGAIN) {
  1219. /*
  1220. * Could not reserve the transport. Try again after the
  1221. * transport is released.
  1222. */
  1223. task->tk_status = 0;
  1224. task->tk_action = call_bc_transmit;
  1225. return;
  1226. }
  1227. task->tk_action = rpc_exit_task;
  1228. if (task->tk_status < 0) {
  1229. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1230. "error: %d\n", task->tk_status);
  1231. return;
  1232. }
  1233. xprt_transmit(task);
  1234. xprt_end_transmit(task);
  1235. dprint_status(task);
  1236. switch (task->tk_status) {
  1237. case 0:
  1238. /* Success */
  1239. break;
  1240. case -EHOSTDOWN:
  1241. case -EHOSTUNREACH:
  1242. case -ENETUNREACH:
  1243. case -ETIMEDOUT:
  1244. /*
  1245. * Problem reaching the server. Disconnect and let the
  1246. * forechannel reestablish the connection. The server will
  1247. * have to retransmit the backchannel request and we'll
  1248. * reprocess it. Since these ops are idempotent, there's no
  1249. * need to cache our reply at this time.
  1250. */
  1251. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1252. "error: %d\n", task->tk_status);
  1253. xprt_conditional_disconnect(task->tk_xprt,
  1254. req->rq_connect_cookie);
  1255. break;
  1256. default:
  1257. /*
  1258. * We were unable to reply and will have to drop the
  1259. * request. The server should reconnect and retransmit.
  1260. */
  1261. BUG_ON(task->tk_status == -EAGAIN);
  1262. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1263. "error: %d\n", task->tk_status);
  1264. break;
  1265. }
  1266. rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
  1267. }
  1268. #endif /* CONFIG_NFS_V4_1 */
  1269. /*
  1270. * 6. Sort out the RPC call status
  1271. */
  1272. static void
  1273. call_status(struct rpc_task *task)
  1274. {
  1275. struct rpc_clnt *clnt = task->tk_client;
  1276. struct rpc_rqst *req = task->tk_rqstp;
  1277. int status;
  1278. if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
  1279. task->tk_status = req->rq_reply_bytes_recvd;
  1280. dprint_status(task);
  1281. status = task->tk_status;
  1282. if (status >= 0) {
  1283. task->tk_action = call_decode;
  1284. return;
  1285. }
  1286. task->tk_status = 0;
  1287. switch(status) {
  1288. case -EHOSTDOWN:
  1289. case -EHOSTUNREACH:
  1290. case -ENETUNREACH:
  1291. /*
  1292. * Delay any retries for 3 seconds, then handle as if it
  1293. * were a timeout.
  1294. */
  1295. rpc_delay(task, 3*HZ);
  1296. case -ETIMEDOUT:
  1297. task->tk_action = call_timeout;
  1298. if (task->tk_client->cl_discrtry)
  1299. xprt_conditional_disconnect(task->tk_xprt,
  1300. req->rq_connect_cookie);
  1301. break;
  1302. case -ECONNRESET:
  1303. case -ECONNREFUSED:
  1304. rpc_force_rebind(clnt);
  1305. rpc_delay(task, 3*HZ);
  1306. case -EPIPE:
  1307. case -ENOTCONN:
  1308. task->tk_action = call_bind;
  1309. break;
  1310. case -EAGAIN:
  1311. task->tk_action = call_transmit;
  1312. break;
  1313. case -EIO:
  1314. /* shutdown or soft timeout */
  1315. rpc_exit(task, status);
  1316. break;
  1317. default:
  1318. if (clnt->cl_chatty)
  1319. printk("%s: RPC call returned error %d\n",
  1320. clnt->cl_protname, -status);
  1321. rpc_exit(task, status);
  1322. }
  1323. }
  1324. /*
  1325. * 6a. Handle RPC timeout
  1326. * We do not release the request slot, so we keep using the
  1327. * same XID for all retransmits.
  1328. */
  1329. static void
  1330. call_timeout(struct rpc_task *task)
  1331. {
  1332. struct rpc_clnt *clnt = task->tk_client;
  1333. if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
  1334. dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
  1335. goto retry;
  1336. }
  1337. dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
  1338. task->tk_timeouts++;
  1339. if (RPC_IS_SOFTCONN(task)) {
  1340. rpc_exit(task, -ETIMEDOUT);
  1341. return;
  1342. }
  1343. if (RPC_IS_SOFT(task)) {
  1344. if (clnt->cl_chatty)
  1345. printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
  1346. clnt->cl_protname, clnt->cl_server);
  1347. rpc_exit(task, -EIO);
  1348. return;
  1349. }
  1350. if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
  1351. task->tk_flags |= RPC_CALL_MAJORSEEN;
  1352. if (clnt->cl_chatty)
  1353. printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
  1354. clnt->cl_protname, clnt->cl_server);
  1355. }
  1356. rpc_force_rebind(clnt);
  1357. /*
  1358. * Did our request time out due to an RPCSEC_GSS out-of-sequence
  1359. * event? RFC2203 requires the server to drop all such requests.
  1360. */
  1361. rpcauth_invalcred(task);
  1362. retry:
  1363. clnt->cl_stats->rpcretrans++;
  1364. task->tk_action = call_bind;
  1365. task->tk_status = 0;
  1366. }
  1367. /*
  1368. * 7. Decode the RPC reply
  1369. */
  1370. static void
  1371. call_decode(struct rpc_task *task)
  1372. {
  1373. struct rpc_clnt *clnt = task->tk_client;
  1374. struct rpc_rqst *req = task->tk_rqstp;
  1375. kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
  1376. __be32 *p;
  1377. dprintk("RPC: %5u call_decode (status %d)\n",
  1378. task->tk_pid, task->tk_status);
  1379. if (task->tk_flags & RPC_CALL_MAJORSEEN) {
  1380. if (clnt->cl_chatty)
  1381. printk(KERN_NOTICE "%s: server %s OK\n",
  1382. clnt->cl_protname, clnt->cl_server);
  1383. task->tk_flags &= ~RPC_CALL_MAJORSEEN;
  1384. }
  1385. /*
  1386. * Ensure that we see all writes made by xprt_complete_rqst()
  1387. * before it changed req->rq_reply_bytes_recvd.
  1388. */
  1389. smp_rmb();
  1390. req->rq_rcv_buf.len = req->rq_private_buf.len;
  1391. /* Check that the softirq receive buffer is valid */
  1392. WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
  1393. sizeof(req->rq_rcv_buf)) != 0);
  1394. if (req->rq_rcv_buf.len < 12) {
  1395. if (!RPC_IS_SOFT(task)) {
  1396. task->tk_action = call_bind;
  1397. clnt->cl_stats->rpcretrans++;
  1398. goto out_retry;
  1399. }
  1400. dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
  1401. clnt->cl_protname, task->tk_status);
  1402. task->tk_action = call_timeout;
  1403. goto out_retry;
  1404. }
  1405. p = rpc_verify_header(task);
  1406. if (IS_ERR(p)) {
  1407. if (p == ERR_PTR(-EAGAIN))
  1408. goto out_retry;
  1409. return;
  1410. }
  1411. task->tk_action = rpc_exit_task;
  1412. if (decode) {
  1413. task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
  1414. task->tk_msg.rpc_resp);
  1415. }
  1416. dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
  1417. task->tk_status);
  1418. return;
  1419. out_retry:
  1420. task->tk_status = 0;
  1421. /* Note: rpc_verify_header() may have freed the RPC slot */
  1422. if (task->tk_rqstp == req) {
  1423. req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
  1424. if (task->tk_client->cl_discrtry)
  1425. xprt_conditional_disconnect(task->tk_xprt,
  1426. req->rq_connect_cookie);
  1427. }
  1428. }
  1429. static __be32 *
  1430. rpc_encode_header(struct rpc_task *task)
  1431. {
  1432. struct rpc_clnt *clnt = task->tk_client;
  1433. struct rpc_rqst *req = task->tk_rqstp;
  1434. __be32 *p = req->rq_svec[0].iov_base;
  1435. /* FIXME: check buffer size? */
  1436. p = xprt_skip_transport_header(task->tk_xprt, p);
  1437. *p++ = req->rq_xid; /* XID */
  1438. *p++ = htonl(RPC_CALL); /* CALL */
  1439. *p++ = htonl(RPC_VERSION); /* RPC version */
  1440. *p++ = htonl(clnt->cl_prog); /* program number */
  1441. *p++ = htonl(clnt->cl_vers); /* program version */
  1442. *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
  1443. p = rpcauth_marshcred(task, p);
  1444. req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
  1445. return p;
  1446. }
  1447. static __be32 *
  1448. rpc_verify_header(struct rpc_task *task)
  1449. {
  1450. struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
  1451. int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
  1452. __be32 *p = iov->iov_base;
  1453. u32 n;
  1454. int error = -EACCES;
  1455. if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
  1456. /* RFC-1014 says that the representation of XDR data must be a
  1457. * multiple of four bytes
  1458. * - if it isn't pointer subtraction in the NFS client may give
  1459. * undefined results
  1460. */
  1461. dprintk("RPC: %5u %s: XDR representation not a multiple of"
  1462. " 4 bytes: 0x%x\n", task->tk_pid, __func__,
  1463. task->tk_rqstp->rq_rcv_buf.len);
  1464. goto out_eio;
  1465. }
  1466. if ((len -= 3) < 0)
  1467. goto out_overflow;
  1468. p += 1; /* skip XID */
  1469. if ((n = ntohl(*p++)) != RPC_REPLY) {
  1470. dprintk("RPC: %5u %s: not an RPC reply: %x\n",
  1471. task->tk_pid, __func__, n);
  1472. goto out_garbage;
  1473. }
  1474. if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
  1475. if (--len < 0)
  1476. goto out_overflow;
  1477. switch ((n = ntohl(*p++))) {
  1478. case RPC_AUTH_ERROR:
  1479. break;
  1480. case RPC_MISMATCH:
  1481. dprintk("RPC: %5u %s: RPC call version "
  1482. "mismatch!\n",
  1483. task->tk_pid, __func__);
  1484. error = -EPROTONOSUPPORT;
  1485. goto out_err;
  1486. default:
  1487. dprintk("RPC: %5u %s: RPC call rejected, "
  1488. "unknown error: %x\n",
  1489. task->tk_pid, __func__, n);
  1490. goto out_eio;
  1491. }
  1492. if (--len < 0)
  1493. goto out_overflow;
  1494. switch ((n = ntohl(*p++))) {
  1495. case RPC_AUTH_REJECTEDCRED:
  1496. case RPC_AUTH_REJECTEDVERF:
  1497. case RPCSEC_GSS_CREDPROBLEM:
  1498. case RPCSEC_GSS_CTXPROBLEM:
  1499. if (!task->tk_cred_retry)
  1500. break;
  1501. task->tk_cred_retry--;
  1502. dprintk("RPC: %5u %s: retry stale creds\n",
  1503. task->tk_pid, __func__);
  1504. rpcauth_invalcred(task);
  1505. /* Ensure we obtain a new XID! */
  1506. xprt_release(task);
  1507. task->tk_action = call_reserve;
  1508. goto out_retry;
  1509. case RPC_AUTH_BADCRED:
  1510. case RPC_AUTH_BADVERF:
  1511. /* possibly garbled cred/verf? */
  1512. if (!task->tk_garb_retry)
  1513. break;
  1514. task->tk_garb_retry--;
  1515. dprintk("RPC: %5u %s: retry garbled creds\n",
  1516. task->tk_pid, __func__);
  1517. task->tk_action = call_bind;
  1518. goto out_retry;
  1519. case RPC_AUTH_TOOWEAK:
  1520. printk(KERN_NOTICE "RPC: server %s requires stronger "
  1521. "authentication.\n", task->tk_client->cl_server);
  1522. break;
  1523. default:
  1524. dprintk("RPC: %5u %s: unknown auth error: %x\n",
  1525. task->tk_pid, __func__, n);
  1526. error = -EIO;
  1527. }
  1528. dprintk("RPC: %5u %s: call rejected %d\n",
  1529. task->tk_pid, __func__, n);
  1530. goto out_err;
  1531. }
  1532. if (!(p = rpcauth_checkverf(task, p))) {
  1533. dprintk("RPC: %5u %s: auth check failed\n",
  1534. task->tk_pid, __func__);
  1535. goto out_garbage; /* bad verifier, retry */
  1536. }
  1537. len = p - (__be32 *)iov->iov_base - 1;
  1538. if (len < 0)
  1539. goto out_overflow;
  1540. switch ((n = ntohl(*p++))) {
  1541. case RPC_SUCCESS:
  1542. return p;
  1543. case RPC_PROG_UNAVAIL:
  1544. dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
  1545. task->tk_pid, __func__,
  1546. (unsigned int)task->tk_client->cl_prog,
  1547. task->tk_client->cl_server);
  1548. error = -EPFNOSUPPORT;
  1549. goto out_err;
  1550. case RPC_PROG_MISMATCH:
  1551. dprintk("RPC: %5u %s: program %u, version %u unsupported by "
  1552. "server %s\n", task->tk_pid, __func__,
  1553. (unsigned int)task->tk_client->cl_prog,
  1554. (unsigned int)task->tk_client->cl_vers,
  1555. task->tk_client->cl_server);
  1556. error = -EPROTONOSUPPORT;
  1557. goto out_err;
  1558. case RPC_PROC_UNAVAIL:
  1559. dprintk("RPC: %5u %s: proc %s unsupported by program %u, "
  1560. "version %u on server %s\n",
  1561. task->tk_pid, __func__,
  1562. rpc_proc_name(task),
  1563. task->tk_client->cl_prog,
  1564. task->tk_client->cl_vers,
  1565. task->tk_client->cl_server);
  1566. error = -EOPNOTSUPP;
  1567. goto out_err;
  1568. case RPC_GARBAGE_ARGS:
  1569. dprintk("RPC: %5u %s: server saw garbage\n",
  1570. task->tk_pid, __func__);
  1571. break; /* retry */
  1572. default:
  1573. dprintk("RPC: %5u %s: server accept status: %x\n",
  1574. task->tk_pid, __func__, n);
  1575. /* Also retry */
  1576. }
  1577. out_garbage:
  1578. task->tk_client->cl_stats->rpcgarbage++;
  1579. if (task->tk_garb_retry) {
  1580. task->tk_garb_retry--;
  1581. dprintk("RPC: %5u %s: retrying\n",
  1582. task->tk_pid, __func__);
  1583. task->tk_action = call_bind;
  1584. out_retry:
  1585. return ERR_PTR(-EAGAIN);
  1586. }
  1587. out_eio:
  1588. error = -EIO;
  1589. out_err:
  1590. rpc_exit(task, error);
  1591. dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
  1592. __func__, error);
  1593. return ERR_PTR(error);
  1594. out_overflow:
  1595. dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
  1596. __func__);
  1597. goto out_garbage;
  1598. }
  1599. static void rpcproc_encode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
  1600. {
  1601. }
  1602. static int rpcproc_decode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
  1603. {
  1604. return 0;
  1605. }
  1606. static struct rpc_procinfo rpcproc_null = {
  1607. .p_encode = rpcproc_encode_null,
  1608. .p_decode = rpcproc_decode_null,
  1609. };
  1610. static int rpc_ping(struct rpc_clnt *clnt)
  1611. {
  1612. struct rpc_message msg = {
  1613. .rpc_proc = &rpcproc_null,
  1614. };
  1615. int err;
  1616. msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
  1617. err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
  1618. put_rpccred(msg.rpc_cred);
  1619. return err;
  1620. }
  1621. struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
  1622. {
  1623. struct rpc_message msg = {
  1624. .rpc_proc = &rpcproc_null,
  1625. .rpc_cred = cred,
  1626. };
  1627. struct rpc_task_setup task_setup_data = {
  1628. .rpc_client = clnt,
  1629. .rpc_message = &msg,
  1630. .callback_ops = &rpc_default_ops,
  1631. .flags = flags,
  1632. };
  1633. return rpc_run_task(&task_setup_data);
  1634. }
  1635. EXPORT_SYMBOL_GPL(rpc_call_null);
  1636. #ifdef RPC_DEBUG
  1637. static void rpc_show_header(void)
  1638. {
  1639. printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
  1640. "-timeout ---ops--\n");
  1641. }
  1642. static void rpc_show_task(const struct rpc_clnt *clnt,
  1643. const struct rpc_task *task)
  1644. {
  1645. const char *rpc_waitq = "none";
  1646. if (RPC_IS_QUEUED(task))
  1647. rpc_waitq = rpc_qname(task->tk_waitqueue);
  1648. printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
  1649. task->tk_pid, task->tk_flags, task->tk_status,
  1650. clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
  1651. clnt->cl_protname, clnt->cl_vers, rpc_proc_name(task),
  1652. task->tk_action, rpc_waitq);
  1653. }
  1654. void rpc_show_tasks(void)
  1655. {
  1656. struct rpc_clnt *clnt;
  1657. struct rpc_task *task;
  1658. int header = 0;
  1659. spin_lock(&rpc_client_lock);
  1660. list_for_each_entry(clnt, &all_clients, cl_clients) {
  1661. spin_lock(&clnt->cl_lock);
  1662. list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
  1663. if (!header) {
  1664. rpc_show_header();
  1665. header++;
  1666. }
  1667. rpc_show_task(clnt, task);
  1668. }
  1669. spin_unlock(&clnt->cl_lock);
  1670. }
  1671. spin_unlock(&rpc_client_lock);
  1672. }
  1673. #endif