clnt.c 44 KB

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