clnt.c 58 KB

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