clnt.c 42 KB

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