clnt.c 52 KB

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