clnt.c 55 KB

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