clnt.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890
  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/rcupdate.h>
  27. #include <linux/utsname.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/in.h>
  30. #include <linux/in6.h>
  31. #include <linux/un.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. #if IS_ENABLED(CONFIG_SUNRPC_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. rpc_remove_client_dir(clnt);
  90. }
  91. static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
  92. {
  93. struct net *net = rpc_net_ns(clnt);
  94. struct super_block *pipefs_sb;
  95. pipefs_sb = rpc_get_sb_net(net);
  96. if (pipefs_sb) {
  97. __rpc_clnt_remove_pipedir(clnt);
  98. rpc_put_sb_net(net);
  99. }
  100. }
  101. static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
  102. struct rpc_clnt *clnt)
  103. {
  104. static uint32_t clntid;
  105. const char *dir_name = clnt->cl_program->pipe_dir_name;
  106. char name[15];
  107. struct dentry *dir, *dentry;
  108. dir = rpc_d_lookup_sb(sb, dir_name);
  109. if (dir == NULL) {
  110. pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name);
  111. return dir;
  112. }
  113. for (;;) {
  114. snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
  115. name[sizeof(name) - 1] = '\0';
  116. dentry = rpc_create_client_dir(dir, name, clnt);
  117. if (!IS_ERR(dentry))
  118. break;
  119. if (dentry == ERR_PTR(-EEXIST))
  120. continue;
  121. printk(KERN_INFO "RPC: Couldn't create pipefs entry"
  122. " %s/%s, error %ld\n",
  123. dir_name, name, PTR_ERR(dentry));
  124. break;
  125. }
  126. dput(dir);
  127. return dentry;
  128. }
  129. static int
  130. rpc_setup_pipedir(struct super_block *pipefs_sb, struct rpc_clnt *clnt)
  131. {
  132. struct dentry *dentry;
  133. if (clnt->cl_program->pipe_dir_name != NULL) {
  134. dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt);
  135. if (IS_ERR(dentry))
  136. return PTR_ERR(dentry);
  137. }
  138. return 0;
  139. }
  140. static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
  141. {
  142. if (clnt->cl_program->pipe_dir_name == NULL)
  143. return 1;
  144. switch (event) {
  145. case RPC_PIPEFS_MOUNT:
  146. if (clnt->cl_pipedir_objects.pdh_dentry != NULL)
  147. return 1;
  148. if (atomic_read(&clnt->cl_count) == 0)
  149. return 1;
  150. break;
  151. case RPC_PIPEFS_UMOUNT:
  152. if (clnt->cl_pipedir_objects.pdh_dentry == NULL)
  153. return 1;
  154. break;
  155. }
  156. return 0;
  157. }
  158. static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
  159. struct super_block *sb)
  160. {
  161. struct dentry *dentry;
  162. switch (event) {
  163. case RPC_PIPEFS_MOUNT:
  164. dentry = rpc_setup_pipedir_sb(sb, clnt);
  165. if (!dentry)
  166. return -ENOENT;
  167. if (IS_ERR(dentry))
  168. return PTR_ERR(dentry);
  169. break;
  170. case RPC_PIPEFS_UMOUNT:
  171. __rpc_clnt_remove_pipedir(clnt);
  172. break;
  173. default:
  174. printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
  175. return -ENOTSUPP;
  176. }
  177. return 0;
  178. }
  179. static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
  180. struct super_block *sb)
  181. {
  182. int error = 0;
  183. for (;; clnt = clnt->cl_parent) {
  184. if (!rpc_clnt_skip_event(clnt, event))
  185. error = __rpc_clnt_handle_event(clnt, event, sb);
  186. if (error || clnt == clnt->cl_parent)
  187. break;
  188. }
  189. return error;
  190. }
  191. static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event)
  192. {
  193. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  194. struct rpc_clnt *clnt;
  195. spin_lock(&sn->rpc_client_lock);
  196. list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
  197. if (rpc_clnt_skip_event(clnt, event))
  198. continue;
  199. spin_unlock(&sn->rpc_client_lock);
  200. return clnt;
  201. }
  202. spin_unlock(&sn->rpc_client_lock);
  203. return NULL;
  204. }
  205. static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
  206. void *ptr)
  207. {
  208. struct super_block *sb = ptr;
  209. struct rpc_clnt *clnt;
  210. int error = 0;
  211. while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) {
  212. error = __rpc_pipefs_event(clnt, event, sb);
  213. if (error)
  214. break;
  215. }
  216. return error;
  217. }
  218. static struct notifier_block rpc_clients_block = {
  219. .notifier_call = rpc_pipefs_event,
  220. .priority = SUNRPC_PIPEFS_RPC_PRIO,
  221. };
  222. int rpc_clients_notifier_register(void)
  223. {
  224. return rpc_pipefs_notifier_register(&rpc_clients_block);
  225. }
  226. void rpc_clients_notifier_unregister(void)
  227. {
  228. return rpc_pipefs_notifier_unregister(&rpc_clients_block);
  229. }
  230. static struct rpc_xprt *rpc_clnt_set_transport(struct rpc_clnt *clnt,
  231. struct rpc_xprt *xprt,
  232. const struct rpc_timeout *timeout)
  233. {
  234. struct rpc_xprt *old;
  235. spin_lock(&clnt->cl_lock);
  236. old = rcu_dereference_protected(clnt->cl_xprt,
  237. lockdep_is_held(&clnt->cl_lock));
  238. if (!xprt_bound(xprt))
  239. clnt->cl_autobind = 1;
  240. clnt->cl_timeout = timeout;
  241. rcu_assign_pointer(clnt->cl_xprt, xprt);
  242. spin_unlock(&clnt->cl_lock);
  243. return old;
  244. }
  245. static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
  246. {
  247. clnt->cl_nodelen = strlcpy(clnt->cl_nodename,
  248. nodename, sizeof(clnt->cl_nodename));
  249. }
  250. static int rpc_client_register(struct rpc_clnt *clnt,
  251. rpc_authflavor_t pseudoflavor,
  252. const char *client_name)
  253. {
  254. struct rpc_auth_create_args auth_args = {
  255. .pseudoflavor = pseudoflavor,
  256. .target_name = client_name,
  257. };
  258. struct rpc_auth *auth;
  259. struct net *net = rpc_net_ns(clnt);
  260. struct super_block *pipefs_sb;
  261. int err;
  262. rpc_clnt_debugfs_register(clnt);
  263. pipefs_sb = rpc_get_sb_net(net);
  264. if (pipefs_sb) {
  265. err = rpc_setup_pipedir(pipefs_sb, clnt);
  266. if (err)
  267. goto out;
  268. }
  269. rpc_register_client(clnt);
  270. if (pipefs_sb)
  271. rpc_put_sb_net(net);
  272. auth = rpcauth_create(&auth_args, clnt);
  273. if (IS_ERR(auth)) {
  274. dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
  275. pseudoflavor);
  276. err = PTR_ERR(auth);
  277. goto err_auth;
  278. }
  279. return 0;
  280. err_auth:
  281. pipefs_sb = rpc_get_sb_net(net);
  282. rpc_unregister_client(clnt);
  283. __rpc_clnt_remove_pipedir(clnt);
  284. out:
  285. if (pipefs_sb)
  286. rpc_put_sb_net(net);
  287. rpc_clnt_debugfs_unregister(clnt);
  288. return err;
  289. }
  290. static DEFINE_IDA(rpc_clids);
  291. void rpc_cleanup_clids(void)
  292. {
  293. ida_destroy(&rpc_clids);
  294. }
  295. static int rpc_alloc_clid(struct rpc_clnt *clnt)
  296. {
  297. int clid;
  298. clid = ida_simple_get(&rpc_clids, 0, 0, GFP_KERNEL);
  299. if (clid < 0)
  300. return clid;
  301. clnt->cl_clid = clid;
  302. return 0;
  303. }
  304. static void rpc_free_clid(struct rpc_clnt *clnt)
  305. {
  306. ida_simple_remove(&rpc_clids, clnt->cl_clid);
  307. }
  308. static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
  309. struct rpc_xprt_switch *xps,
  310. struct rpc_xprt *xprt,
  311. struct rpc_clnt *parent)
  312. {
  313. const struct rpc_program *program = args->program;
  314. const struct rpc_version *version;
  315. struct rpc_clnt *clnt = NULL;
  316. const struct rpc_timeout *timeout;
  317. const char *nodename = args->nodename;
  318. int err;
  319. /* sanity check the name before trying to print it */
  320. dprintk("RPC: creating %s client for %s (xprt %p)\n",
  321. program->name, args->servername, xprt);
  322. err = rpciod_up();
  323. if (err)
  324. goto out_no_rpciod;
  325. err = -EINVAL;
  326. if (args->version >= program->nrvers)
  327. goto out_err;
  328. version = program->version[args->version];
  329. if (version == NULL)
  330. goto out_err;
  331. err = -ENOMEM;
  332. clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
  333. if (!clnt)
  334. goto out_err;
  335. clnt->cl_parent = parent ? : clnt;
  336. err = rpc_alloc_clid(clnt);
  337. if (err)
  338. goto out_no_clid;
  339. clnt->cl_procinfo = version->procs;
  340. clnt->cl_maxproc = version->nrprocs;
  341. clnt->cl_prog = args->prognumber ? : program->number;
  342. clnt->cl_vers = version->number;
  343. clnt->cl_stats = program->stats;
  344. clnt->cl_metrics = rpc_alloc_iostats(clnt);
  345. rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
  346. err = -ENOMEM;
  347. if (clnt->cl_metrics == NULL)
  348. goto out_no_stats;
  349. clnt->cl_program = program;
  350. INIT_LIST_HEAD(&clnt->cl_tasks);
  351. spin_lock_init(&clnt->cl_lock);
  352. timeout = xprt->timeout;
  353. if (args->timeout != NULL) {
  354. memcpy(&clnt->cl_timeout_default, args->timeout,
  355. sizeof(clnt->cl_timeout_default));
  356. timeout = &clnt->cl_timeout_default;
  357. }
  358. rpc_clnt_set_transport(clnt, xprt, timeout);
  359. xprt_iter_init(&clnt->cl_xpi, xps);
  360. xprt_switch_put(xps);
  361. clnt->cl_rtt = &clnt->cl_rtt_default;
  362. rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
  363. atomic_set(&clnt->cl_count, 1);
  364. if (nodename == NULL)
  365. nodename = utsname()->nodename;
  366. /* save the nodename */
  367. rpc_clnt_set_nodename(clnt, nodename);
  368. err = rpc_client_register(clnt, args->authflavor, args->client_name);
  369. if (err)
  370. goto out_no_path;
  371. if (parent)
  372. atomic_inc(&parent->cl_count);
  373. return clnt;
  374. out_no_path:
  375. rpc_free_iostats(clnt->cl_metrics);
  376. out_no_stats:
  377. rpc_free_clid(clnt);
  378. out_no_clid:
  379. kfree(clnt);
  380. out_err:
  381. rpciod_down();
  382. out_no_rpciod:
  383. xprt_switch_put(xps);
  384. xprt_put(xprt);
  385. return ERR_PTR(err);
  386. }
  387. static struct rpc_clnt *rpc_create_xprt(struct rpc_create_args *args,
  388. struct rpc_xprt *xprt)
  389. {
  390. struct rpc_clnt *clnt = NULL;
  391. struct rpc_xprt_switch *xps;
  392. if (args->bc_xprt && args->bc_xprt->xpt_bc_xps) {
  393. WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
  394. xps = args->bc_xprt->xpt_bc_xps;
  395. xprt_switch_get(xps);
  396. } else {
  397. xps = xprt_switch_alloc(xprt, GFP_KERNEL);
  398. if (xps == NULL) {
  399. xprt_put(xprt);
  400. return ERR_PTR(-ENOMEM);
  401. }
  402. if (xprt->bc_xprt) {
  403. xprt_switch_get(xps);
  404. xprt->bc_xprt->xpt_bc_xps = xps;
  405. }
  406. }
  407. clnt = rpc_new_client(args, xps, xprt, NULL);
  408. if (IS_ERR(clnt))
  409. return clnt;
  410. if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
  411. int err = rpc_ping(clnt);
  412. if (err != 0) {
  413. rpc_shutdown_client(clnt);
  414. return ERR_PTR(err);
  415. }
  416. }
  417. clnt->cl_softrtry = 1;
  418. if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
  419. clnt->cl_softrtry = 0;
  420. if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
  421. clnt->cl_autobind = 1;
  422. if (args->flags & RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT)
  423. clnt->cl_noretranstimeo = 1;
  424. if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
  425. clnt->cl_discrtry = 1;
  426. if (!(args->flags & RPC_CLNT_CREATE_QUIET))
  427. clnt->cl_chatty = 1;
  428. return clnt;
  429. }
  430. /**
  431. * rpc_create - create an RPC client and transport with one call
  432. * @args: rpc_clnt create argument structure
  433. *
  434. * Creates and initializes an RPC transport and an RPC client.
  435. *
  436. * It can ping the server in order to determine if it is up, and to see if
  437. * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
  438. * this behavior so asynchronous tasks can also use rpc_create.
  439. */
  440. struct rpc_clnt *rpc_create(struct rpc_create_args *args)
  441. {
  442. struct rpc_xprt *xprt;
  443. struct xprt_create xprtargs = {
  444. .net = args->net,
  445. .ident = args->protocol,
  446. .srcaddr = args->saddress,
  447. .dstaddr = args->address,
  448. .addrlen = args->addrsize,
  449. .servername = args->servername,
  450. .bc_xprt = args->bc_xprt,
  451. };
  452. char servername[48];
  453. if (args->bc_xprt) {
  454. WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
  455. xprt = args->bc_xprt->xpt_bc_xprt;
  456. if (xprt) {
  457. xprt_get(xprt);
  458. return rpc_create_xprt(args, xprt);
  459. }
  460. }
  461. if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
  462. xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
  463. if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
  464. xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
  465. /*
  466. * If the caller chooses not to specify a hostname, whip
  467. * up a string representation of the passed-in address.
  468. */
  469. if (xprtargs.servername == NULL) {
  470. struct sockaddr_un *sun =
  471. (struct sockaddr_un *)args->address;
  472. struct sockaddr_in *sin =
  473. (struct sockaddr_in *)args->address;
  474. struct sockaddr_in6 *sin6 =
  475. (struct sockaddr_in6 *)args->address;
  476. servername[0] = '\0';
  477. switch (args->address->sa_family) {
  478. case AF_LOCAL:
  479. snprintf(servername, sizeof(servername), "%s",
  480. sun->sun_path);
  481. break;
  482. case AF_INET:
  483. snprintf(servername, sizeof(servername), "%pI4",
  484. &sin->sin_addr.s_addr);
  485. break;
  486. case AF_INET6:
  487. snprintf(servername, sizeof(servername), "%pI6",
  488. &sin6->sin6_addr);
  489. break;
  490. default:
  491. /* caller wants default server name, but
  492. * address family isn't recognized. */
  493. return ERR_PTR(-EINVAL);
  494. }
  495. xprtargs.servername = servername;
  496. }
  497. xprt = xprt_create_transport(&xprtargs);
  498. if (IS_ERR(xprt))
  499. return (struct rpc_clnt *)xprt;
  500. /*
  501. * By default, kernel RPC client connects from a reserved port.
  502. * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
  503. * but it is always enabled for rpciod, which handles the connect
  504. * operation.
  505. */
  506. xprt->resvport = 1;
  507. if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
  508. xprt->resvport = 0;
  509. return rpc_create_xprt(args, xprt);
  510. }
  511. EXPORT_SYMBOL_GPL(rpc_create);
  512. /*
  513. * This function clones the RPC client structure. It allows us to share the
  514. * same transport while varying parameters such as the authentication
  515. * flavour.
  516. */
  517. static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
  518. struct rpc_clnt *clnt)
  519. {
  520. struct rpc_xprt_switch *xps;
  521. struct rpc_xprt *xprt;
  522. struct rpc_clnt *new;
  523. int err;
  524. err = -ENOMEM;
  525. rcu_read_lock();
  526. xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
  527. xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
  528. rcu_read_unlock();
  529. if (xprt == NULL || xps == NULL) {
  530. xprt_put(xprt);
  531. xprt_switch_put(xps);
  532. goto out_err;
  533. }
  534. args->servername = xprt->servername;
  535. args->nodename = clnt->cl_nodename;
  536. new = rpc_new_client(args, xps, xprt, clnt);
  537. if (IS_ERR(new)) {
  538. err = PTR_ERR(new);
  539. goto out_err;
  540. }
  541. /* Turn off autobind on clones */
  542. new->cl_autobind = 0;
  543. new->cl_softrtry = clnt->cl_softrtry;
  544. new->cl_noretranstimeo = clnt->cl_noretranstimeo;
  545. new->cl_discrtry = clnt->cl_discrtry;
  546. new->cl_chatty = clnt->cl_chatty;
  547. return new;
  548. out_err:
  549. dprintk("RPC: %s: returned error %d\n", __func__, err);
  550. return ERR_PTR(err);
  551. }
  552. /**
  553. * rpc_clone_client - Clone an RPC client structure
  554. *
  555. * @clnt: RPC client whose parameters are copied
  556. *
  557. * Returns a fresh RPC client or an ERR_PTR.
  558. */
  559. struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
  560. {
  561. struct rpc_create_args args = {
  562. .program = clnt->cl_program,
  563. .prognumber = clnt->cl_prog,
  564. .version = clnt->cl_vers,
  565. .authflavor = clnt->cl_auth->au_flavor,
  566. };
  567. return __rpc_clone_client(&args, clnt);
  568. }
  569. EXPORT_SYMBOL_GPL(rpc_clone_client);
  570. /**
  571. * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
  572. *
  573. * @clnt: RPC client whose parameters are copied
  574. * @flavor: security flavor for new client
  575. *
  576. * Returns a fresh RPC client or an ERR_PTR.
  577. */
  578. struct rpc_clnt *
  579. rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
  580. {
  581. struct rpc_create_args args = {
  582. .program = clnt->cl_program,
  583. .prognumber = clnt->cl_prog,
  584. .version = clnt->cl_vers,
  585. .authflavor = flavor,
  586. };
  587. return __rpc_clone_client(&args, clnt);
  588. }
  589. EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
  590. /**
  591. * rpc_switch_client_transport: switch the RPC transport on the fly
  592. * @clnt: pointer to a struct rpc_clnt
  593. * @args: pointer to the new transport arguments
  594. * @timeout: pointer to the new timeout parameters
  595. *
  596. * This function allows the caller to switch the RPC transport for the
  597. * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
  598. * server, for instance. It assumes that the caller has ensured that
  599. * there are no active RPC tasks by using some form of locking.
  600. *
  601. * Returns zero if "clnt" is now using the new xprt. Otherwise a
  602. * negative errno is returned, and "clnt" continues to use the old
  603. * xprt.
  604. */
  605. int rpc_switch_client_transport(struct rpc_clnt *clnt,
  606. struct xprt_create *args,
  607. const struct rpc_timeout *timeout)
  608. {
  609. const struct rpc_timeout *old_timeo;
  610. rpc_authflavor_t pseudoflavor;
  611. struct rpc_xprt_switch *xps, *oldxps;
  612. struct rpc_xprt *xprt, *old;
  613. struct rpc_clnt *parent;
  614. int err;
  615. xprt = xprt_create_transport(args);
  616. if (IS_ERR(xprt)) {
  617. dprintk("RPC: failed to create new xprt for clnt %p\n",
  618. clnt);
  619. return PTR_ERR(xprt);
  620. }
  621. xps = xprt_switch_alloc(xprt, GFP_KERNEL);
  622. if (xps == NULL) {
  623. xprt_put(xprt);
  624. return -ENOMEM;
  625. }
  626. pseudoflavor = clnt->cl_auth->au_flavor;
  627. old_timeo = clnt->cl_timeout;
  628. old = rpc_clnt_set_transport(clnt, xprt, timeout);
  629. oldxps = xprt_iter_xchg_switch(&clnt->cl_xpi, xps);
  630. rpc_unregister_client(clnt);
  631. __rpc_clnt_remove_pipedir(clnt);
  632. rpc_clnt_debugfs_unregister(clnt);
  633. /*
  634. * A new transport was created. "clnt" therefore
  635. * becomes the root of a new cl_parent tree. clnt's
  636. * children, if it has any, still point to the old xprt.
  637. */
  638. parent = clnt->cl_parent;
  639. clnt->cl_parent = clnt;
  640. /*
  641. * The old rpc_auth cache cannot be re-used. GSS
  642. * contexts in particular are between a single
  643. * client and server.
  644. */
  645. err = rpc_client_register(clnt, pseudoflavor, NULL);
  646. if (err)
  647. goto out_revert;
  648. synchronize_rcu();
  649. if (parent != clnt)
  650. rpc_release_client(parent);
  651. xprt_switch_put(oldxps);
  652. xprt_put(old);
  653. dprintk("RPC: replaced xprt for clnt %p\n", clnt);
  654. return 0;
  655. out_revert:
  656. xps = xprt_iter_xchg_switch(&clnt->cl_xpi, oldxps);
  657. rpc_clnt_set_transport(clnt, old, old_timeo);
  658. clnt->cl_parent = parent;
  659. rpc_client_register(clnt, pseudoflavor, NULL);
  660. xprt_switch_put(xps);
  661. xprt_put(xprt);
  662. dprintk("RPC: failed to switch xprt for clnt %p\n", clnt);
  663. return err;
  664. }
  665. EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
  666. static
  667. int rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi)
  668. {
  669. struct rpc_xprt_switch *xps;
  670. rcu_read_lock();
  671. xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
  672. rcu_read_unlock();
  673. if (xps == NULL)
  674. return -EAGAIN;
  675. xprt_iter_init_listall(xpi, xps);
  676. xprt_switch_put(xps);
  677. return 0;
  678. }
  679. /**
  680. * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
  681. * @clnt: pointer to client
  682. * @fn: function to apply
  683. * @data: void pointer to function data
  684. *
  685. * Iterates through the list of RPC transports currently attached to the
  686. * client and applies the function fn(clnt, xprt, data).
  687. *
  688. * On error, the iteration stops, and the function returns the error value.
  689. */
  690. int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt,
  691. int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *),
  692. void *data)
  693. {
  694. struct rpc_xprt_iter xpi;
  695. int ret;
  696. ret = rpc_clnt_xprt_iter_init(clnt, &xpi);
  697. if (ret)
  698. return ret;
  699. for (;;) {
  700. struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
  701. if (!xprt)
  702. break;
  703. ret = fn(clnt, xprt, data);
  704. xprt_put(xprt);
  705. if (ret < 0)
  706. break;
  707. }
  708. xprt_iter_destroy(&xpi);
  709. return ret;
  710. }
  711. EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt);
  712. /*
  713. * Kill all tasks for the given client.
  714. * XXX: kill their descendants as well?
  715. */
  716. void rpc_killall_tasks(struct rpc_clnt *clnt)
  717. {
  718. struct rpc_task *rovr;
  719. if (list_empty(&clnt->cl_tasks))
  720. return;
  721. dprintk("RPC: killing all tasks for client %p\n", clnt);
  722. /*
  723. * Spin lock all_tasks to prevent changes...
  724. */
  725. spin_lock(&clnt->cl_lock);
  726. list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
  727. if (!RPC_IS_ACTIVATED(rovr))
  728. continue;
  729. if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
  730. rovr->tk_flags |= RPC_TASK_KILLED;
  731. rpc_exit(rovr, -EIO);
  732. if (RPC_IS_QUEUED(rovr))
  733. rpc_wake_up_queued_task(rovr->tk_waitqueue,
  734. rovr);
  735. }
  736. }
  737. spin_unlock(&clnt->cl_lock);
  738. }
  739. EXPORT_SYMBOL_GPL(rpc_killall_tasks);
  740. /*
  741. * Properly shut down an RPC client, terminating all outstanding
  742. * requests.
  743. */
  744. void rpc_shutdown_client(struct rpc_clnt *clnt)
  745. {
  746. might_sleep();
  747. dprintk_rcu("RPC: shutting down %s client for %s\n",
  748. clnt->cl_program->name,
  749. rcu_dereference(clnt->cl_xprt)->servername);
  750. while (!list_empty(&clnt->cl_tasks)) {
  751. rpc_killall_tasks(clnt);
  752. wait_event_timeout(destroy_wait,
  753. list_empty(&clnt->cl_tasks), 1*HZ);
  754. }
  755. rpc_release_client(clnt);
  756. }
  757. EXPORT_SYMBOL_GPL(rpc_shutdown_client);
  758. /*
  759. * Free an RPC client
  760. */
  761. static struct rpc_clnt *
  762. rpc_free_client(struct rpc_clnt *clnt)
  763. {
  764. struct rpc_clnt *parent = NULL;
  765. dprintk_rcu("RPC: destroying %s client for %s\n",
  766. clnt->cl_program->name,
  767. rcu_dereference(clnt->cl_xprt)->servername);
  768. if (clnt->cl_parent != clnt)
  769. parent = clnt->cl_parent;
  770. rpc_clnt_debugfs_unregister(clnt);
  771. rpc_clnt_remove_pipedir(clnt);
  772. rpc_unregister_client(clnt);
  773. rpc_free_iostats(clnt->cl_metrics);
  774. clnt->cl_metrics = NULL;
  775. xprt_put(rcu_dereference_raw(clnt->cl_xprt));
  776. xprt_iter_destroy(&clnt->cl_xpi);
  777. rpciod_down();
  778. rpc_free_clid(clnt);
  779. kfree(clnt);
  780. return parent;
  781. }
  782. /*
  783. * Free an RPC client
  784. */
  785. static struct rpc_clnt *
  786. rpc_free_auth(struct rpc_clnt *clnt)
  787. {
  788. if (clnt->cl_auth == NULL)
  789. return rpc_free_client(clnt);
  790. /*
  791. * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
  792. * release remaining GSS contexts. This mechanism ensures
  793. * that it can do so safely.
  794. */
  795. atomic_inc(&clnt->cl_count);
  796. rpcauth_release(clnt->cl_auth);
  797. clnt->cl_auth = NULL;
  798. if (atomic_dec_and_test(&clnt->cl_count))
  799. return rpc_free_client(clnt);
  800. return NULL;
  801. }
  802. /*
  803. * Release reference to the RPC client
  804. */
  805. void
  806. rpc_release_client(struct rpc_clnt *clnt)
  807. {
  808. dprintk("RPC: rpc_release_client(%p)\n", clnt);
  809. do {
  810. if (list_empty(&clnt->cl_tasks))
  811. wake_up(&destroy_wait);
  812. if (!atomic_dec_and_test(&clnt->cl_count))
  813. break;
  814. clnt = rpc_free_auth(clnt);
  815. } while (clnt != NULL);
  816. }
  817. EXPORT_SYMBOL_GPL(rpc_release_client);
  818. /**
  819. * rpc_bind_new_program - bind a new RPC program to an existing client
  820. * @old: old rpc_client
  821. * @program: rpc program to set
  822. * @vers: rpc program version
  823. *
  824. * Clones the rpc client and sets up a new RPC program. This is mainly
  825. * of use for enabling different RPC programs to share the same transport.
  826. * The Sun NFSv2/v3 ACL protocol can do this.
  827. */
  828. struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
  829. const struct rpc_program *program,
  830. u32 vers)
  831. {
  832. struct rpc_create_args args = {
  833. .program = program,
  834. .prognumber = program->number,
  835. .version = vers,
  836. .authflavor = old->cl_auth->au_flavor,
  837. };
  838. struct rpc_clnt *clnt;
  839. int err;
  840. clnt = __rpc_clone_client(&args, old);
  841. if (IS_ERR(clnt))
  842. goto out;
  843. err = rpc_ping(clnt);
  844. if (err != 0) {
  845. rpc_shutdown_client(clnt);
  846. clnt = ERR_PTR(err);
  847. }
  848. out:
  849. return clnt;
  850. }
  851. EXPORT_SYMBOL_GPL(rpc_bind_new_program);
  852. void rpc_task_release_transport(struct rpc_task *task)
  853. {
  854. struct rpc_xprt *xprt = task->tk_xprt;
  855. if (xprt) {
  856. task->tk_xprt = NULL;
  857. xprt_put(xprt);
  858. }
  859. }
  860. EXPORT_SYMBOL_GPL(rpc_task_release_transport);
  861. void rpc_task_release_client(struct rpc_task *task)
  862. {
  863. struct rpc_clnt *clnt = task->tk_client;
  864. if (clnt != NULL) {
  865. /* Remove from client task list */
  866. spin_lock(&clnt->cl_lock);
  867. list_del(&task->tk_task);
  868. spin_unlock(&clnt->cl_lock);
  869. task->tk_client = NULL;
  870. rpc_release_client(clnt);
  871. }
  872. rpc_task_release_transport(task);
  873. }
  874. static
  875. void rpc_task_set_transport(struct rpc_task *task, struct rpc_clnt *clnt)
  876. {
  877. if (!task->tk_xprt)
  878. task->tk_xprt = xprt_iter_get_next(&clnt->cl_xpi);
  879. }
  880. static
  881. void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
  882. {
  883. if (clnt != NULL) {
  884. rpc_task_set_transport(task, clnt);
  885. task->tk_client = clnt;
  886. atomic_inc(&clnt->cl_count);
  887. if (clnt->cl_softrtry)
  888. task->tk_flags |= RPC_TASK_SOFT;
  889. if (clnt->cl_noretranstimeo)
  890. task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
  891. if (atomic_read(&clnt->cl_swapper))
  892. task->tk_flags |= RPC_TASK_SWAPPER;
  893. /* Add to the client's list of all tasks */
  894. spin_lock(&clnt->cl_lock);
  895. list_add_tail(&task->tk_task, &clnt->cl_tasks);
  896. spin_unlock(&clnt->cl_lock);
  897. }
  898. }
  899. static void
  900. rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
  901. {
  902. if (msg != NULL) {
  903. task->tk_msg.rpc_proc = msg->rpc_proc;
  904. task->tk_msg.rpc_argp = msg->rpc_argp;
  905. task->tk_msg.rpc_resp = msg->rpc_resp;
  906. if (msg->rpc_cred != NULL)
  907. task->tk_msg.rpc_cred = get_rpccred(msg->rpc_cred);
  908. }
  909. }
  910. /*
  911. * Default callback for async RPC calls
  912. */
  913. static void
  914. rpc_default_callback(struct rpc_task *task, void *data)
  915. {
  916. }
  917. static const struct rpc_call_ops rpc_default_ops = {
  918. .rpc_call_done = rpc_default_callback,
  919. };
  920. /**
  921. * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
  922. * @task_setup_data: pointer to task initialisation data
  923. */
  924. struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
  925. {
  926. struct rpc_task *task;
  927. task = rpc_new_task(task_setup_data);
  928. rpc_task_set_client(task, task_setup_data->rpc_client);
  929. rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
  930. if (task->tk_action == NULL)
  931. rpc_call_start(task);
  932. atomic_inc(&task->tk_count);
  933. rpc_execute(task);
  934. return task;
  935. }
  936. EXPORT_SYMBOL_GPL(rpc_run_task);
  937. /**
  938. * rpc_call_sync - Perform a synchronous RPC call
  939. * @clnt: pointer to RPC client
  940. * @msg: RPC call parameters
  941. * @flags: RPC call flags
  942. */
  943. int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
  944. {
  945. struct rpc_task *task;
  946. struct rpc_task_setup task_setup_data = {
  947. .rpc_client = clnt,
  948. .rpc_message = msg,
  949. .callback_ops = &rpc_default_ops,
  950. .flags = flags,
  951. };
  952. int status;
  953. WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
  954. if (flags & RPC_TASK_ASYNC) {
  955. rpc_release_calldata(task_setup_data.callback_ops,
  956. task_setup_data.callback_data);
  957. return -EINVAL;
  958. }
  959. task = rpc_run_task(&task_setup_data);
  960. if (IS_ERR(task))
  961. return PTR_ERR(task);
  962. status = task->tk_status;
  963. rpc_put_task(task);
  964. return status;
  965. }
  966. EXPORT_SYMBOL_GPL(rpc_call_sync);
  967. /**
  968. * rpc_call_async - Perform an asynchronous RPC call
  969. * @clnt: pointer to RPC client
  970. * @msg: RPC call parameters
  971. * @flags: RPC call flags
  972. * @tk_ops: RPC call ops
  973. * @data: user call data
  974. */
  975. int
  976. rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
  977. const struct rpc_call_ops *tk_ops, void *data)
  978. {
  979. struct rpc_task *task;
  980. struct rpc_task_setup task_setup_data = {
  981. .rpc_client = clnt,
  982. .rpc_message = msg,
  983. .callback_ops = tk_ops,
  984. .callback_data = data,
  985. .flags = flags|RPC_TASK_ASYNC,
  986. };
  987. task = rpc_run_task(&task_setup_data);
  988. if (IS_ERR(task))
  989. return PTR_ERR(task);
  990. rpc_put_task(task);
  991. return 0;
  992. }
  993. EXPORT_SYMBOL_GPL(rpc_call_async);
  994. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  995. /**
  996. * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
  997. * rpc_execute against it
  998. * @req: RPC request
  999. */
  1000. struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req)
  1001. {
  1002. struct rpc_task *task;
  1003. struct xdr_buf *xbufp = &req->rq_snd_buf;
  1004. struct rpc_task_setup task_setup_data = {
  1005. .callback_ops = &rpc_default_ops,
  1006. .flags = RPC_TASK_SOFTCONN,
  1007. };
  1008. dprintk("RPC: rpc_run_bc_task req= %p\n", req);
  1009. /*
  1010. * Create an rpc_task to send the data
  1011. */
  1012. task = rpc_new_task(&task_setup_data);
  1013. task->tk_rqstp = req;
  1014. /*
  1015. * Set up the xdr_buf length.
  1016. * This also indicates that the buffer is XDR encoded already.
  1017. */
  1018. xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
  1019. xbufp->tail[0].iov_len;
  1020. task->tk_action = call_bc_transmit;
  1021. atomic_inc(&task->tk_count);
  1022. WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
  1023. rpc_execute(task);
  1024. dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
  1025. return task;
  1026. }
  1027. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  1028. void
  1029. rpc_call_start(struct rpc_task *task)
  1030. {
  1031. task->tk_action = call_start;
  1032. }
  1033. EXPORT_SYMBOL_GPL(rpc_call_start);
  1034. /**
  1035. * rpc_peeraddr - extract remote peer address from clnt's xprt
  1036. * @clnt: RPC client structure
  1037. * @buf: target buffer
  1038. * @bufsize: length of target buffer
  1039. *
  1040. * Returns the number of bytes that are actually in the stored address.
  1041. */
  1042. size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
  1043. {
  1044. size_t bytes;
  1045. struct rpc_xprt *xprt;
  1046. rcu_read_lock();
  1047. xprt = rcu_dereference(clnt->cl_xprt);
  1048. bytes = xprt->addrlen;
  1049. if (bytes > bufsize)
  1050. bytes = bufsize;
  1051. memcpy(buf, &xprt->addr, bytes);
  1052. rcu_read_unlock();
  1053. return bytes;
  1054. }
  1055. EXPORT_SYMBOL_GPL(rpc_peeraddr);
  1056. /**
  1057. * rpc_peeraddr2str - return remote peer address in printable format
  1058. * @clnt: RPC client structure
  1059. * @format: address format
  1060. *
  1061. * NB: the lifetime of the memory referenced by the returned pointer is
  1062. * the same as the rpc_xprt itself. As long as the caller uses this
  1063. * pointer, it must hold the RCU read lock.
  1064. */
  1065. const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
  1066. enum rpc_display_format_t format)
  1067. {
  1068. struct rpc_xprt *xprt;
  1069. xprt = rcu_dereference(clnt->cl_xprt);
  1070. if (xprt->address_strings[format] != NULL)
  1071. return xprt->address_strings[format];
  1072. else
  1073. return "unprintable";
  1074. }
  1075. EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
  1076. static const struct sockaddr_in rpc_inaddr_loopback = {
  1077. .sin_family = AF_INET,
  1078. .sin_addr.s_addr = htonl(INADDR_ANY),
  1079. };
  1080. static const struct sockaddr_in6 rpc_in6addr_loopback = {
  1081. .sin6_family = AF_INET6,
  1082. .sin6_addr = IN6ADDR_ANY_INIT,
  1083. };
  1084. /*
  1085. * Try a getsockname() on a connected datagram socket. Using a
  1086. * connected datagram socket prevents leaving a socket in TIME_WAIT.
  1087. * This conserves the ephemeral port number space.
  1088. *
  1089. * Returns zero and fills in "buf" if successful; otherwise, a
  1090. * negative errno is returned.
  1091. */
  1092. static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
  1093. struct sockaddr *buf, int buflen)
  1094. {
  1095. struct socket *sock;
  1096. int err;
  1097. err = __sock_create(net, sap->sa_family,
  1098. SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
  1099. if (err < 0) {
  1100. dprintk("RPC: can't create UDP socket (%d)\n", err);
  1101. goto out;
  1102. }
  1103. switch (sap->sa_family) {
  1104. case AF_INET:
  1105. err = kernel_bind(sock,
  1106. (struct sockaddr *)&rpc_inaddr_loopback,
  1107. sizeof(rpc_inaddr_loopback));
  1108. break;
  1109. case AF_INET6:
  1110. err = kernel_bind(sock,
  1111. (struct sockaddr *)&rpc_in6addr_loopback,
  1112. sizeof(rpc_in6addr_loopback));
  1113. break;
  1114. default:
  1115. err = -EAFNOSUPPORT;
  1116. goto out;
  1117. }
  1118. if (err < 0) {
  1119. dprintk("RPC: can't bind UDP socket (%d)\n", err);
  1120. goto out_release;
  1121. }
  1122. err = kernel_connect(sock, sap, salen, 0);
  1123. if (err < 0) {
  1124. dprintk("RPC: can't connect UDP socket (%d)\n", err);
  1125. goto out_release;
  1126. }
  1127. err = kernel_getsockname(sock, buf, &buflen);
  1128. if (err < 0) {
  1129. dprintk("RPC: getsockname failed (%d)\n", err);
  1130. goto out_release;
  1131. }
  1132. err = 0;
  1133. if (buf->sa_family == AF_INET6) {
  1134. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
  1135. sin6->sin6_scope_id = 0;
  1136. }
  1137. dprintk("RPC: %s succeeded\n", __func__);
  1138. out_release:
  1139. sock_release(sock);
  1140. out:
  1141. return err;
  1142. }
  1143. /*
  1144. * Scraping a connected socket failed, so we don't have a useable
  1145. * local address. Fallback: generate an address that will prevent
  1146. * the server from calling us back.
  1147. *
  1148. * Returns zero and fills in "buf" if successful; otherwise, a
  1149. * negative errno is returned.
  1150. */
  1151. static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
  1152. {
  1153. switch (family) {
  1154. case AF_INET:
  1155. if (buflen < sizeof(rpc_inaddr_loopback))
  1156. return -EINVAL;
  1157. memcpy(buf, &rpc_inaddr_loopback,
  1158. sizeof(rpc_inaddr_loopback));
  1159. break;
  1160. case AF_INET6:
  1161. if (buflen < sizeof(rpc_in6addr_loopback))
  1162. return -EINVAL;
  1163. memcpy(buf, &rpc_in6addr_loopback,
  1164. sizeof(rpc_in6addr_loopback));
  1165. break;
  1166. default:
  1167. dprintk("RPC: %s: address family not supported\n",
  1168. __func__);
  1169. return -EAFNOSUPPORT;
  1170. }
  1171. dprintk("RPC: %s: succeeded\n", __func__);
  1172. return 0;
  1173. }
  1174. /**
  1175. * rpc_localaddr - discover local endpoint address for an RPC client
  1176. * @clnt: RPC client structure
  1177. * @buf: target buffer
  1178. * @buflen: size of target buffer, in bytes
  1179. *
  1180. * Returns zero and fills in "buf" and "buflen" if successful;
  1181. * otherwise, a negative errno is returned.
  1182. *
  1183. * This works even if the underlying transport is not currently connected,
  1184. * or if the upper layer never previously provided a source address.
  1185. *
  1186. * The result of this function call is transient: multiple calls in
  1187. * succession may give different results, depending on how local
  1188. * networking configuration changes over time.
  1189. */
  1190. int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
  1191. {
  1192. struct sockaddr_storage address;
  1193. struct sockaddr *sap = (struct sockaddr *)&address;
  1194. struct rpc_xprt *xprt;
  1195. struct net *net;
  1196. size_t salen;
  1197. int err;
  1198. rcu_read_lock();
  1199. xprt = rcu_dereference(clnt->cl_xprt);
  1200. salen = xprt->addrlen;
  1201. memcpy(sap, &xprt->addr, salen);
  1202. net = get_net(xprt->xprt_net);
  1203. rcu_read_unlock();
  1204. rpc_set_port(sap, 0);
  1205. err = rpc_sockname(net, sap, salen, buf, buflen);
  1206. put_net(net);
  1207. if (err != 0)
  1208. /* Couldn't discover local address, return ANYADDR */
  1209. return rpc_anyaddr(sap->sa_family, buf, buflen);
  1210. return 0;
  1211. }
  1212. EXPORT_SYMBOL_GPL(rpc_localaddr);
  1213. void
  1214. rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
  1215. {
  1216. struct rpc_xprt *xprt;
  1217. rcu_read_lock();
  1218. xprt = rcu_dereference(clnt->cl_xprt);
  1219. if (xprt->ops->set_buffer_size)
  1220. xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
  1221. rcu_read_unlock();
  1222. }
  1223. EXPORT_SYMBOL_GPL(rpc_setbufsize);
  1224. /**
  1225. * rpc_protocol - Get transport protocol number for an RPC client
  1226. * @clnt: RPC client to query
  1227. *
  1228. */
  1229. int rpc_protocol(struct rpc_clnt *clnt)
  1230. {
  1231. int protocol;
  1232. rcu_read_lock();
  1233. protocol = rcu_dereference(clnt->cl_xprt)->prot;
  1234. rcu_read_unlock();
  1235. return protocol;
  1236. }
  1237. EXPORT_SYMBOL_GPL(rpc_protocol);
  1238. /**
  1239. * rpc_net_ns - Get the network namespace for this RPC client
  1240. * @clnt: RPC client to query
  1241. *
  1242. */
  1243. struct net *rpc_net_ns(struct rpc_clnt *clnt)
  1244. {
  1245. struct net *ret;
  1246. rcu_read_lock();
  1247. ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
  1248. rcu_read_unlock();
  1249. return ret;
  1250. }
  1251. EXPORT_SYMBOL_GPL(rpc_net_ns);
  1252. /**
  1253. * rpc_max_payload - Get maximum payload size for a transport, in bytes
  1254. * @clnt: RPC client to query
  1255. *
  1256. * For stream transports, this is one RPC record fragment (see RFC
  1257. * 1831), as we don't support multi-record requests yet. For datagram
  1258. * transports, this is the size of an IP packet minus the IP, UDP, and
  1259. * RPC header sizes.
  1260. */
  1261. size_t rpc_max_payload(struct rpc_clnt *clnt)
  1262. {
  1263. size_t ret;
  1264. rcu_read_lock();
  1265. ret = rcu_dereference(clnt->cl_xprt)->max_payload;
  1266. rcu_read_unlock();
  1267. return ret;
  1268. }
  1269. EXPORT_SYMBOL_GPL(rpc_max_payload);
  1270. /**
  1271. * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
  1272. * @clnt: RPC client to query
  1273. */
  1274. size_t rpc_max_bc_payload(struct rpc_clnt *clnt)
  1275. {
  1276. struct rpc_xprt *xprt;
  1277. size_t ret;
  1278. rcu_read_lock();
  1279. xprt = rcu_dereference(clnt->cl_xprt);
  1280. ret = xprt->ops->bc_maxpayload(xprt);
  1281. rcu_read_unlock();
  1282. return ret;
  1283. }
  1284. EXPORT_SYMBOL_GPL(rpc_max_bc_payload);
  1285. /**
  1286. * rpc_force_rebind - force transport to check that remote port is unchanged
  1287. * @clnt: client to rebind
  1288. *
  1289. */
  1290. void rpc_force_rebind(struct rpc_clnt *clnt)
  1291. {
  1292. if (clnt->cl_autobind) {
  1293. rcu_read_lock();
  1294. xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
  1295. rcu_read_unlock();
  1296. }
  1297. }
  1298. EXPORT_SYMBOL_GPL(rpc_force_rebind);
  1299. /*
  1300. * Restart an (async) RPC call from the call_prepare state.
  1301. * Usually called from within the exit handler.
  1302. */
  1303. int
  1304. rpc_restart_call_prepare(struct rpc_task *task)
  1305. {
  1306. if (RPC_ASSASSINATED(task))
  1307. return 0;
  1308. task->tk_action = call_start;
  1309. task->tk_status = 0;
  1310. if (task->tk_ops->rpc_call_prepare != NULL)
  1311. task->tk_action = rpc_prepare_task;
  1312. return 1;
  1313. }
  1314. EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
  1315. /*
  1316. * Restart an (async) RPC call. Usually called from within the
  1317. * exit handler.
  1318. */
  1319. int
  1320. rpc_restart_call(struct rpc_task *task)
  1321. {
  1322. if (RPC_ASSASSINATED(task))
  1323. return 0;
  1324. task->tk_action = call_start;
  1325. task->tk_status = 0;
  1326. return 1;
  1327. }
  1328. EXPORT_SYMBOL_GPL(rpc_restart_call);
  1329. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  1330. const char
  1331. *rpc_proc_name(const struct rpc_task *task)
  1332. {
  1333. const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
  1334. if (proc) {
  1335. if (proc->p_name)
  1336. return proc->p_name;
  1337. else
  1338. return "NULL";
  1339. } else
  1340. return "no proc";
  1341. }
  1342. #endif
  1343. /*
  1344. * 0. Initial state
  1345. *
  1346. * Other FSM states can be visited zero or more times, but
  1347. * this state is visited exactly once for each RPC.
  1348. */
  1349. static void
  1350. call_start(struct rpc_task *task)
  1351. {
  1352. struct rpc_clnt *clnt = task->tk_client;
  1353. int idx = task->tk_msg.rpc_proc->p_statidx;
  1354. dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
  1355. clnt->cl_program->name, clnt->cl_vers,
  1356. rpc_proc_name(task),
  1357. (RPC_IS_ASYNC(task) ? "async" : "sync"));
  1358. /* Increment call count (version might not be valid for ping) */
  1359. if (clnt->cl_program->version[clnt->cl_vers])
  1360. clnt->cl_program->version[clnt->cl_vers]->counts[idx]++;
  1361. clnt->cl_stats->rpccnt++;
  1362. task->tk_action = call_reserve;
  1363. rpc_task_set_transport(task, clnt);
  1364. }
  1365. /*
  1366. * 1. Reserve an RPC call slot
  1367. */
  1368. static void
  1369. call_reserve(struct rpc_task *task)
  1370. {
  1371. dprint_status(task);
  1372. task->tk_status = 0;
  1373. task->tk_action = call_reserveresult;
  1374. xprt_reserve(task);
  1375. }
  1376. static void call_retry_reserve(struct rpc_task *task);
  1377. /*
  1378. * 1b. Grok the result of xprt_reserve()
  1379. */
  1380. static void
  1381. call_reserveresult(struct rpc_task *task)
  1382. {
  1383. int status = task->tk_status;
  1384. dprint_status(task);
  1385. /*
  1386. * After a call to xprt_reserve(), we must have either
  1387. * a request slot or else an error status.
  1388. */
  1389. task->tk_status = 0;
  1390. if (status >= 0) {
  1391. if (task->tk_rqstp) {
  1392. task->tk_action = call_refresh;
  1393. return;
  1394. }
  1395. printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
  1396. __func__, status);
  1397. rpc_exit(task, -EIO);
  1398. return;
  1399. }
  1400. /*
  1401. * Even though there was an error, we may have acquired
  1402. * a request slot somehow. Make sure not to leak it.
  1403. */
  1404. if (task->tk_rqstp) {
  1405. printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
  1406. __func__, status);
  1407. xprt_release(task);
  1408. }
  1409. switch (status) {
  1410. case -ENOMEM:
  1411. rpc_delay(task, HZ >> 2);
  1412. case -EAGAIN: /* woken up; retry */
  1413. task->tk_action = call_retry_reserve;
  1414. return;
  1415. case -EIO: /* probably a shutdown */
  1416. break;
  1417. default:
  1418. printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
  1419. __func__, status);
  1420. break;
  1421. }
  1422. rpc_exit(task, status);
  1423. }
  1424. /*
  1425. * 1c. Retry reserving an RPC call slot
  1426. */
  1427. static void
  1428. call_retry_reserve(struct rpc_task *task)
  1429. {
  1430. dprint_status(task);
  1431. task->tk_status = 0;
  1432. task->tk_action = call_reserveresult;
  1433. xprt_retry_reserve(task);
  1434. }
  1435. /*
  1436. * 2. Bind and/or refresh the credentials
  1437. */
  1438. static void
  1439. call_refresh(struct rpc_task *task)
  1440. {
  1441. dprint_status(task);
  1442. task->tk_action = call_refreshresult;
  1443. task->tk_status = 0;
  1444. task->tk_client->cl_stats->rpcauthrefresh++;
  1445. rpcauth_refreshcred(task);
  1446. }
  1447. /*
  1448. * 2a. Process the results of a credential refresh
  1449. */
  1450. static void
  1451. call_refreshresult(struct rpc_task *task)
  1452. {
  1453. int status = task->tk_status;
  1454. dprint_status(task);
  1455. task->tk_status = 0;
  1456. task->tk_action = call_refresh;
  1457. switch (status) {
  1458. case 0:
  1459. if (rpcauth_uptodatecred(task)) {
  1460. task->tk_action = call_allocate;
  1461. return;
  1462. }
  1463. /* Use rate-limiting and a max number of retries if refresh
  1464. * had status 0 but failed to update the cred.
  1465. */
  1466. case -ETIMEDOUT:
  1467. rpc_delay(task, 3*HZ);
  1468. case -EAGAIN:
  1469. status = -EACCES;
  1470. case -EKEYEXPIRED:
  1471. if (!task->tk_cred_retry)
  1472. break;
  1473. task->tk_cred_retry--;
  1474. dprintk("RPC: %5u %s: retry refresh creds\n",
  1475. task->tk_pid, __func__);
  1476. return;
  1477. }
  1478. dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
  1479. task->tk_pid, __func__, status);
  1480. rpc_exit(task, status);
  1481. }
  1482. /*
  1483. * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
  1484. * (Note: buffer memory is freed in xprt_release).
  1485. */
  1486. static void
  1487. call_allocate(struct rpc_task *task)
  1488. {
  1489. unsigned int slack = task->tk_rqstp->rq_cred->cr_auth->au_cslack;
  1490. struct rpc_rqst *req = task->tk_rqstp;
  1491. struct rpc_xprt *xprt = req->rq_xprt;
  1492. const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
  1493. int status;
  1494. dprint_status(task);
  1495. task->tk_status = 0;
  1496. task->tk_action = call_bind;
  1497. if (req->rq_buffer)
  1498. return;
  1499. if (proc->p_proc != 0) {
  1500. BUG_ON(proc->p_arglen == 0);
  1501. if (proc->p_decode != NULL)
  1502. BUG_ON(proc->p_replen == 0);
  1503. }
  1504. /*
  1505. * Calculate the size (in quads) of the RPC call
  1506. * and reply headers, and convert both values
  1507. * to byte sizes.
  1508. */
  1509. req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
  1510. req->rq_callsize <<= 2;
  1511. req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
  1512. req->rq_rcvsize <<= 2;
  1513. status = xprt->ops->buf_alloc(task);
  1514. xprt_inject_disconnect(xprt);
  1515. if (status == 0)
  1516. return;
  1517. if (status != -ENOMEM) {
  1518. rpc_exit(task, status);
  1519. return;
  1520. }
  1521. dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
  1522. if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
  1523. task->tk_action = call_allocate;
  1524. rpc_delay(task, HZ>>4);
  1525. return;
  1526. }
  1527. rpc_exit(task, -ERESTARTSYS);
  1528. }
  1529. static inline int
  1530. rpc_task_need_encode(struct rpc_task *task)
  1531. {
  1532. return task->tk_rqstp->rq_snd_buf.len == 0;
  1533. }
  1534. static inline void
  1535. rpc_task_force_reencode(struct rpc_task *task)
  1536. {
  1537. task->tk_rqstp->rq_snd_buf.len = 0;
  1538. task->tk_rqstp->rq_bytes_sent = 0;
  1539. }
  1540. /*
  1541. * 3. Encode arguments of an RPC call
  1542. */
  1543. static void
  1544. rpc_xdr_encode(struct rpc_task *task)
  1545. {
  1546. struct rpc_rqst *req = task->tk_rqstp;
  1547. kxdreproc_t encode;
  1548. __be32 *p;
  1549. dprint_status(task);
  1550. xdr_buf_init(&req->rq_snd_buf,
  1551. req->rq_buffer,
  1552. req->rq_callsize);
  1553. xdr_buf_init(&req->rq_rcv_buf,
  1554. req->rq_rbuffer,
  1555. req->rq_rcvsize);
  1556. p = rpc_encode_header(task);
  1557. if (p == NULL) {
  1558. printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
  1559. rpc_exit(task, -EIO);
  1560. return;
  1561. }
  1562. encode = task->tk_msg.rpc_proc->p_encode;
  1563. if (encode == NULL)
  1564. return;
  1565. task->tk_status = rpcauth_wrap_req(task, encode, req, p,
  1566. task->tk_msg.rpc_argp);
  1567. }
  1568. /*
  1569. * 4. Get the server port number if not yet set
  1570. */
  1571. static void
  1572. call_bind(struct rpc_task *task)
  1573. {
  1574. struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
  1575. dprint_status(task);
  1576. task->tk_action = call_connect;
  1577. if (!xprt_bound(xprt)) {
  1578. task->tk_action = call_bind_status;
  1579. task->tk_timeout = xprt->bind_timeout;
  1580. xprt->ops->rpcbind(task);
  1581. }
  1582. }
  1583. /*
  1584. * 4a. Sort out bind result
  1585. */
  1586. static void
  1587. call_bind_status(struct rpc_task *task)
  1588. {
  1589. int status = -EIO;
  1590. if (task->tk_status >= 0) {
  1591. dprint_status(task);
  1592. task->tk_status = 0;
  1593. task->tk_action = call_connect;
  1594. return;
  1595. }
  1596. trace_rpc_bind_status(task);
  1597. switch (task->tk_status) {
  1598. case -ENOMEM:
  1599. dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
  1600. rpc_delay(task, HZ >> 2);
  1601. goto retry_timeout;
  1602. case -EACCES:
  1603. dprintk("RPC: %5u remote rpcbind: RPC program/version "
  1604. "unavailable\n", task->tk_pid);
  1605. /* fail immediately if this is an RPC ping */
  1606. if (task->tk_msg.rpc_proc->p_proc == 0) {
  1607. status = -EOPNOTSUPP;
  1608. break;
  1609. }
  1610. if (task->tk_rebind_retry == 0)
  1611. break;
  1612. task->tk_rebind_retry--;
  1613. rpc_delay(task, 3*HZ);
  1614. goto retry_timeout;
  1615. case -ETIMEDOUT:
  1616. dprintk("RPC: %5u rpcbind request timed out\n",
  1617. task->tk_pid);
  1618. goto retry_timeout;
  1619. case -EPFNOSUPPORT:
  1620. /* server doesn't support any rpcbind version we know of */
  1621. dprintk("RPC: %5u unrecognized remote rpcbind service\n",
  1622. task->tk_pid);
  1623. break;
  1624. case -EPROTONOSUPPORT:
  1625. dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
  1626. task->tk_pid);
  1627. goto retry_timeout;
  1628. case -ECONNREFUSED: /* connection problems */
  1629. case -ECONNRESET:
  1630. case -ECONNABORTED:
  1631. case -ENOTCONN:
  1632. case -EHOSTDOWN:
  1633. case -EHOSTUNREACH:
  1634. case -ENETUNREACH:
  1635. case -ENOBUFS:
  1636. case -EPIPE:
  1637. dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
  1638. task->tk_pid, task->tk_status);
  1639. if (!RPC_IS_SOFTCONN(task)) {
  1640. rpc_delay(task, 5*HZ);
  1641. goto retry_timeout;
  1642. }
  1643. status = task->tk_status;
  1644. break;
  1645. default:
  1646. dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
  1647. task->tk_pid, -task->tk_status);
  1648. }
  1649. rpc_exit(task, status);
  1650. return;
  1651. retry_timeout:
  1652. task->tk_status = 0;
  1653. task->tk_action = call_timeout;
  1654. }
  1655. /*
  1656. * 4b. Connect to the RPC server
  1657. */
  1658. static void
  1659. call_connect(struct rpc_task *task)
  1660. {
  1661. struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
  1662. dprintk("RPC: %5u call_connect xprt %p %s connected\n",
  1663. task->tk_pid, xprt,
  1664. (xprt_connected(xprt) ? "is" : "is not"));
  1665. task->tk_action = call_transmit;
  1666. if (!xprt_connected(xprt)) {
  1667. task->tk_action = call_connect_status;
  1668. if (task->tk_status < 0)
  1669. return;
  1670. if (task->tk_flags & RPC_TASK_NOCONNECT) {
  1671. rpc_exit(task, -ENOTCONN);
  1672. return;
  1673. }
  1674. xprt_connect(task);
  1675. }
  1676. }
  1677. /*
  1678. * 4c. Sort out connect result
  1679. */
  1680. static void
  1681. call_connect_status(struct rpc_task *task)
  1682. {
  1683. struct rpc_clnt *clnt = task->tk_client;
  1684. int status = task->tk_status;
  1685. dprint_status(task);
  1686. trace_rpc_connect_status(task, status);
  1687. task->tk_status = 0;
  1688. switch (status) {
  1689. case -ECONNREFUSED:
  1690. /* A positive refusal suggests a rebind is needed. */
  1691. if (RPC_IS_SOFTCONN(task))
  1692. break;
  1693. if (clnt->cl_autobind) {
  1694. rpc_force_rebind(clnt);
  1695. task->tk_action = call_bind;
  1696. return;
  1697. }
  1698. case -ECONNRESET:
  1699. case -ECONNABORTED:
  1700. case -ENETUNREACH:
  1701. case -EHOSTUNREACH:
  1702. case -EADDRINUSE:
  1703. case -ENOBUFS:
  1704. case -EPIPE:
  1705. xprt_conditional_disconnect(task->tk_rqstp->rq_xprt,
  1706. task->tk_rqstp->rq_connect_cookie);
  1707. if (RPC_IS_SOFTCONN(task))
  1708. break;
  1709. /* retry with existing socket, after a delay */
  1710. rpc_delay(task, 3*HZ);
  1711. case -EAGAIN:
  1712. /* Check for timeouts before looping back to call_bind */
  1713. case -ETIMEDOUT:
  1714. task->tk_action = call_timeout;
  1715. return;
  1716. case 0:
  1717. clnt->cl_stats->netreconn++;
  1718. task->tk_action = call_transmit;
  1719. return;
  1720. }
  1721. rpc_exit(task, status);
  1722. }
  1723. /*
  1724. * 5. Transmit the RPC request, and wait for reply
  1725. */
  1726. static void
  1727. call_transmit(struct rpc_task *task)
  1728. {
  1729. int is_retrans = RPC_WAS_SENT(task);
  1730. dprint_status(task);
  1731. task->tk_action = call_status;
  1732. if (task->tk_status < 0)
  1733. return;
  1734. if (!xprt_prepare_transmit(task))
  1735. return;
  1736. task->tk_action = call_transmit_status;
  1737. /* Encode here so that rpcsec_gss can use correct sequence number. */
  1738. if (rpc_task_need_encode(task)) {
  1739. rpc_xdr_encode(task);
  1740. /* Did the encode result in an error condition? */
  1741. if (task->tk_status != 0) {
  1742. /* Was the error nonfatal? */
  1743. if (task->tk_status == -EAGAIN)
  1744. rpc_delay(task, HZ >> 4);
  1745. else
  1746. rpc_exit(task, task->tk_status);
  1747. return;
  1748. }
  1749. }
  1750. xprt_transmit(task);
  1751. if (task->tk_status < 0)
  1752. return;
  1753. if (is_retrans)
  1754. task->tk_client->cl_stats->rpcretrans++;
  1755. /*
  1756. * On success, ensure that we call xprt_end_transmit() before sleeping
  1757. * in order to allow access to the socket to other RPC requests.
  1758. */
  1759. call_transmit_status(task);
  1760. if (rpc_reply_expected(task))
  1761. return;
  1762. task->tk_action = rpc_exit_task;
  1763. rpc_wake_up_queued_task(&task->tk_rqstp->rq_xprt->pending, task);
  1764. }
  1765. /*
  1766. * 5a. Handle cleanup after a transmission
  1767. */
  1768. static void
  1769. call_transmit_status(struct rpc_task *task)
  1770. {
  1771. task->tk_action = call_status;
  1772. /*
  1773. * Common case: success. Force the compiler to put this
  1774. * test first.
  1775. */
  1776. if (task->tk_status == 0) {
  1777. xprt_end_transmit(task);
  1778. rpc_task_force_reencode(task);
  1779. return;
  1780. }
  1781. switch (task->tk_status) {
  1782. case -EAGAIN:
  1783. case -ENOBUFS:
  1784. break;
  1785. default:
  1786. dprint_status(task);
  1787. xprt_end_transmit(task);
  1788. rpc_task_force_reencode(task);
  1789. break;
  1790. /*
  1791. * Special cases: if we've been waiting on the
  1792. * socket's write_space() callback, or if the
  1793. * socket just returned a connection error,
  1794. * then hold onto the transport lock.
  1795. */
  1796. case -ECONNREFUSED:
  1797. case -EHOSTDOWN:
  1798. case -EHOSTUNREACH:
  1799. case -ENETUNREACH:
  1800. case -EPERM:
  1801. if (RPC_IS_SOFTCONN(task)) {
  1802. xprt_end_transmit(task);
  1803. rpc_exit(task, task->tk_status);
  1804. break;
  1805. }
  1806. case -ECONNRESET:
  1807. case -ECONNABORTED:
  1808. case -EADDRINUSE:
  1809. case -ENOTCONN:
  1810. case -EPIPE:
  1811. rpc_task_force_reencode(task);
  1812. }
  1813. }
  1814. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  1815. /*
  1816. * 5b. Send the backchannel RPC reply. On error, drop the reply. In
  1817. * addition, disconnect on connectivity errors.
  1818. */
  1819. static void
  1820. call_bc_transmit(struct rpc_task *task)
  1821. {
  1822. struct rpc_rqst *req = task->tk_rqstp;
  1823. if (!xprt_prepare_transmit(task))
  1824. goto out_retry;
  1825. if (task->tk_status < 0) {
  1826. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1827. "error: %d\n", task->tk_status);
  1828. goto out_done;
  1829. }
  1830. if (req->rq_connect_cookie != req->rq_xprt->connect_cookie)
  1831. req->rq_bytes_sent = 0;
  1832. xprt_transmit(task);
  1833. if (task->tk_status == -EAGAIN)
  1834. goto out_nospace;
  1835. xprt_end_transmit(task);
  1836. dprint_status(task);
  1837. switch (task->tk_status) {
  1838. case 0:
  1839. /* Success */
  1840. case -EHOSTDOWN:
  1841. case -EHOSTUNREACH:
  1842. case -ENETUNREACH:
  1843. case -ECONNRESET:
  1844. case -ECONNREFUSED:
  1845. case -EADDRINUSE:
  1846. case -ENOTCONN:
  1847. case -EPIPE:
  1848. break;
  1849. case -ETIMEDOUT:
  1850. /*
  1851. * Problem reaching the server. Disconnect and let the
  1852. * forechannel reestablish the connection. The server will
  1853. * have to retransmit the backchannel request and we'll
  1854. * reprocess it. Since these ops are idempotent, there's no
  1855. * need to cache our reply at this time.
  1856. */
  1857. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1858. "error: %d\n", task->tk_status);
  1859. xprt_conditional_disconnect(req->rq_xprt,
  1860. req->rq_connect_cookie);
  1861. break;
  1862. default:
  1863. /*
  1864. * We were unable to reply and will have to drop the
  1865. * request. The server should reconnect and retransmit.
  1866. */
  1867. WARN_ON_ONCE(task->tk_status == -EAGAIN);
  1868. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1869. "error: %d\n", task->tk_status);
  1870. break;
  1871. }
  1872. rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
  1873. out_done:
  1874. task->tk_action = rpc_exit_task;
  1875. return;
  1876. out_nospace:
  1877. req->rq_connect_cookie = req->rq_xprt->connect_cookie;
  1878. out_retry:
  1879. task->tk_status = 0;
  1880. }
  1881. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  1882. /*
  1883. * 6. Sort out the RPC call status
  1884. */
  1885. static void
  1886. call_status(struct rpc_task *task)
  1887. {
  1888. struct rpc_clnt *clnt = task->tk_client;
  1889. struct rpc_rqst *req = task->tk_rqstp;
  1890. int status;
  1891. if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
  1892. task->tk_status = req->rq_reply_bytes_recvd;
  1893. dprint_status(task);
  1894. status = task->tk_status;
  1895. if (status >= 0) {
  1896. task->tk_action = call_decode;
  1897. return;
  1898. }
  1899. trace_rpc_call_status(task);
  1900. task->tk_status = 0;
  1901. switch(status) {
  1902. case -EHOSTDOWN:
  1903. case -EHOSTUNREACH:
  1904. case -ENETUNREACH:
  1905. case -EPERM:
  1906. if (RPC_IS_SOFTCONN(task)) {
  1907. rpc_exit(task, status);
  1908. break;
  1909. }
  1910. /*
  1911. * Delay any retries for 3 seconds, then handle as if it
  1912. * were a timeout.
  1913. */
  1914. rpc_delay(task, 3*HZ);
  1915. case -ETIMEDOUT:
  1916. task->tk_action = call_timeout;
  1917. break;
  1918. case -ECONNREFUSED:
  1919. case -ECONNRESET:
  1920. case -ECONNABORTED:
  1921. rpc_force_rebind(clnt);
  1922. case -EADDRINUSE:
  1923. rpc_delay(task, 3*HZ);
  1924. case -EPIPE:
  1925. case -ENOTCONN:
  1926. task->tk_action = call_bind;
  1927. break;
  1928. case -ENOBUFS:
  1929. rpc_delay(task, HZ>>2);
  1930. case -EAGAIN:
  1931. task->tk_action = call_transmit;
  1932. break;
  1933. case -EIO:
  1934. /* shutdown or soft timeout */
  1935. rpc_exit(task, status);
  1936. break;
  1937. default:
  1938. if (clnt->cl_chatty)
  1939. printk("%s: RPC call returned error %d\n",
  1940. clnt->cl_program->name, -status);
  1941. rpc_exit(task, status);
  1942. }
  1943. }
  1944. /*
  1945. * 6a. Handle RPC timeout
  1946. * We do not release the request slot, so we keep using the
  1947. * same XID for all retransmits.
  1948. */
  1949. static void
  1950. call_timeout(struct rpc_task *task)
  1951. {
  1952. struct rpc_clnt *clnt = task->tk_client;
  1953. if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
  1954. dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
  1955. goto retry;
  1956. }
  1957. dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
  1958. task->tk_timeouts++;
  1959. if (RPC_IS_SOFTCONN(task)) {
  1960. rpc_exit(task, -ETIMEDOUT);
  1961. return;
  1962. }
  1963. if (RPC_IS_SOFT(task)) {
  1964. if (clnt->cl_chatty) {
  1965. printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
  1966. clnt->cl_program->name,
  1967. task->tk_xprt->servername);
  1968. }
  1969. if (task->tk_flags & RPC_TASK_TIMEOUT)
  1970. rpc_exit(task, -ETIMEDOUT);
  1971. else
  1972. rpc_exit(task, -EIO);
  1973. return;
  1974. }
  1975. if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
  1976. task->tk_flags |= RPC_CALL_MAJORSEEN;
  1977. if (clnt->cl_chatty) {
  1978. printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
  1979. clnt->cl_program->name,
  1980. task->tk_xprt->servername);
  1981. }
  1982. }
  1983. rpc_force_rebind(clnt);
  1984. /*
  1985. * Did our request time out due to an RPCSEC_GSS out-of-sequence
  1986. * event? RFC2203 requires the server to drop all such requests.
  1987. */
  1988. rpcauth_invalcred(task);
  1989. retry:
  1990. task->tk_action = call_bind;
  1991. task->tk_status = 0;
  1992. }
  1993. /*
  1994. * 7. Decode the RPC reply
  1995. */
  1996. static void
  1997. call_decode(struct rpc_task *task)
  1998. {
  1999. struct rpc_clnt *clnt = task->tk_client;
  2000. struct rpc_rqst *req = task->tk_rqstp;
  2001. kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
  2002. __be32 *p;
  2003. dprint_status(task);
  2004. if (task->tk_flags & RPC_CALL_MAJORSEEN) {
  2005. if (clnt->cl_chatty) {
  2006. printk(KERN_NOTICE "%s: server %s OK\n",
  2007. clnt->cl_program->name,
  2008. task->tk_xprt->servername);
  2009. }
  2010. task->tk_flags &= ~RPC_CALL_MAJORSEEN;
  2011. }
  2012. /*
  2013. * Ensure that we see all writes made by xprt_complete_rqst()
  2014. * before it changed req->rq_reply_bytes_recvd.
  2015. */
  2016. smp_rmb();
  2017. req->rq_rcv_buf.len = req->rq_private_buf.len;
  2018. /* Check that the softirq receive buffer is valid */
  2019. WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
  2020. sizeof(req->rq_rcv_buf)) != 0);
  2021. if (req->rq_rcv_buf.len < 12) {
  2022. if (!RPC_IS_SOFT(task)) {
  2023. task->tk_action = call_bind;
  2024. goto out_retry;
  2025. }
  2026. dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
  2027. clnt->cl_program->name, task->tk_status);
  2028. task->tk_action = call_timeout;
  2029. goto out_retry;
  2030. }
  2031. p = rpc_verify_header(task);
  2032. if (IS_ERR(p)) {
  2033. if (p == ERR_PTR(-EAGAIN))
  2034. goto out_retry;
  2035. return;
  2036. }
  2037. task->tk_action = rpc_exit_task;
  2038. if (decode) {
  2039. task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
  2040. task->tk_msg.rpc_resp);
  2041. }
  2042. dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
  2043. task->tk_status);
  2044. return;
  2045. out_retry:
  2046. task->tk_status = 0;
  2047. /* Note: rpc_verify_header() may have freed the RPC slot */
  2048. if (task->tk_rqstp == req) {
  2049. req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
  2050. if (task->tk_client->cl_discrtry)
  2051. xprt_conditional_disconnect(req->rq_xprt,
  2052. req->rq_connect_cookie);
  2053. }
  2054. }
  2055. static __be32 *
  2056. rpc_encode_header(struct rpc_task *task)
  2057. {
  2058. struct rpc_clnt *clnt = task->tk_client;
  2059. struct rpc_rqst *req = task->tk_rqstp;
  2060. __be32 *p = req->rq_svec[0].iov_base;
  2061. /* FIXME: check buffer size? */
  2062. p = xprt_skip_transport_header(req->rq_xprt, p);
  2063. *p++ = req->rq_xid; /* XID */
  2064. *p++ = htonl(RPC_CALL); /* CALL */
  2065. *p++ = htonl(RPC_VERSION); /* RPC version */
  2066. *p++ = htonl(clnt->cl_prog); /* program number */
  2067. *p++ = htonl(clnt->cl_vers); /* program version */
  2068. *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
  2069. p = rpcauth_marshcred(task, p);
  2070. req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
  2071. return p;
  2072. }
  2073. static __be32 *
  2074. rpc_verify_header(struct rpc_task *task)
  2075. {
  2076. struct rpc_clnt *clnt = task->tk_client;
  2077. struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
  2078. int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
  2079. __be32 *p = iov->iov_base;
  2080. u32 n;
  2081. int error = -EACCES;
  2082. if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
  2083. /* RFC-1014 says that the representation of XDR data must be a
  2084. * multiple of four bytes
  2085. * - if it isn't pointer subtraction in the NFS client may give
  2086. * undefined results
  2087. */
  2088. dprintk("RPC: %5u %s: XDR representation not a multiple of"
  2089. " 4 bytes: 0x%x\n", task->tk_pid, __func__,
  2090. task->tk_rqstp->rq_rcv_buf.len);
  2091. error = -EIO;
  2092. goto out_err;
  2093. }
  2094. if ((len -= 3) < 0)
  2095. goto out_overflow;
  2096. p += 1; /* skip XID */
  2097. if ((n = ntohl(*p++)) != RPC_REPLY) {
  2098. dprintk("RPC: %5u %s: not an RPC reply: %x\n",
  2099. task->tk_pid, __func__, n);
  2100. error = -EIO;
  2101. goto out_garbage;
  2102. }
  2103. if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
  2104. if (--len < 0)
  2105. goto out_overflow;
  2106. switch ((n = ntohl(*p++))) {
  2107. case RPC_AUTH_ERROR:
  2108. break;
  2109. case RPC_MISMATCH:
  2110. dprintk("RPC: %5u %s: RPC call version mismatch!\n",
  2111. task->tk_pid, __func__);
  2112. error = -EPROTONOSUPPORT;
  2113. goto out_err;
  2114. default:
  2115. dprintk("RPC: %5u %s: RPC call rejected, "
  2116. "unknown error: %x\n",
  2117. task->tk_pid, __func__, n);
  2118. error = -EIO;
  2119. goto out_err;
  2120. }
  2121. if (--len < 0)
  2122. goto out_overflow;
  2123. switch ((n = ntohl(*p++))) {
  2124. case RPC_AUTH_REJECTEDCRED:
  2125. case RPC_AUTH_REJECTEDVERF:
  2126. case RPCSEC_GSS_CREDPROBLEM:
  2127. case RPCSEC_GSS_CTXPROBLEM:
  2128. if (!task->tk_cred_retry)
  2129. break;
  2130. task->tk_cred_retry--;
  2131. dprintk("RPC: %5u %s: retry stale creds\n",
  2132. task->tk_pid, __func__);
  2133. rpcauth_invalcred(task);
  2134. /* Ensure we obtain a new XID! */
  2135. xprt_release(task);
  2136. task->tk_action = call_reserve;
  2137. goto out_retry;
  2138. case RPC_AUTH_BADCRED:
  2139. case RPC_AUTH_BADVERF:
  2140. /* possibly garbled cred/verf? */
  2141. if (!task->tk_garb_retry)
  2142. break;
  2143. task->tk_garb_retry--;
  2144. dprintk("RPC: %5u %s: retry garbled creds\n",
  2145. task->tk_pid, __func__);
  2146. task->tk_action = call_bind;
  2147. goto out_retry;
  2148. case RPC_AUTH_TOOWEAK:
  2149. printk(KERN_NOTICE "RPC: server %s requires stronger "
  2150. "authentication.\n",
  2151. task->tk_xprt->servername);
  2152. break;
  2153. default:
  2154. dprintk("RPC: %5u %s: unknown auth error: %x\n",
  2155. task->tk_pid, __func__, n);
  2156. error = -EIO;
  2157. }
  2158. dprintk("RPC: %5u %s: call rejected %d\n",
  2159. task->tk_pid, __func__, n);
  2160. goto out_err;
  2161. }
  2162. p = rpcauth_checkverf(task, p);
  2163. if (IS_ERR(p)) {
  2164. error = PTR_ERR(p);
  2165. dprintk("RPC: %5u %s: auth check failed with %d\n",
  2166. task->tk_pid, __func__, error);
  2167. goto out_garbage; /* bad verifier, retry */
  2168. }
  2169. len = p - (__be32 *)iov->iov_base - 1;
  2170. if (len < 0)
  2171. goto out_overflow;
  2172. switch ((n = ntohl(*p++))) {
  2173. case RPC_SUCCESS:
  2174. return p;
  2175. case RPC_PROG_UNAVAIL:
  2176. dprintk("RPC: %5u %s: program %u is unsupported "
  2177. "by server %s\n", task->tk_pid, __func__,
  2178. (unsigned int)clnt->cl_prog,
  2179. task->tk_xprt->servername);
  2180. error = -EPFNOSUPPORT;
  2181. goto out_err;
  2182. case RPC_PROG_MISMATCH:
  2183. dprintk("RPC: %5u %s: program %u, version %u unsupported "
  2184. "by server %s\n", task->tk_pid, __func__,
  2185. (unsigned int)clnt->cl_prog,
  2186. (unsigned int)clnt->cl_vers,
  2187. task->tk_xprt->servername);
  2188. error = -EPROTONOSUPPORT;
  2189. goto out_err;
  2190. case RPC_PROC_UNAVAIL:
  2191. dprintk("RPC: %5u %s: proc %s unsupported by program %u, "
  2192. "version %u on server %s\n",
  2193. task->tk_pid, __func__,
  2194. rpc_proc_name(task),
  2195. clnt->cl_prog, clnt->cl_vers,
  2196. task->tk_xprt->servername);
  2197. error = -EOPNOTSUPP;
  2198. goto out_err;
  2199. case RPC_GARBAGE_ARGS:
  2200. dprintk("RPC: %5u %s: server saw garbage\n",
  2201. task->tk_pid, __func__);
  2202. break; /* retry */
  2203. default:
  2204. dprintk("RPC: %5u %s: server accept status: %x\n",
  2205. task->tk_pid, __func__, n);
  2206. /* Also retry */
  2207. }
  2208. out_garbage:
  2209. clnt->cl_stats->rpcgarbage++;
  2210. if (task->tk_garb_retry) {
  2211. task->tk_garb_retry--;
  2212. dprintk("RPC: %5u %s: retrying\n",
  2213. task->tk_pid, __func__);
  2214. task->tk_action = call_bind;
  2215. out_retry:
  2216. return ERR_PTR(-EAGAIN);
  2217. }
  2218. out_err:
  2219. rpc_exit(task, error);
  2220. dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
  2221. __func__, error);
  2222. return ERR_PTR(error);
  2223. out_overflow:
  2224. dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
  2225. __func__);
  2226. goto out_garbage;
  2227. }
  2228. static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
  2229. const void *obj)
  2230. {
  2231. }
  2232. static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
  2233. void *obj)
  2234. {
  2235. return 0;
  2236. }
  2237. static const struct rpc_procinfo rpcproc_null = {
  2238. .p_encode = rpcproc_encode_null,
  2239. .p_decode = rpcproc_decode_null,
  2240. };
  2241. static int rpc_ping(struct rpc_clnt *clnt)
  2242. {
  2243. struct rpc_message msg = {
  2244. .rpc_proc = &rpcproc_null,
  2245. };
  2246. int err;
  2247. msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
  2248. err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
  2249. put_rpccred(msg.rpc_cred);
  2250. return err;
  2251. }
  2252. static
  2253. struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
  2254. struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
  2255. const struct rpc_call_ops *ops, void *data)
  2256. {
  2257. struct rpc_message msg = {
  2258. .rpc_proc = &rpcproc_null,
  2259. .rpc_cred = cred,
  2260. };
  2261. struct rpc_task_setup task_setup_data = {
  2262. .rpc_client = clnt,
  2263. .rpc_xprt = xprt,
  2264. .rpc_message = &msg,
  2265. .callback_ops = (ops != NULL) ? ops : &rpc_default_ops,
  2266. .callback_data = data,
  2267. .flags = flags,
  2268. };
  2269. return rpc_run_task(&task_setup_data);
  2270. }
  2271. struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
  2272. {
  2273. return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
  2274. }
  2275. EXPORT_SYMBOL_GPL(rpc_call_null);
  2276. struct rpc_cb_add_xprt_calldata {
  2277. struct rpc_xprt_switch *xps;
  2278. struct rpc_xprt *xprt;
  2279. };
  2280. static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
  2281. {
  2282. struct rpc_cb_add_xprt_calldata *data = calldata;
  2283. if (task->tk_status == 0)
  2284. rpc_xprt_switch_add_xprt(data->xps, data->xprt);
  2285. }
  2286. static void rpc_cb_add_xprt_release(void *calldata)
  2287. {
  2288. struct rpc_cb_add_xprt_calldata *data = calldata;
  2289. xprt_put(data->xprt);
  2290. xprt_switch_put(data->xps);
  2291. kfree(data);
  2292. }
  2293. static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
  2294. .rpc_call_done = rpc_cb_add_xprt_done,
  2295. .rpc_release = rpc_cb_add_xprt_release,
  2296. };
  2297. /**
  2298. * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
  2299. * @clnt: pointer to struct rpc_clnt
  2300. * @xps: pointer to struct rpc_xprt_switch,
  2301. * @xprt: pointer struct rpc_xprt
  2302. * @dummy: unused
  2303. */
  2304. int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
  2305. struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
  2306. void *dummy)
  2307. {
  2308. struct rpc_cb_add_xprt_calldata *data;
  2309. struct rpc_cred *cred;
  2310. struct rpc_task *task;
  2311. data = kmalloc(sizeof(*data), GFP_NOFS);
  2312. if (!data)
  2313. return -ENOMEM;
  2314. data->xps = xprt_switch_get(xps);
  2315. data->xprt = xprt_get(xprt);
  2316. cred = authnull_ops.lookup_cred(NULL, NULL, 0);
  2317. task = rpc_call_null_helper(clnt, xprt, cred,
  2318. RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC,
  2319. &rpc_cb_add_xprt_call_ops, data);
  2320. put_rpccred(cred);
  2321. if (IS_ERR(task))
  2322. return PTR_ERR(task);
  2323. rpc_put_task(task);
  2324. return 1;
  2325. }
  2326. EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
  2327. /**
  2328. * rpc_clnt_setup_test_and_add_xprt()
  2329. *
  2330. * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
  2331. * 1) caller of the test function must dereference the rpc_xprt_switch
  2332. * and the rpc_xprt.
  2333. * 2) test function must call rpc_xprt_switch_add_xprt, usually in
  2334. * the rpc_call_done routine.
  2335. *
  2336. * Upon success (return of 1), the test function adds the new
  2337. * transport to the rpc_clnt xprt switch
  2338. *
  2339. * @clnt: struct rpc_clnt to get the new transport
  2340. * @xps: the rpc_xprt_switch to hold the new transport
  2341. * @xprt: the rpc_xprt to test
  2342. * @data: a struct rpc_add_xprt_test pointer that holds the test function
  2343. * and test function call data
  2344. */
  2345. int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt,
  2346. struct rpc_xprt_switch *xps,
  2347. struct rpc_xprt *xprt,
  2348. void *data)
  2349. {
  2350. struct rpc_cred *cred;
  2351. struct rpc_task *task;
  2352. struct rpc_add_xprt_test *xtest = (struct rpc_add_xprt_test *)data;
  2353. int status = -EADDRINUSE;
  2354. xprt = xprt_get(xprt);
  2355. xprt_switch_get(xps);
  2356. if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
  2357. goto out_err;
  2358. /* Test the connection */
  2359. cred = authnull_ops.lookup_cred(NULL, NULL, 0);
  2360. task = rpc_call_null_helper(clnt, xprt, cred,
  2361. RPC_TASK_SOFT | RPC_TASK_SOFTCONN,
  2362. NULL, NULL);
  2363. put_rpccred(cred);
  2364. if (IS_ERR(task)) {
  2365. status = PTR_ERR(task);
  2366. goto out_err;
  2367. }
  2368. status = task->tk_status;
  2369. rpc_put_task(task);
  2370. if (status < 0)
  2371. goto out_err;
  2372. /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
  2373. xtest->add_xprt_test(clnt, xprt, xtest->data);
  2374. /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
  2375. return 1;
  2376. out_err:
  2377. xprt_put(xprt);
  2378. xprt_switch_put(xps);
  2379. pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not added\n",
  2380. status, xprt->address_strings[RPC_DISPLAY_ADDR]);
  2381. return status;
  2382. }
  2383. EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt);
  2384. /**
  2385. * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
  2386. * @clnt: pointer to struct rpc_clnt
  2387. * @xprtargs: pointer to struct xprt_create
  2388. * @setup: callback to test and/or set up the connection
  2389. * @data: pointer to setup function data
  2390. *
  2391. * Creates a new transport using the parameters set in args and
  2392. * adds it to clnt.
  2393. * If ping is set, then test that connectivity succeeds before
  2394. * adding the new transport.
  2395. *
  2396. */
  2397. int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
  2398. struct xprt_create *xprtargs,
  2399. int (*setup)(struct rpc_clnt *,
  2400. struct rpc_xprt_switch *,
  2401. struct rpc_xprt *,
  2402. void *),
  2403. void *data)
  2404. {
  2405. struct rpc_xprt_switch *xps;
  2406. struct rpc_xprt *xprt;
  2407. unsigned long connect_timeout;
  2408. unsigned long reconnect_timeout;
  2409. unsigned char resvport;
  2410. int ret = 0;
  2411. rcu_read_lock();
  2412. xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
  2413. xprt = xprt_iter_xprt(&clnt->cl_xpi);
  2414. if (xps == NULL || xprt == NULL) {
  2415. rcu_read_unlock();
  2416. xprt_switch_put(xps);
  2417. return -EAGAIN;
  2418. }
  2419. resvport = xprt->resvport;
  2420. connect_timeout = xprt->connect_timeout;
  2421. reconnect_timeout = xprt->max_reconnect_timeout;
  2422. rcu_read_unlock();
  2423. xprt = xprt_create_transport(xprtargs);
  2424. if (IS_ERR(xprt)) {
  2425. ret = PTR_ERR(xprt);
  2426. goto out_put_switch;
  2427. }
  2428. xprt->resvport = resvport;
  2429. if (xprt->ops->set_connect_timeout != NULL)
  2430. xprt->ops->set_connect_timeout(xprt,
  2431. connect_timeout,
  2432. reconnect_timeout);
  2433. rpc_xprt_switch_set_roundrobin(xps);
  2434. if (setup) {
  2435. ret = setup(clnt, xps, xprt, data);
  2436. if (ret != 0)
  2437. goto out_put_xprt;
  2438. }
  2439. rpc_xprt_switch_add_xprt(xps, xprt);
  2440. out_put_xprt:
  2441. xprt_put(xprt);
  2442. out_put_switch:
  2443. xprt_switch_put(xps);
  2444. return ret;
  2445. }
  2446. EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
  2447. struct connect_timeout_data {
  2448. unsigned long connect_timeout;
  2449. unsigned long reconnect_timeout;
  2450. };
  2451. static int
  2452. rpc_xprt_set_connect_timeout(struct rpc_clnt *clnt,
  2453. struct rpc_xprt *xprt,
  2454. void *data)
  2455. {
  2456. struct connect_timeout_data *timeo = data;
  2457. if (xprt->ops->set_connect_timeout)
  2458. xprt->ops->set_connect_timeout(xprt,
  2459. timeo->connect_timeout,
  2460. timeo->reconnect_timeout);
  2461. return 0;
  2462. }
  2463. void
  2464. rpc_set_connect_timeout(struct rpc_clnt *clnt,
  2465. unsigned long connect_timeout,
  2466. unsigned long reconnect_timeout)
  2467. {
  2468. struct connect_timeout_data timeout = {
  2469. .connect_timeout = connect_timeout,
  2470. .reconnect_timeout = reconnect_timeout,
  2471. };
  2472. rpc_clnt_iterate_for_each_xprt(clnt,
  2473. rpc_xprt_set_connect_timeout,
  2474. &timeout);
  2475. }
  2476. EXPORT_SYMBOL_GPL(rpc_set_connect_timeout);
  2477. void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
  2478. {
  2479. rcu_read_lock();
  2480. xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
  2481. rcu_read_unlock();
  2482. }
  2483. EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
  2484. void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
  2485. {
  2486. rcu_read_lock();
  2487. rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
  2488. xprt);
  2489. rcu_read_unlock();
  2490. }
  2491. EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
  2492. bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
  2493. const struct sockaddr *sap)
  2494. {
  2495. struct rpc_xprt_switch *xps;
  2496. bool ret;
  2497. rcu_read_lock();
  2498. xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
  2499. ret = rpc_xprt_switch_has_addr(xps, sap);
  2500. rcu_read_unlock();
  2501. return ret;
  2502. }
  2503. EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
  2504. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  2505. static void rpc_show_header(void)
  2506. {
  2507. printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
  2508. "-timeout ---ops--\n");
  2509. }
  2510. static void rpc_show_task(const struct rpc_clnt *clnt,
  2511. const struct rpc_task *task)
  2512. {
  2513. const char *rpc_waitq = "none";
  2514. if (RPC_IS_QUEUED(task))
  2515. rpc_waitq = rpc_qname(task->tk_waitqueue);
  2516. printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
  2517. task->tk_pid, task->tk_flags, task->tk_status,
  2518. clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
  2519. clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
  2520. task->tk_action, rpc_waitq);
  2521. }
  2522. void rpc_show_tasks(struct net *net)
  2523. {
  2524. struct rpc_clnt *clnt;
  2525. struct rpc_task *task;
  2526. int header = 0;
  2527. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  2528. spin_lock(&sn->rpc_client_lock);
  2529. list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
  2530. spin_lock(&clnt->cl_lock);
  2531. list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
  2532. if (!header) {
  2533. rpc_show_header();
  2534. header++;
  2535. }
  2536. rpc_show_task(clnt, task);
  2537. }
  2538. spin_unlock(&clnt->cl_lock);
  2539. }
  2540. spin_unlock(&sn->rpc_client_lock);
  2541. }
  2542. #endif
  2543. #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
  2544. static int
  2545. rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
  2546. struct rpc_xprt *xprt,
  2547. void *dummy)
  2548. {
  2549. return xprt_enable_swap(xprt);
  2550. }
  2551. int
  2552. rpc_clnt_swap_activate(struct rpc_clnt *clnt)
  2553. {
  2554. if (atomic_inc_return(&clnt->cl_swapper) == 1)
  2555. return rpc_clnt_iterate_for_each_xprt(clnt,
  2556. rpc_clnt_swap_activate_callback, NULL);
  2557. return 0;
  2558. }
  2559. EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
  2560. static int
  2561. rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
  2562. struct rpc_xprt *xprt,
  2563. void *dummy)
  2564. {
  2565. xprt_disable_swap(xprt);
  2566. return 0;
  2567. }
  2568. void
  2569. rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
  2570. {
  2571. if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
  2572. rpc_clnt_iterate_for_each_xprt(clnt,
  2573. rpc_clnt_swap_deactivate_callback, NULL);
  2574. }
  2575. EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
  2576. #endif /* CONFIG_SUNRPC_SWAP */