clnt.c 55 KB

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