clnt.c 55 KB

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