clnt.c 55 KB

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