clnt.c 54 KB

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