clnt.c 44 KB

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