clnt.c 54 KB

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