clnt.c 53 KB

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