clnt.c 55 KB

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