clnt.c 53 KB

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