clnt.c 59 KB

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