clnt.c 54 KB

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