clnt.c 58 KB

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