clnt.c 59 KB

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