clnt.c 55 KB

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